From 8cb84210ab95e882c01363a6794508ec923ade90 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 13 Nov 2018 13:50:33 -0200 Subject: String buffer using to-be-closed variable The string buffers in the C API now mark their boxes as to-be-closed variables, to release their buffers in case of errors. --- testes/locals.lua | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'testes') diff --git a/testes/locals.lua b/testes/locals.lua index 28f88e54..8766b3d5 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -324,6 +324,38 @@ if rawget(_G, "T") then local _, msg = pcall(test) assert(msg == 1000) + + do -- testing 'toclose' in C string buffer + local s = string.rep("a", 10000) + local a = {s, s} + + -- ensure proper initialization (stack space, metatable) + table.concat(a) + collectgarbage(); collectgarbage() + + local m = T.totalmem() + + -- error in the second buffer allocation + T.alloccount(3) + assert(not pcall(table.concat, a)) + T.alloccount() + -- first buffer was released by 'toclose' + assert(T.totalmem() - m <= 5000) + + -- error in creation of final string + T.alloccount(4) + assert(not pcall(table.concat, a)) + T.alloccount() + -- second buffer was released by 'toclose' + assert(T.totalmem() - m <= 5000) + + -- userdata, upvalue, buffer, buffer, string + T.alloccount(5) + assert(#table.concat(a) == 20000) + T.alloccount() + + print'+' + end end -- cgit v1.2.3-55-g6feb