Lua_Integer and lua_createtable (table size limit)

In Lua 5.3, tables associated with functions in the C API accept and return lua_Integer .

 void lua_rawgeti (lua_State *L, int idx, lua_Integer n); void lua_rawseti (lua_State *L, int idx, lua_Integer n); lua_Integer luaL_len (lua_State *L, int index); 

But lua_createtable still gets int .

 void lua_createtable (lua_State *L, int narr, int nrec); 

In the sample function below, the length of the source table is used to create a copy with the same size.

 static int copy_sequence(lua_State *L) { lua_Integer len, i; luaL_checktype(L, 1, LUA_TTABLE); len = luaL_len(L, 1); lua_createtable(L, (int)len, 0); /* conversion warning */ for (i = 1; i <= len; i++) { lua_rawgeti(L, 1, i); lua_rawseti(L, -2, i); } return 1; } 

But to turn off the warning, you need to throw:

warning: converting to 'int from' lua_Integer may change its value [-Wconversion]

Searching the Lua mailing list I found the following thread that applies to Lua 5.2 (also applies to earlier versions, which I also allow):

Quote: Roberto Ierusalimschy (August 7, 2012)

The size of the tables is already limited to 2147483647 elements. Lua internally uses 'int' to index all of its arrays (except strings / byte arrays). It is a pain to work with unsigned values ​​(such as size_t) everywhere; ptrdiff_t has no warranty.

Is this still the case for Lua 5.3, which uses long long for lua_Integer ? Is listing int from lua_Integer as used in the above example safe in Lua 5.3?

+5
source share
1 answer

The size of the table (number of elements) is still limited to "int". Which does not prevent the table from having arbitrary lua_Integer keys (as long as the table is not the correct sequence).

+5
source

Source: https://habr.com/ru/post/1210824/


All Articles