diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-11-13 13:50:33 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-11-13 13:50:33 -0200 |
| commit | 8cb84210ab95e882c01363a6794508ec923ade90 (patch) | |
| tree | 816598810dbf22e7bb119977863501edf067ffab /testes | |
| parent | 5fda30b4f9f82b901113a6e666c797f835c708eb (diff) | |
| download | lua-8cb84210ab95e882c01363a6794508ec923ade90.tar.gz lua-8cb84210ab95e882c01363a6794508ec923ade90.tar.bz2 lua-8cb84210ab95e882c01363a6794508ec923ade90.zip | |
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.
Diffstat (limited to 'testes')
| -rw-r--r-- | testes/locals.lua | 32 |
1 files changed, 32 insertions, 0 deletions
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 | |||
| 324 | local _, msg = pcall(test) | 324 | local _, msg = pcall(test) |
| 325 | assert(msg == 1000) | 325 | assert(msg == 1000) |
| 326 | 326 | ||
| 327 | |||
| 328 | do -- testing 'toclose' in C string buffer | ||
| 329 | local s = string.rep("a", 10000) | ||
| 330 | local a = {s, s} | ||
| 331 | |||
| 332 | -- ensure proper initialization (stack space, metatable) | ||
| 333 | table.concat(a) | ||
| 334 | collectgarbage(); collectgarbage() | ||
| 335 | |||
| 336 | local m = T.totalmem() | ||
| 337 | |||
| 338 | -- error in the second buffer allocation | ||
| 339 | T.alloccount(3) | ||
| 340 | assert(not pcall(table.concat, a)) | ||
| 341 | T.alloccount() | ||
| 342 | -- first buffer was released by 'toclose' | ||
| 343 | assert(T.totalmem() - m <= 5000) | ||
| 344 | |||
| 345 | -- error in creation of final string | ||
| 346 | T.alloccount(4) | ||
| 347 | assert(not pcall(table.concat, a)) | ||
| 348 | T.alloccount() | ||
| 349 | -- second buffer was released by 'toclose' | ||
| 350 | assert(T.totalmem() - m <= 5000) | ||
| 351 | |||
| 352 | -- userdata, upvalue, buffer, buffer, string | ||
| 353 | T.alloccount(5) | ||
| 354 | assert(#table.concat(a) == 20000) | ||
| 355 | T.alloccount() | ||
| 356 | |||
| 357 | print'+' | ||
| 358 | end | ||
| 327 | end | 359 | end |
| 328 | 360 | ||
| 329 | 361 | ||
