Skip to content

Commit

Permalink
Length of external strings must fit in Lua integer
Browse files Browse the repository at this point in the history
(As the length of any string in Lua.)
  • Loading branch information
roberto-ieru committed Jun 24, 2024
1 parent 0f7025d commit c1dc08e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions lapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ LUA_API const char *lua_pushextlstring (lua_State *L,
const char *s, size_t len, lua_Alloc falloc, void *ud) {
TString *ts;
lua_lock(L);
api_check(L, len <= MAX_SIZE, "string too large");
api_check(L, s[len] == '\0', "string not ending with zero");
ts = luaS_newextlstr (L, s, len, falloc, ud);
setsvalue2s(L, L->top.p, ts);
Expand Down
8 changes: 5 additions & 3 deletions lauxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,12 @@ static void newbox (lua_State *L) {
*/
static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */
if (l_unlikely(MAX_SIZET - sz - 1 < B->n)) /* overflow in (B->n + sz + 1)? */
return luaL_error(B->L, "buffer too large");
if (newsize < B->n + sz + 1) /* not big enough? */
if (l_unlikely(sz > MAX_SIZE - B->n - 1))
return luaL_error(B->L, "resulting string too large");
if (newsize < B->n + sz + 1 || newsize > MAX_SIZE) {
/* newsize was not big enough or too big */
newsize = B->n + sz + 1;
}
return newsize;
}

Expand Down
2 changes: 1 addition & 1 deletion lundump.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static size_t loadVarint (LoadState *S, size_t limit) {


static size_t loadSize (LoadState *S) {
return loadVarint(S, MAX_SIZET);
return loadVarint(S, MAX_SIZE);
}


Expand Down
2 changes: 2 additions & 0 deletions manual/manual.of
Original file line number Diff line number Diff line change
Expand Up @@ -3942,6 +3942,8 @@ holding the string content,
and @id{len} is the length of the string.
The string should have a zero at its end,
that is, the condition @T{s[len] == '\0'} should hold.
As with any string in Lua,
the length must fit in a Lua integer.

If @id{falloc} is different from @id{NULL},
that function will be called by Lua
Expand Down

0 comments on commit c1dc08e

Please sign in to comment.