justincormack / ljsyscall

LuaJIT Unix syscall FFI
http://www.myriabit.com/ljsyscall/
Other
441 stars 54 forks source link

Make "offset" arg to lseek a signed integer #224

Closed wingo closed 6 years ago

wingo commented 6 years ago

Otherwise, passing a negative seek amount as a normal Lua number will involve a cast from double to uint64. In C it's undefined behavior when a double outside the [0,2^64) range is cast to uint64. In Lua we try to additionally accomodate the range [-2^63, -1], but there is a bug on x64-64 and might be a bug on other platforms:

https://github.com/LuaJIT/LuaJIT/pull/415

However it's cheaper to simply target an int64_t when you want to allow negative numbers, as is our case, because you don't have to do assembly heroics for it to do what you want. The tonumber call in the return of linux.lua:lseek indicates anyway that our range is not the full 64-bit range, so we might as well restrict instead to long rather than ulong.

wingo commented 6 years ago

See #223 for more discussion.

justincormack commented 6 years ago

Yes I was thinking about this and think it makes sense (wonder if there are other cases...)