aboutsummaryrefslogtreecommitdiff
path: root/ltable.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-02-07 13:39:54 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-02-07 13:39:54 -0300
commit0c9bec0d38ed3d2c45d7be4e764a0bcffef98be1 (patch)
tree05fd1ba56705dc0a1728e1bedda7961cc96414c1 /ltable.c
parentc31d6774ac7db4cfbc548ce507ae65ab6036f873 (diff)
downloadlua-0c9bec0d38ed3d2c45d7be4e764a0bcffef98be1.tar.gz
lua-0c9bec0d38ed3d2c45d7be4e764a0bcffef98be1.tar.bz2
lua-0c9bec0d38ed3d2c45d7be4e764a0bcffef98be1.zip
Better handling of size limit when resizing a table
Avoid silent conversions from int to unsigned int when calling 'luaH_resize'; avoid silent conversions from lua_Integer to int in 'table.create'; MAXASIZE corrected for the new implementation of arrays; 'luaH_resize' checks explicitly whether new size respects MAXASIZE. (Even constructors were bypassing that check.)
Diffstat (limited to 'ltable.c')
-rw-r--r--ltable.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/ltable.c b/ltable.c
index 353e567b..b86e2281 100644
--- a/ltable.c
+++ b/ltable.c
@@ -61,18 +61,25 @@ typedef union {
61 61
62 62
63/* 63/*
64** MAXABITS is the largest integer such that MAXASIZE fits in an 64** MAXABITS is the largest integer such that 2^MAXABITS fits in an
65** unsigned int. 65** unsigned int.
66*/ 66*/
67#define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1) 67#define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1)
68 68
69 69
70/* 70/*
71** MAXASIZEB is the maximum number of elements in the array part such
72** that the size of the array fits in 'size_t'.
73*/
74#define MAXASIZEB ((MAX_SIZET/sizeof(ArrayCell)) * NM)
75
76
77/*
71** MAXASIZE is the maximum size of the array part. It is the minimum 78** MAXASIZE is the maximum size of the array part. It is the minimum
72** between 2^MAXABITS and the maximum size that, measured in bytes, 79** between 2^MAXABITS and MAXASIZEB.
73** fits in a 'size_t'.
74*/ 80*/
75#define MAXASIZE luaM_limitN(1u << MAXABITS, TValue) 81#define MAXASIZE \
82 (((1u << MAXABITS) < MAXASIZEB) ? (1u << MAXABITS) : cast_uint(MAXASIZEB))
76 83
77/* 84/*
78** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a 85** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a
@@ -663,6 +670,8 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize,
663 Table newt; /* to keep the new hash part */ 670 Table newt; /* to keep the new hash part */
664 unsigned int oldasize = setlimittosize(t); 671 unsigned int oldasize = setlimittosize(t);
665 ArrayCell *newarray; 672 ArrayCell *newarray;
673 if (newasize > MAXASIZE)
674 luaG_runerror(L, "table overflow");
666 /* create new hash part with appropriate size into 'newt' */ 675 /* create new hash part with appropriate size into 'newt' */
667 newt.flags = 0; 676 newt.flags = 0;
668 setnodevector(L, &newt, nhsize); 677 setnodevector(L, &newt, nhsize);