lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Mar 12, 2020, at 6:53 PM, dyngeccetor8 wrote:
> On 12/03/2020 20.06, cherue wrote:
> > Hello,
> > 
> > I found a bug in string.unpack:
> > 
> > ```
> > Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> >> string.unpack("I4", "\255\255\255\255")
> > 4294967295	5
> >> string.unpack("I8", "\255\255\255\255\255\255\255\255")
> > -1	9
> >> string.unpack("I8", "\255\255\255\255\255\255\255\127")
> > 9223372036854775807	9
> >> string.unpack("I8", "\255\255\255\255\255\255\255\128")
> > -9151314442816847873	9
> > ```
> > 
> > I tried looking for the cause but I am not fluent in C. Maybe something about lua_Integer?
> > 
> 
> So where is the bug?
> 
> In my point of view you've got quite consistent results
> for two's-complement little-endian integer encoding.
> 
> -- Martin

I should have gone into more detail, sorry.

The documentation on format strings includes:
- i[n]: a signed int with n bytes (default is native size)
- I[n]: an unsigned int with n bytes (default is native size)

So string.unpack("I8", "\255\255\255\255\255\255\255\255")
should be an unsigned integer with value 18446744073709551615.