aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-02-12 13:42:44 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-02-12 13:42:44 -0200
commit255052b6c6a1b109c93b104c03de8968b56dcd5a (patch)
treec5142cc2af37efa4d3f8352995a31055381450c2 /lapi.c
parent1bdc156b52f51c7c353a5cd50447c827de853005 (diff)
downloadlua-255052b6c6a1b109c93b104c03de8968b56dcd5a.tar.gz
lua-255052b6c6a1b109c93b104c03de8968b56dcd5a.tar.bz2
lua-255052b6c6a1b109c93b104c03de8968b56dcd5a.zip
better API checks
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c80
1 files changed, 59 insertions, 21 deletions
diff --git a/lapi.c b/lapi.c
index c0c9f234..65c7a726 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.126 2001/02/07 18:13:49 roberto Exp roberto $ 2** $Id: lapi.c,v 1.127 2001/02/09 19:53:16 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -27,6 +27,18 @@ const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
27 27
28 28
29 29
30#ifndef api_check
31#define api_check(L, o) /* nothing */
32#endif
33
34/* valid indices */
35#define api_checkindex(L, i) \
36 api_check(L, (0 < abs(i)) && abs(i) <= (L->top - L->Cbase))
37
38#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->Cbase))
39
40
41
30#define Index(L,i) ((i) >= 0 ? (L->Cbase+((i)-1)) : (L->top+(i))) 42#define Index(L,i) ((i) >= 0 ? (L->Cbase+((i)-1)) : (L->top+(i)))
31 43
32#define api_incr_top(L) incr_top 44#define api_incr_top(L) incr_top
@@ -39,6 +51,7 @@ TObject *luaA_index (lua_State *L, int index) {
39 51
40 52
41static TObject *luaA_indexAcceptable (lua_State *L, int index) { 53static TObject *luaA_indexAcceptable (lua_State *L, int index) {
54 api_check(L, (0 < abs(index)) && abs(index) <= (L->stack_last - L->Cbase));
42 if (index >= 0) { 55 if (index >= 0) {
43 TObject *o = L->Cbase+(index-1); 56 TObject *o = L->Cbase+(index-1);
44 if (o >= L->top) return NULL; 57 if (o >= L->top) return NULL;
@@ -81,8 +94,10 @@ LUA_API void lua_settop (lua_State *L, int index) {
81 LUA_LOCK(L); 94 LUA_LOCK(L);
82 if (index >= 0) 95 if (index >= 0)
83 luaD_adjusttop(L, L->Cbase, index); 96 luaD_adjusttop(L, L->Cbase, index);
84 else 97 else {
98 api_check(L, -(index+1) <= (L->top - L->Cbase));
85 L->top = L->top+index+1; /* index is negative */ 99 L->top = L->top+index+1; /* index is negative */
100 }
86 LUA_UNLOCK(L); 101 LUA_UNLOCK(L);
87} 102}
88 103
@@ -90,7 +105,8 @@ LUA_API void lua_settop (lua_State *L, int index) {
90LUA_API void lua_remove (lua_State *L, int index) { 105LUA_API void lua_remove (lua_State *L, int index) {
91 StkId p; 106 StkId p;
92 LUA_LOCK(L); 107 LUA_LOCK(L);
93 p = luaA_index(L, index); 108 api_checkindex(L, index);
109 p = Index(L, index);
94 while (++p < L->top) setobj(p-1, p); 110 while (++p < L->top) setobj(p-1, p);
95 L->top--; 111 L->top--;
96 LUA_UNLOCK(L); 112 LUA_UNLOCK(L);
@@ -101,7 +117,8 @@ LUA_API void lua_insert (lua_State *L, int index) {
101 StkId p; 117 StkId p;
102 StkId q; 118 StkId q;
103 LUA_LOCK(L); 119 LUA_LOCK(L);
104 p = luaA_index(L, index); 120 api_checkindex(L, index);
121 p = Index(L, index);
105 for (q = L->top; q>p; q--) setobj(q, q-1); 122 for (q = L->top; q>p; q--) setobj(q, q-1);
106 setobj(p, L->top); 123 setobj(p, L->top);
107 LUA_UNLOCK(L); 124 LUA_UNLOCK(L);
@@ -110,7 +127,8 @@ LUA_API void lua_insert (lua_State *L, int index) {
110 127
111LUA_API void lua_pushvalue (lua_State *L, int index) { 128LUA_API void lua_pushvalue (lua_State *L, int index) {
112 LUA_LOCK(L); 129 LUA_LOCK(L);
113 setobj(L->top, luaA_index(L, index)); 130 api_checkindex(L, index);
131 setobj(L->top, Index(L, index));
114 api_incr_top(L); 132 api_incr_top(L);
115 LUA_UNLOCK(L); 133 LUA_UNLOCK(L);
116} 134}
@@ -330,6 +348,7 @@ LUA_API void lua_pushstring (lua_State *L, const char *s) {
330 348
331LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { 349LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
332 LUA_LOCK(L); 350 LUA_LOCK(L);
351 api_checknelems(L, n);
333 luaV_Cclosure(L, fn, n); 352 luaV_Cclosure(L, fn, n);
334 LUA_UNLOCK(L); 353 LUA_UNLOCK(L);
335} 354}
@@ -362,6 +381,7 @@ LUA_API void lua_getglobal (lua_State *L, const char *name) {
362LUA_API void lua_gettable (lua_State *L, int index) { 381LUA_API void lua_gettable (lua_State *L, int index) {
363 StkId t; 382 StkId t;
364 LUA_LOCK(L); 383 LUA_LOCK(L);
384 api_checkindex(L, index);
365 t = Index(L, index); 385 t = Index(L, index);
366 luaV_gettable(L, t, L->top-1, L->top-1); 386 luaV_gettable(L, t, L->top-1, L->top-1);
367 LUA_UNLOCK(L); 387 LUA_UNLOCK(L);
@@ -371,8 +391,9 @@ LUA_API void lua_gettable (lua_State *L, int index) {
371LUA_API void lua_rawget (lua_State *L, int index) { 391LUA_API void lua_rawget (lua_State *L, int index) {
372 StkId t; 392 StkId t;
373 LUA_LOCK(L); 393 LUA_LOCK(L);
394 api_checkindex(L, index);
374 t = Index(L, index); 395 t = Index(L, index);
375 lua_assert(ttype(t) == LUA_TTABLE); 396 api_check(L, ttype(t) == LUA_TTABLE);
376 setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1)); 397 setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
377 LUA_UNLOCK(L); 398 LUA_UNLOCK(L);
378} 399}
@@ -381,8 +402,9 @@ LUA_API void lua_rawget (lua_State *L, int index) {
381LUA_API void lua_rawgeti (lua_State *L, int index, int n) { 402LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
382 StkId o; 403 StkId o;
383 LUA_LOCK(L); 404 LUA_LOCK(L);
405 api_checkindex(L, index);
384 o = Index(L, index); 406 o = Index(L, index);
385 lua_assert(ttype(o) == LUA_TTABLE); 407 api_check(L, ttype(o) == LUA_TTABLE);
386 setobj(L->top, luaH_getnum(hvalue(o), n)); 408 setobj(L->top, luaH_getnum(hvalue(o), n));
387 api_incr_top(L); 409 api_incr_top(L);
388 LUA_UNLOCK(L); 410 LUA_UNLOCK(L);
@@ -404,13 +426,15 @@ LUA_API int lua_getref (lua_State *L, int ref) {
404 setnilvalue(L->top); 426 setnilvalue(L->top);
405 api_incr_top(L); 427 api_incr_top(L);
406 } 428 }
407 else if (0 <= ref && ref < G(L)->nref && 429 else {
408 (G(L)->refArray[ref].st == LOCK || G(L)->refArray[ref].st == HOLD)) { 430 api_check(L, 0 <= ref && ref < G(L)->nref);
409 setobj(L->top, &G(L)->refArray[ref].o); 431 if (G(L)->refArray[ref].st != LOCK && G(L)->refArray[ref].st != HOLD)
410 api_incr_top(L); 432 status = 0;
433 else {
434 setobj(L->top, &G(L)->refArray[ref].o);
435 api_incr_top(L);
436 }
411 } 437 }
412 else
413 status = 0;
414 LUA_UNLOCK(L); 438 LUA_UNLOCK(L);
415 return status; 439 return status;
416} 440}
@@ -432,6 +456,7 @@ LUA_API void lua_newtable (lua_State *L) {
432 456
433LUA_API void lua_setglobal (lua_State *L, const char *name) { 457LUA_API void lua_setglobal (lua_State *L, const char *name) {
434 LUA_LOCK(L); 458 LUA_LOCK(L);
459 api_checknelems(L, 1);
435 luaV_setglobal(L, luaS_new(L, name), L->top - 1); 460 luaV_setglobal(L, luaS_new(L, name), L->top - 1);
436 L->top--; /* remove element from the top */ 461 L->top--; /* remove element from the top */
437 LUA_UNLOCK(L); 462 LUA_UNLOCK(L);
@@ -441,6 +466,8 @@ LUA_API void lua_setglobal (lua_State *L, const char *name) {
441LUA_API void lua_settable (lua_State *L, int index) { 466LUA_API void lua_settable (lua_State *L, int index) {
442 StkId t; 467 StkId t;
443 LUA_LOCK(L); 468 LUA_LOCK(L);
469 api_checknelems(L, 2);
470 api_checkindex(L, index);
444 t = Index(L, index); 471 t = Index(L, index);
445 luaV_settable(L, t, L->top - 2, L->top - 1); 472 luaV_settable(L, t, L->top - 2, L->top - 1);
446 L->top -= 2; /* pop index and value */ 473 L->top -= 2; /* pop index and value */
@@ -451,8 +478,10 @@ LUA_API void lua_settable (lua_State *L, int index) {
451LUA_API void lua_rawset (lua_State *L, int index) { 478LUA_API void lua_rawset (lua_State *L, int index) {
452 StkId t; 479 StkId t;
453 LUA_LOCK(L); 480 LUA_LOCK(L);
481 api_checknelems(L, 2);
482 api_checkindex(L, index);
454 t = Index(L, index); 483 t = Index(L, index);
455 lua_assert(ttype(t) == LUA_TTABLE); 484 api_check(L, ttype(t) == LUA_TTABLE);
456 setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1)); 485 setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1));
457 L->top -= 2; 486 L->top -= 2;
458 LUA_UNLOCK(L); 487 LUA_UNLOCK(L);
@@ -462,8 +491,10 @@ LUA_API void lua_rawset (lua_State *L, int index) {
462LUA_API void lua_rawseti (lua_State *L, int index, int n) { 491LUA_API void lua_rawseti (lua_State *L, int index, int n) {
463 StkId o; 492 StkId o;
464 LUA_LOCK(L); 493 LUA_LOCK(L);
494 api_checknelems(L, 1);
495 api_checkindex(L, index);
465 o = Index(L, index); 496 o = Index(L, index);
466 lua_assert(ttype(o) == LUA_TTABLE); 497 api_check(L, ttype(o) == LUA_TTABLE);
467 setobj(luaH_setnum(L, hvalue(o), n), (L->top-1)); 498 setobj(luaH_setnum(L, hvalue(o), n), (L->top-1));
468 L->top--; 499 L->top--;
469 LUA_UNLOCK(L); 500 LUA_UNLOCK(L);
@@ -473,8 +504,9 @@ LUA_API void lua_rawseti (lua_State *L, int index, int n) {
473LUA_API void lua_setglobals (lua_State *L) { 504LUA_API void lua_setglobals (lua_State *L) {
474 StkId newtable; 505 StkId newtable;
475 LUA_LOCK(L); 506 LUA_LOCK(L);
507 api_checknelems(L, 1);
476 newtable = --L->top; 508 newtable = --L->top;
477 lua_assert(ttype(newtable) == LUA_TTABLE); 509 api_check(L, ttype(newtable) == LUA_TTABLE);
478 L->gt = hvalue(newtable); 510 L->gt = hvalue(newtable);
479 LUA_UNLOCK(L); 511 LUA_UNLOCK(L);
480} 512}
@@ -483,6 +515,7 @@ LUA_API void lua_setglobals (lua_State *L) {
483LUA_API int lua_ref (lua_State *L, int lock) { 515LUA_API int lua_ref (lua_State *L, int lock) {
484 int ref; 516 int ref;
485 LUA_LOCK(L); 517 LUA_LOCK(L);
518 api_checknelems(L, 1);
486 if (ttype(L->top-1) == LUA_TNIL) 519 if (ttype(L->top-1) == LUA_TNIL)
487 ref = LUA_REFNIL; 520 ref = LUA_REFNIL;
488 else { 521 else {
@@ -511,6 +544,7 @@ LUA_API int lua_ref (lua_State *L, int lock) {
511 544
512LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) { 545LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
513 LUA_LOCK(L); 546 LUA_LOCK(L);
547 api_checknelems(L, nargs+1);
514 luaD_call(L, L->top-(nargs+1), nresults); 548 luaD_call(L, L->top-(nargs+1), nresults);
515 LUA_UNLOCK(L); 549 LUA_UNLOCK(L);
516} 550}
@@ -589,6 +623,7 @@ LUA_API int lua_type2tag (lua_State *L, const char *name) {
589LUA_API void lua_settag (lua_State *L, int tag) { 623LUA_API void lua_settag (lua_State *L, int tag) {
590 int basictype; 624 int basictype;
591 LUA_LOCK(L); 625 LUA_LOCK(L);
626 api_checknelems(L, 1);
592 if (tag < 0 || tag >= G(L)->ntag) 627 if (tag < 0 || tag >= G(L)->ntag)
593 luaO_verror(L, "%d is not a valid tag", tag); 628 luaO_verror(L, "%d is not a valid tag", tag);
594 basictype = G(L)->TMtable[tag].basictype; 629 basictype = G(L)->TMtable[tag].basictype;
@@ -620,7 +655,7 @@ LUA_API void lua_error (lua_State *L, const char *s) {
620LUA_API void lua_unref (lua_State *L, int ref) { 655LUA_API void lua_unref (lua_State *L, int ref) {
621 LUA_LOCK(L); 656 LUA_LOCK(L);
622 if (ref >= 0) { 657 if (ref >= 0) {
623 lua_assert(ref < G(L)->nref && G(L)->refArray[ref].st < 0); 658 api_check(L, ref < G(L)->nref && G(L)->refArray[ref].st < 0);
624 G(L)->refArray[ref].st = G(L)->refFree; 659 G(L)->refArray[ref].st = G(L)->refFree;
625 G(L)->refFree = ref; 660 G(L)->refFree = ref;
626 } 661 }
@@ -633,9 +668,10 @@ LUA_API int lua_next (lua_State *L, int index) {
633 Node *n; 668 Node *n;
634 int more; 669 int more;
635 LUA_LOCK(L); 670 LUA_LOCK(L);
636 t = luaA_index(L, index); 671 api_checkindex(L, index);
637 lua_assert(ttype(t) == LUA_TTABLE); 672 t = Index(L, index);
638 n = luaH_next(L, hvalue(t), luaA_index(L, -1)); 673 api_check(L, ttype(t) == LUA_TTABLE);
674 n = luaH_next(L, hvalue(t), Index(L, -1));
639 if (n) { 675 if (n) {
640 setkey2obj(L->top-1, n); 676 setkey2obj(L->top-1, n);
641 setobj(L->top, val(n)); 677 setobj(L->top, val(n));
@@ -656,7 +692,8 @@ LUA_API int lua_getn (lua_State *L, int index) {
656 const TObject *value; 692 const TObject *value;
657 int n; 693 int n;
658 LUA_LOCK(L); 694 LUA_LOCK(L);
659 h = hvalue(luaA_index(L, index)); 695 api_checkindex(L, index);
696 h = hvalue(Index(L, index));
660 value = luaH_getstr(h, luaS_newliteral(L, "n")); /* = h.n */ 697 value = luaH_getstr(h, luaS_newliteral(L, "n")); /* = h.n */
661 if (ttype(value) == LUA_TNUMBER) 698 if (ttype(value) == LUA_TNUMBER)
662 n = (int)nvalue(value); 699 n = (int)nvalue(value);
@@ -681,6 +718,7 @@ LUA_API int lua_getn (lua_State *L, int index) {
681LUA_API void lua_concat (lua_State *L, int n) { 718LUA_API void lua_concat (lua_State *L, int n) {
682 StkId top; 719 StkId top;
683 LUA_LOCK(L); 720 LUA_LOCK(L);
721 api_checknelems(L, n);
684 top = L->top; 722 top = L->top;
685 luaV_strconc(L, n, top); 723 luaV_strconc(L, n, top);
686 L->top = top-(n-1); 724 L->top = top-(n-1);