[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: `string.unpack` "s bug in option `z`
- From: Dirk Laurie <dirk.laurie@...>
- Date: Mon, 3 Sep 2018 11:51:50 +0200
Op Ma., 3 Sep. 2018 om 04:29 het actboy168 <[email protected]> geskryf:
> Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
>
> > print(string.unpack("z", "ABC"))
> ABC 5
> This is not a zero-terminated string, so I think lua should raise an error.
You are right that "ABC" is not a zero-terminated string, but I do not
agree that Lua should raise an error. The manual says "All integral
options check overflows; ... string.unpack checks whether the read
value fits in a Lua integer." There is no risk of overflow here, so an
error should not be raised, and the manual almost says so: "Options
"c" and "z" are not aligned".
The string pack/unpack routines are taken from Roberto"s "struct"
library http://www.inf.puc-rio.br/~roberto/struct/, which has been in
existence for at least six years (timestamp on the makefile).
BTW that library is a beautiful example of how to write C API code
that compiles on all Lua versions from 5.0 to 5.4 with only this
version-dependent test:
#if (LUA_VERSION_NUM >= 502)
#define luaL_register(L,n,f) luaL_newlib(L,f)
#endif