summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-02-25 13:39:36 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-02-25 13:39:36 -0300
commite0260eb2d4085723302d637dd8f3fca339d18817 (patch)
treeb9cf6cdaf3c5b3a51c16ef0cf9bb46564e5ce807
parent5205f073c57ae4b69e90d35c02e3a1a1cca44eb4 (diff)
downloadlua-github-e0260eb2d4085723302d637dd8f3fca339d18817.tar.gz
Bug (kind of) in 'isinstack'
The function 'isinstack' tried to work around the undefined behavior of subtracting two pointers that do not point to the same object, but the compiler killed to trick. (It optimizes out the safety check, because in a correct execution it will be always true.)
-rw-r--r--ldebug.c16
-rw-r--r--testes/errors.lua7
2 files changed, 17 insertions, 6 deletions
diff --git a/ldebug.c b/ldebug.c
index 603c39fc..8e3657a9 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -638,14 +638,18 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
/*
-** The subtraction of two potentially unrelated pointers is
-** not ISO C, but it should not crash a program; the subsequent
-** checks are ISO C and ensure a correct result.
+** Check whether pointer 'o' points to some value in the stack
+** frame of the current function. Because 'o' may not point to a
+** value in this stack, we cannot compare it with the region
+** boundaries (undefined behaviour in ISO C).
*/
static int isinstack (CallInfo *ci, const TValue *o) {
- StkId base = ci->func + 1;
- ptrdiff_t i = cast(StkId, o) - base;
- return (0 <= i && i < (ci->top - base) && s2v(base + i) == o);
+ StkId pos;
+ for (pos = ci->func + 1; pos < ci->top; pos++) {
+ if (o == s2v(pos))
+ return 1;
+ }
+ return 0; /* not found */
}
diff --git a/testes/errors.lua b/testes/errors.lua
index 4249f570..fd02806e 100644
--- a/testes/errors.lua
+++ b/testes/errors.lua
@@ -191,6 +191,13 @@ checkmessage("a = 24 // 0", "divide by zero")
checkmessage("a = 1 % 0", "'n%0'")
+-- type error for an object which is neither in an upvalue nor a register.
+-- The following code will try to index the value 10 that is stored in
+-- the metatable, without moving it to a register.
+checkmessage("local a = setmetatable({}, {__index = 10}).x",
+ "attempt to index a number value")
+
+
-- numeric for loops
checkmessage("for i = {}, 10 do end", "table")
checkmessage("for i = io.stdin, 10 do end", "FILE")