Giter VIP home page Giter VIP logo

Comments (5)

TERESH1 avatar TERESH1 commented on June 12, 2024 1

For checking it and getting file size you can use something like this:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int get_file_size(const char* filename, size_t& out_result) {
   struct stat st;
   if(stat(filename, &st) != 0) {
      // return error
   }
   if (!S_ISREG(st.st_mode)) {
      // return error
   }
   out_result = st.st_size;
   // return ok
}

Or use Filesystem library (since C++17) for getting size: std::filesystem::path with std::filesystem::file_size

In case of using it with dirs it throws exception:

terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
  what():  filesystem error: cannot get file size: Is a directory [/]

from pugixml.

TERESH1 avatar TERESH1 commented on June 12, 2024 1

The bug was found with Futag

from pugixml.

zeux avatar zeux commented on June 12, 2024

Thanks! This should not have production impact, as the code correctly handles the OOM, but is certainly inconvenient for fuzzing. ftell here returns the maximum signed integer of the relevant type instead of returning a negative number for some reason.

std::filesystem can't be used in pugixml as it doesn't use STL except for STL-specific API helpers that can be disabled, and carries compatibility burden on systems like macOS. It is probably better to solve this by explicitly checking the size; stat could work but has some other issues and an extra syscall per file open is not optimal for some workloads. That said, maybe stat is actually fine; I'll look into this further.

from pugixml.

zeux avatar zeux commented on June 12, 2024

Ah, looks like I hit this before :) 7664bbf

from pugixml.

zeux avatar zeux commented on June 12, 2024

Looks like fseek/ftell use fstatat under the hood, so stat is actually more efficient (only matters for small files, but right now it takes us 7 syscalls to read a file, and with stat it only takes 5; one of these is done by fread and can be removed by switching to open/read or by disabling buffering via setvbuf).

from pugixml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.