diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-06-16 16:33:02 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-06-16 16:33:02 -0300 |
| commit | 1b0f943da7dfb25987456a77259edbeea0b94edc (patch) | |
| tree | 6d095ff675b6779078e4368db338ad57562d7651 | |
| parent | 6e22fedb74cf0c9b6656e9fce8b7331db847c605 (diff) | |
| download | lua-1b0f943da7dfb25987456a77259edbeea0b94edc.tar.gz lua-1b0f943da7dfb25987456a77259edbeea0b94edc.tar.bz2 lua-1b0f943da7dfb25987456a77259edbeea0b94edc.zip | |
Bug: new metatable in weak table can fool the GC
All-weak tables are not being revisited after being visited during
propagation; if it gets a new metatable after that, the new metatable
may not be marked.
| -rw-r--r-- | lgc.c | 8 | ||||
| -rw-r--r-- | testes/gc.lua | 10 |
2 files changed, 16 insertions, 2 deletions
| @@ -553,8 +553,12 @@ static lu_mem traversetable (global_State *g, Table *h) { | |||
| 553 | traverseweakvalue(g, h); | 553 | traverseweakvalue(g, h); |
| 554 | else if (!weakvalue) /* strong values? */ | 554 | else if (!weakvalue) /* strong values? */ |
| 555 | traverseephemeron(g, h, 0); | 555 | traverseephemeron(g, h, 0); |
| 556 | else /* all weak */ | 556 | else { /* all weak */ |
| 557 | linkgclist(h, g->allweak); /* nothing to traverse now */ | 557 | if (g->gcstate == GCSpropagate) |
| 558 | linkgclist(h, g->grayagain); /* must visit again its metatable */ | ||
| 559 | else | ||
| 560 | linkgclist(h, g->allweak); /* must clear collected entries */ | ||
| 561 | } | ||
| 558 | } | 562 | } |
| 559 | else /* not weak */ | 563 | else /* not weak */ |
| 560 | traversestrongtable(g, h); | 564 | traversestrongtable(g, h); |
diff --git a/testes/gc.lua b/testes/gc.lua index 03093e34..f017f330 100644 --- a/testes/gc.lua +++ b/testes/gc.lua | |||
| @@ -301,6 +301,16 @@ collectgarbage() | |||
| 301 | assert(next(a) == string.rep('$', 11)) | 301 | assert(next(a) == string.rep('$', 11)) |
| 302 | 302 | ||
| 303 | 303 | ||
| 304 | if T then -- bug since 5.3: all-weak tables are not being revisited | ||
| 305 | T.gcstate("propagate") | ||
| 306 | local t = setmetatable({}, {__mode = "kv"}) | ||
| 307 | T.gcstate("atomic") -- 't' was visited | ||
| 308 | setmetatable(t, {__mode = "kv"}) | ||
| 309 | T.gcstate("pause") -- its new metatable is not being visited | ||
| 310 | assert(getmetatable(t).__mode == "kv") | ||
| 311 | end | ||
| 312 | |||
| 313 | |||
| 304 | -- 'bug' in 5.1 | 314 | -- 'bug' in 5.1 |
| 305 | a = {} | 315 | a = {} |
| 306 | local t = {x = 10} | 316 | local t = {x = 10} |
