lua-users home
lua-l archive

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


But the struct library will raise an error.

 

 

--actboy168

 

发件人: Dirk Laurie
发送时间: 201893 17:52
收件人: Lua mailing list
主题: Re: `string.unpack` 's bug in option `z`

 

Op Ma., 3 Sep. 2018 om 04:29 het actboy168 <actboy168@gmail.com> 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