HaxeFoundation / hxcpp

Runtime files for c++ backend for haxe
Other
300 stars 193 forks source link

FileStat size integer overflow #705

Open haxiomic opened 6 years ago

haxiomic commented 6 years ago

sys.FileSystem.stat(path).size returns a negative number for a file more than 2.1 GB long

The problem is all values from stat are cast to 4 byte int: STATF(size); Sys.cpp#L410 #define STATF(f) o->Add(HX_CSTRING(#f),(int)(s.st_##f)) Sys.cpp#L369

(also mode is set twice)

haxiomic commented 6 years ago

It can be fixed by using the correct types from sys/types.h:

o->Add(HX_CSTRING("gid"), (int) s.st_gid);
o->Add(HX_CSTRING("uid"), (int) s.st_uid);
o->Add(HX_CSTRING("atime"), (int) s.st_atime);
o->Add(HX_CSTRING("mtime"), (int) s.st_mtime);
o->Add(HX_CSTRING("ctime"), (int) s.st_ctime);
o->Add(HX_CSTRING("dev"), (unsigned int) s.st_dev);
o->Add(HX_CSTRING("ino"), (unsigned int) s.st_ino);
o->Add(HX_CSTRING("mode"), (unsigned int) s.st_mode);
o->Add(HX_CSTRING("nlink"), (unsigned int) s.st_nlink);
o->Add(HX_CSTRING("rdev"), (unsigned int) s.st_rdev);
o->Add(HX_CSTRING("size"), (::cpp::Int64) s.st_size);

And adjusting FileStat to type size as cpp.Int64

Happy to PR if you don't see any issues