lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
| 1 | -- $Id: testes/heavy.lua,v $ |
| 2 | -- See Copyright Notice in file lua.h |
| 3 | |
| 4 | global <const> * |
| 5 | |
| 6 | local function teststring () |
| 7 | print("creating a string too long") |
| 8 | do |
| 9 | local a = "x" |
| 10 | local st, msg = pcall(function () |
| 11 | while true do |
| 12 | a = a .. a.. a.. a.. a.. a.. a.. a.. a.. a |
| 13 | .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a |
| 14 | .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a |
| 15 | .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a |
| 16 | .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a |
| 17 | .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a |
| 18 | .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a |
| 19 | .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a |
| 20 | .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a |
| 21 | .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a |
| 22 | print(string.format("string with %d bytes", #a)) |
| 23 | end |
| 24 | end) |
| 25 | assert(not st and |
| 26 | (string.find(msg, "string length overflow") or |
| 27 | string.find(msg, "not enough memory"))) |
| 28 | print("string length overflow with " .. #a * 100) |
| 29 | end |
| 30 | print('+') |
| 31 | end |
| 32 | |
| 33 | local function loadrep (x, what) |
| 34 | local p = 1<<20 |
| 35 | local s = string.rep(x, p) |
| 36 | local count = 0 |
| 37 | local function f() |
| 38 | count = count + p |
| 39 | if count % (0x80*p) == 0 then |
| 40 | io.stderr:write("(", count // 2^20, " M)") |
| 41 | end |
| 42 | return s |
| 43 | end |
| 44 | local st, msg = load(f, "=big") |
| 45 | print("\nmemory: ", collectgarbage'count' * 1024) |
| 46 | msg = string.match(msg, "^[^\n]+") -- get only first line |
| 47 | print(string.format("total: 0x%x %s ('%s')", count, what, msg)) |
| 48 | return st, msg |
| 49 | end |
| 50 | |
| 51 | |
| 52 | local function controlstruct () |
| 53 | print("control structure too long") |
| 54 | local lim = ((1 << 24) - 2) // 4 |
| 55 | local s = string.rep("a = a + 1\n", lim) |
| 56 | s = "while true do " .. s .. "end" |
| 57 | assert(load(s)) |
| 58 | print("ok with " .. lim .. " lines") |
| 59 | lim = lim + 3 |
| 60 | s = string.rep("a = a + 1\n", lim) |
| 61 | s = "while true do " .. s .. "end" |
| 62 | local st, msg = load(s) |
| 63 | assert(not st and string.find(msg, "too long")) |
| 64 | print(msg) |
| 65 | end |
| 66 | |
| 67 | |
| 68 | local function manylines () |
| 69 | print("loading chunk with too many lines") |
| 70 | local st, msg = loadrep("\n", "lines") |
| 71 | assert(not st and string.find(msg, "too many lines")) |
| 72 | print('+') |
| 73 | end |
| 74 | |
| 75 | |
| 76 | local function hugeid () |
| 77 | print("loading chunk with huge identifier") |
| 78 | local st, msg = loadrep("a", "chars") |
| 79 | assert(not st and |
| 80 | (string.find(msg, "lexical element too long") or |
| 81 | string.find(msg, "not enough memory"))) |
| 82 | print('+') |
| 83 | end |
| 84 | |
| 85 | local function toomanyinst () |
| 86 | print("loading chunk with too many instructions") |
| 87 | local st, msg = loadrep("a = 10; ", "instructions") |
| 88 | print('+') |
| 89 | end |
| 90 | |
| 91 | |
| 92 | local function loadrepfunc (prefix, f) |
| 93 | local count = -1 |
| 94 | local function aux () |
| 95 | count = count + 1 |
| 96 | if count == 0 then |
| 97 | return prefix |
| 98 | else |
| 99 | if count % (0x100000) == 0 then |
| 100 | io.stderr:write("(", count // 2^20, " M)") |
| 101 | end |
| 102 | return f(count) |
| 103 | end |
| 104 | end |
| 105 | local st, msg = load(aux, "k") |
| 106 | print("\nmemory: ", collectgarbage'count' * 1024) |
| 107 | msg = string.match(msg, "^[^\n]+") -- get only first line |
| 108 | print("expected error: ", msg) |
| 109 | end |
| 110 | |
| 111 | |
| 112 | local function toomanyconst () |
| 113 | print("loading function with too many constants") |
| 114 | loadrepfunc("function foo () return {0,", |
| 115 | function (n) |
| 116 | -- convert 'n' to a string in the format [["...",]], |
| 117 | -- where '...' is a kind of number in base 128 |
| 118 | -- (in a range that does not include either the double quote |
| 119 | -- and the escape.) |
| 120 | return string.char(34, |
| 121 | ((n // 128^0) & 127) + 128, |
| 122 | ((n // 128^1) & 127) + 128, |
| 123 | ((n // 128^2) & 127) + 128, |
| 124 | ((n // 128^3) & 127) + 128, |
| 125 | ((n // 128^4) & 127) + 128, |
| 126 | 34, 44) |
| 127 | end) |
| 128 | end |
| 129 | |
| 130 | |
| 131 | local function toomanystr () |
| 132 | local a = {} |
| 133 | local st, msg = pcall(function () |
| 134 | for i = 1, math.huge do |
| 135 | if i % (0x100000) == 0 then |
| 136 | io.stderr:write("(", i // 2^20, " M)") |
| 137 | end |
| 138 | a[i] = string.pack("I", i) |
| 139 | end |
| 140 | end) |
| 141 | local size = #a |
| 142 | a = collectgarbage'count' |
| 143 | print("\nmemory:", a * 1024) |
| 144 | print("expected error:", msg) |
| 145 | print("size:", size) |
| 146 | end |
| 147 | |
| 148 | |
| 149 | local function toomanyidx () |
| 150 | local a = {} |
| 151 | local st, msg = pcall(function () |
| 152 | for i = 1, math.huge do |
| 153 | if i % (0x100000) == 0 then |
| 154 | io.stderr:write("(", i // 2^20, " M)") |
| 155 | end |
| 156 | a[i] = i |
| 157 | end |
| 158 | end) |
| 159 | print("\nmemory: ", collectgarbage'count' * 1024) |
| 160 | print("expected error: ", msg) |
| 161 | print("size:", #a) |
| 162 | end |
| 163 | |
| 164 | |
| 165 | |
| 166 | -- teststring() |
| 167 | -- controlstruct() |
| 168 | -- manylines() |
| 169 | -- hugeid() |
| 170 | -- toomanyinst() |
| 171 | -- toomanyconst() |
| 172 | -- toomanystr() |
| 173 | toomanyidx() |
| 174 | |
| 175 | print "OK" |