aboutsummaryrefslogtreecommitdiff
path: root/lparser.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-06-17 14:04:03 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-06-17 14:04:03 -0300
commit36b6fe8d175bb2aec8fc55ffb090eab90cb12fd8 (patch)
treeefa22fb54a0d354249d9f3c9e0a26118d6527bd0 /lparser.c
parentd4dce57f5ca64c65c7c49eac683b8f807e8dc02d (diff)
downloadlua-36b6fe8d175bb2aec8fc55ffb090eab90cb12fd8.tar.gz
lua-36b6fe8d175bb2aec8fc55ffb090eab90cb12fd8.tar.bz2
lua-36b6fe8d175bb2aec8fc55ffb090eab90cb12fd8.zip
better treatment for arbitrary limits
Diffstat (limited to 'lparser.c')
-rw-r--r--lparser.c50
1 files changed, 31 insertions, 19 deletions
diff --git a/lparser.c b/lparser.c
index f44a9563..8fc3df90 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 1.35 1999/06/16 13:22:04 roberto Exp roberto $ 2** $Id: lparser.c,v 1.36 1999/06/16 13:35:01 roberto Exp roberto $
3** LL(1) Parser and code generator for Lua 3** LL(1) Parser and code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -22,26 +22,32 @@
22#include "lzio.h" 22#include "lzio.h"
23 23
24 24
25/* for limit numbers in error messages */
26#define MES_LIM(x) "(limit=" x ")"
27
28 25
29/* size of a "normal" jump instruction: OpCode + 1 byte */ 26/* size of a "normal" jump instruction: OpCode + 1 byte */
30#define JMPSIZE 2 27#define JMPSIZE 2
31 28
32/* maximum number of local variables */ 29/* maximum number of local variables */
33#define MAXLOCALS 200 30#ifndef MAXLOCALS
34#define SMAXLOCALS "200" 31#define MAXLOCALS 200 /* arbitrary limit (<256) */
32#endif
35 33
36 34
37/* maximum number of upvalues */ 35/* maximum number of upvalues */
38#define MAXUPVALUES 32 36#ifndef MAXUPVALUES
39#define SMAXUPVALUES "32" 37#define MAXUPVALUES 32 /* arbitrary limit (<256) */
38#endif
40 39
41 40
42/* maximum number of variables in the left side of an assignment */ 41/* maximum number of variables in the left side of an assignment */
43#define MAXVARSLH 100 42#ifndef MAXVARSLH
44#define SMAXVARSLH "100" 43#define MAXVARSLH 100 /* arbitrary limit (<255) */
44#endif
45
46
47/* maximum number of parameters in a function */
48#ifndef MAXPARAMS
49#define MAXPARAMS 100 /* arbitrary limit (<ZEROVARARG) */
50#endif
45 51
46 52
47/* 53/*
@@ -136,6 +142,15 @@ static void var_or_func_tail (LexState *ls, vardesc *v);
136 142
137 143
138 144
145static void checklimit (LexState *ls, int val, int limit, char *msg) {
146 if (val > limit) {
147 char buff[100];
148 sprintf(buff, "too many %s (limit=%d)", msg, limit);
149 luaX_error(ls, buff);
150 }
151}
152
153
139static void check_pc (FuncState *fs, int n) { 154static void check_pc (FuncState *fs, int n) {
140 luaM_growvector(fs->f->code, fs->pc, n, Byte, codeEM, MAX_INT); 155 luaM_growvector(fs->f->code, fs->pc, n, Byte, codeEM, MAX_INT);
141} 156}
@@ -310,8 +325,7 @@ static void luaI_unregisterlocalvar (FuncState *fs, int line) {
310 325
311static void store_localvar (LexState *ls, TaggedString *name, int n) { 326static void store_localvar (LexState *ls, TaggedString *name, int n) {
312 FuncState *fs = ls->fs; 327 FuncState *fs = ls->fs;
313 if (fs->nlocalvar+n >= MAXLOCALS) 328 checklimit(ls, fs->nlocalvar+n+1, MAXLOCALS, "local variables");
314 luaX_error(ls, "too many local variables " MES_LIM(SMAXLOCALS));
315 fs->localvar[fs->nlocalvar+n] = name; 329 fs->localvar[fs->nlocalvar+n] = name;
316 luaI_registerlocalvar(fs, name, ls->linenumber); 330 luaI_registerlocalvar(fs, name, ls->linenumber);
317} 331}
@@ -369,9 +383,8 @@ static int indexupvalue (LexState *ls, TaggedString *n) {
369 return i; 383 return i;
370 } 384 }
371 /* new one */ 385 /* new one */
372 if (++(fs->nupvalues) > MAXUPVALUES) 386 ++(fs->nupvalues);
373 luaX_error(ls, "too many upvalues in a single function " 387 checklimit(ls, fs->nupvalues, MAXUPVALUES, "upvalues");
374 MES_LIM(SMAXUPVALUES));
375 fs->upvalues[i] = v; /* i = fs->nupvalues - 1 */ 388 fs->upvalues[i] = v; /* i = fs->nupvalues - 1 */
376 return i; 389 return i;
377} 390}
@@ -439,6 +452,7 @@ static void adjust_mult_assign (LexState *ls, int nvars, listdesc *d) {
439static void code_args (LexState *ls, int nparams, int dots) { 452static void code_args (LexState *ls, int nparams, int dots) {
440 FuncState *fs = ls->fs; 453 FuncState *fs = ls->fs;
441 fs->nlocalvar += nparams; /* "self" may already be there */ 454 fs->nlocalvar += nparams; /* "self" may already be there */
455 checklimit(ls, fs->nlocalvar, MAXPARAMS, "parameters");
442 nparams = fs->nlocalvar; 456 nparams = fs->nlocalvar;
443 if (!dots) { 457 if (!dots) {
444 fs->f->code[1] = (Byte)nparams; /* fill-in arg information */ 458 fs->f->code[1] = (Byte)nparams; /* fill-in arg information */
@@ -917,7 +931,7 @@ static int priority [POW+1] = {5, 5, 1, 1, 1, 1, 1, 1, 2, 3, 3, 4, 4, 6};
917static OpCode opcodes [POW+1] = {NOTOP, MINUSOP, EQOP, NEQOP, GTOP, LTOP, 931static OpCode opcodes [POW+1] = {NOTOP, MINUSOP, EQOP, NEQOP, GTOP, LTOP,
918 LEOP, GEOP, CONCOP, ADDOP, SUBOP, MULTOP, DIVOP, POWOP}; 932 LEOP, GEOP, CONCOP, ADDOP, SUBOP, MULTOP, DIVOP, POWOP};
919 933
920#define MAXOPS 20 /* op's stack size */ 934#define MAXOPS 20 /* op's stack size (arbitrary limit) */
921 935
922typedef struct stack_op { 936typedef struct stack_op {
923 int ops[MAXOPS]; 937 int ops[MAXOPS];
@@ -1226,9 +1240,7 @@ static void decinit (LexState *ls, listdesc *d) {
1226 1240
1227static int assignment (LexState *ls, vardesc *v, int nvars) { 1241static int assignment (LexState *ls, vardesc *v, int nvars) {
1228 int left = 0; 1242 int left = 0;
1229 if (nvars > MAXVARSLH) 1243 checklimit(ls, nvars, MAXVARSLH, "variables in a multiple assignment");
1230 luaX_error(ls, "too many variables in a multiple assignment "
1231 MES_LIM(SMAXVARSLH));
1232 unloaddot(ls, v); 1244 unloaddot(ls, v);
1233 if (ls->token == ',') { /* assignment -> ',' NAME assignment */ 1245 if (ls->token == ',') { /* assignment -> ',' NAME assignment */
1234 vardesc nv; 1246 vardesc nv;