Skip to content

Commit 72373f8

Browse files
authored
Update struct.c
1 parent c8faff2 commit 72373f8

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

app/modules/struct.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,16 @@ static int b_unpack (lua_State *L) {
309309
const char *fmt = luaL_checkstring(L, 1);
310310
size_t ld;
311311
const char *data = luaL_checklstring(L, 2, &ld);
312-
size_t pos = luaL_optinteger(L, 3, 1) - 1;
312+
size_t pos = luaL_optinteger(L, 3, 1);
313+
luaL_argcheck(L, pos > 0, 3, "offset must be 1 or greater");
314+
pos--;
313315
defaultoptions(&h);
314316
lua_settop(L, 2);
315317
while (*fmt) {
316318
int opt = *fmt++;
317319
size_t size = optsize(L, opt, &fmt);
318320
pos += gettoalign(pos, &h, opt, size);
319-
luaL_argcheck(L, pos+size <= ld, 2, "data string too short");
321+
luaL_argcheck(L, size <= ld && pos <= ld - size, 2, "data string too short");
320322
luaL_checkstack(L, 2, "too many results");
321323
switch (opt) {
322324
case 'b': case 'B': case 'h': case 'H':

0 commit comments

Comments
 (0)