Visitar URL original
Bug: Bad error message with fields of string _ENV · lua/lua@d1ee2a4 · GitHub
Skip to content

Commit d1ee2a4

Browse files
committed
Bug: Bad error message with fields of string _ENV
Errors with fields of the string _ENV (e.g., ("_ENV").u + 1) report the error as comming from a global.
1 parent 3fe7be9 commit d1ee2a4

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

‎ldebug.c‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
3838
const char **name);
3939

40+
static const char strlocal[] = "local";
41+
static const char strupval[] = "upvalue";
42+
4043

4144
static int currentpc (CallInfo *ci) {
4245
lua_assert(isLua(ci));
@@ -497,7 +500,7 @@ static const char *basicgetobjname (const Proto *p, int *ppc, int reg,
497500
int pc = *ppc;
498501
*name = luaF_getlocalname(p, reg + 1, pc);
499502
if (*name) /* is a local? */
500-
return "local";
503+
return strlocal;
501504
/* else try symbolic execution */
502505
*ppc = pc = findsetreg(p, pc, reg);
503506
if (pc != -1) { /* could find instruction? */
@@ -512,7 +515,7 @@ static const char *basicgetobjname (const Proto *p, int *ppc, int reg,
512515
}
513516
case OP_GETUPVAL: {
514517
*name = upvalname(p, GETARG_B(i));
515-
return "upvalue";
518+
return strupval;
516519
}
517520
case OP_LOADK: return kname(p, GETARG_Bx(i), name);
518521
case OP_LOADKX: return kname(p, GETARG_Ax(p->code[pc + 1]), name);
@@ -547,15 +550,21 @@ static void rkname (const Proto *p, int pc, Instruction i, const char **name) {
547550

548551
/*
549552
** Check whether table being indexed by instruction 'i' is the
550-
** environment '_ENV'
553+
** environment '_ENV'. If the table is an upvalue, get its name;
554+
** otherwise, find some "name" for the table and check whether
555+
** that name is the name of a local variable (and not, for instance,
556+
** a string). Then check that, if there is a name, it is '_ENV'.
551557
*/
552558
static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) {
553559
int t = GETARG_B(i); /* table index */
554560
const char *name; /* name of indexed variable */
555561
if (isup) /* is 't' an upvalue? */
556562
name = upvalname(p, t);
557-
else /* 't' is a register */
558-
basicgetobjname(p, &pc, t, &name);
563+
else { /* 't' is a register */
564+
const char *what = basicgetobjname(p, &pc, t, &name);
565+
if (what != strlocal && what != strupval)
566+
name = NULL; /* cannot be the variable _ENV */
567+
}
559568
return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
560569
}
561570

@@ -701,7 +710,7 @@ static const char *getupvalname (CallInfo *ci, const TValue *o,
701710
for (i = 0; i < c->nupvalues; i++) {
702711
if (c->upvals[i]->v.p == o) {
703712
*name = upvalname(c->p, i);
704-
return "upvalue";
713+
return strupval;
705714
}
706715
}
707716
return NULL;

‎testes/errors.lua‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ checkmessage("aaa=(1)..{}", "a table value")
137137
-- bug in 5.4.6
138138
checkmessage("a = {_ENV = {}}; print(a._ENV.x + 1)", "field 'x'")
139139

140+
-- a similar bug in 5.4.7, since 5.4.0
141+
checkmessage("print(('_ENV').x + 1)", "field 'x'")
142+
143+
140144
_G.aaa, _G.bbbb = nil
141145

142146
-- calls

0 commit comments

Comments
 (0)