Mercurial > prosody-hg
comparison util/multitable.lua @ 14140:c9c4a13732e4 13.0
util.multitable: Don't treat false as a missing key
'if not next(some_table)' is a tempting pattern to check whether a table is
empty, but we have had multiple bugs in the past where a literal `false` was
present as a key, incorrectly matching this test.
The correct test is to test for nil, which cannot ever be a key, and means the
table is legitimately empty.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 17 Apr 2026 17:45:31 +0100 |
| parents | 39ae08180c81 |
| children |
comparison
equal
deleted
inserted
replaced
| 14139:374b72785488 | 14140:c9c4a13732e4 |
|---|---|
| 56 end | 56 end |
| 57 if k then | 57 if k then |
| 58 local v = t[k]; | 58 local v = t[k]; |
| 59 if v then | 59 if v then |
| 60 r(v, n+1, _end, ...); | 60 r(v, n+1, _end, ...); |
| 61 if not next(v) then | 61 if next(v) == nil then |
| 62 t[k] = nil; | 62 t[k] = nil; |
| 63 end | 63 end |
| 64 end | 64 end |
| 65 else | 65 else |
| 66 for _,b in pairs(t) do | 66 for _,b in pairs(t) do |
| 67 r(b, n+1, _end, ...); | 67 r(b, n+1, _end, ...); |
| 68 if not next(b) then | 68 if next(b) == nil then |
| 69 t[_] = nil; | 69 t[_] = nil; |
| 70 end | 70 end |
| 71 end | 71 end |
| 72 end | 72 end |
| 73 end | 73 end |
