lua

A copy of the Lua development repository
Log | Files | Refs | README

commit 9e6aa878c96485ba0658303c0da16adef56ba54c
parent c0fdaf58420a4c224f7d0167465b40b7bf91e934
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Mon,  9 Aug 2004 10:30:11 -0300

string.byte truncates indices out of range

Diffstat:
Mlstrlib.c | 7++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lstrlib.c b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.104 2004/07/09 18:24:41 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.105 2004/08/06 17:35:38 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -110,8 +110,9 @@ static int str_byte (lua_State *L) { sint32 posi = posrelat(luaL_optinteger(L, 2, 1), l); sint32 pose = posrelat(luaL_optinteger(L, 3, posi), l); int n, i; - if (!(0 < posi && posi <= pose && (size_t)pose <= l)) - return 0; /* index out of range; no answer */ + if (posi <= 0) posi = 1; + if ((size_t)pose > l) pose = l; + if (posi > pose) return 0; /* empty interval; return no values */ n = pose - posi + 1; luaL_checkstack(L, n, "string slice too long"); for (i=0; i<n; i++)