Table length evaluations --> Lua 5.5 vs. Lua 5.4

98 views
Skip to first unread message

Kritzel Kratzel

unread,
Feb 15, 2026, 6:05:20 AM (yesterday) Feb 15
to lua-l
While porting lua-cjson to Lua 5.5, I encountered a test case failure. It appears that Lua 5.5 handles table length evaluation differently than previous versions, especially regarding tables with holes:

[C:\...\tests]> lua -e "print(_VERSION); t={nil,true}; print(#t)"
Lua 5.5
0

[C:\...\tests]> C:\Apps\OneLuaPro-5.4.8.3-x64\bin\lua.exe -e "print(_VERSION); t={nil,true}; print(#t)"
Lua 5.4
2

Bug or feature?

Regards,
KK

Sainan

unread,
Feb 15, 2026, 6:15:09 AM (yesterday) Feb 15
to lu...@googlegroups.com

Błażej Roszkowski

unread,
Feb 15, 2026, 7:05:07 AM (yesterday) Feb 15
to lu...@googlegroups.com
It seems there are still bugs. Comment in C source says "or 0 if t[1] is absent" but
for {nil, 1, 1, 1} the # yields 4 (due to *lenhint being 2 so 1 is never checked).

On Sun, 15 Feb 2026 at 12:15, 'Sainan' via lua-l <lu...@googlegroups.com> wrote:
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/-cUyh55EpmfwvcYw_VudoxV8qT60SexwLNx36uzfDGDSDOS2-GGp4Kqh0lQxxX7NARzhur1LZMlcyutRiYLuQ-KnvKOFizgH0wAA9Bks82E%3D%40calamity.inc.

Kritzel Kratzel

unread,
Feb 15, 2026, 7:21:34 AM (yesterday) Feb 15
to lua-l
Indeed:

[C:\...\tests]> lua -e "print(_VERSION); t={nil,true}; print(#t)"
Lua 5.5
0

[C:\...\tests]> lua -e "print(_VERSION); t={nil,true,true}; print(#t)"
Lua 5.5
0

[C:\...\tests]> lua -e "print(_VERSION); t={nil,true,true,true}; print(#t)"
Lua 5.5
4

[C:\...\tests]> lua -e "print(_VERSION); t={nil,true,true,true,true}; print(#t)"
Lua 5.5
5

[C:\...\tests]> lua -e "print(_VERSION); t={nil,true,true,true,true,true}; print(#t)"
Lua 5.5
6


I mean, is the the intented behavior?

Luiz Henrique de Figueiredo

unread,
Feb 15, 2026, 7:27:15 AM (yesterday) Feb 15
to lu...@googlegroups.com
> It appears that Lua 5.5 handles table length evaluation differently than previous versions, especially regarding tables with holes:

Taking the length of a table with holes is undefined behavior.

Luther Thompson

unread,
Feb 15, 2026, 9:48:44 AM (yesterday) Feb 15
to lu...@googlegroups.com
On 2/15/26 07:04, Błażej Roszkowski wrote:
> It seems there are still bugs. Comment in C source says "or 0 if t[1]
> is absent" but
> for {nil, 1, 1, 1} the # yields 4 (due to *lenhint being 2 so 1 is
> never checked).

I don't know where that comment is, but if it's like the documentation
at https://www.lua.org/manual/5.5/manual.html#3.4.7, it's merely part of
the definition of a border. Since the # result can be any border, it
won't always be 0 if there are other borders in the table.

Luther

Błażej Roszkowski

unread,
Feb 15, 2026, 10:39:47 AM (yesterday) Feb 15
to lu...@googlegroups.com
The comment in ltable.c line 1291 above luaH_getn:

** Try to find a border in table 't'. (A 'border' is an integer index
** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent,
** or 'maxinteger' if t[maxinteger] is present.)

The "or 0 if t[1] is absent" makes it sound like it took precedence over the first definition.

OTOH the documentation is correct and even mentions if there's more
than one border (non-sequence table) then any can be returned.

--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages