Skip to content

Commit 6bc0f13

Browse files
committed
Fixed bug of long strings in binary chunks
When "undumping" a long string, the function 'loadVector' can call the reader function, which can run the garbage collector, which can collect the string being read. So, the string must be anchored during the call to 'loadVector'.
1 parent 8a89da0 commit 6bc0f13

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lundump.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ static TString *loadStringN (LoadState *S, Proto *p) {
120120
}
121121
else { /* long string */
122122
ts = luaS_createlngstrobj(L, size); /* create string */
123+
setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */
124+
luaD_inctop(L);
123125
loadVector(S, getstr(ts), size); /* load directly in final place */
126+
L->top--; /* pop string */
124127
}
125128
luaC_objbarrier(L, p, ts);
126129
return ts;

testes/calls.lua

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,16 @@ f = load(string.dump(function () return 1 end), nil, "b", {})
317317
assert(type(f) == "function" and f() == 1)
318318

319319

320+
do -- another bug (in 5.4.0)
321+
-- loading a binary long string interrupted by GC cycles
322+
local f = string.dump(function ()
323+
return '01234567890123456789012345678901234567890123456789'
324+
end)
325+
f = load(read1(f))
326+
assert(f() == '01234567890123456789012345678901234567890123456789')
327+
end
328+
329+
320330
x = string.dump(load("x = 1; return x"))
321331
a = assert(load(read1(x), nil, "b"))
322332
assert(a() == 1 and _G.x == 1)
@@ -358,8 +368,12 @@ x = [[
358368
end
359369
end
360370
]]
371+
a = assert(load(read1(x), "read", "t"))
372+
assert(a()(2)(3)(10) == 15)
361373

362-
a = assert(load(read1(x)))
374+
-- repeat the test loading a binary chunk
375+
x = string.dump(a)
376+
a = assert(load(read1(x), "read", "b"))
363377
assert(a()(2)(3)(10) == 15)
364378

365379

0 commit comments

Comments
 (0)