do not assume numbers are coercible to strings

This commit is contained in:
Roberto Ierusalimschy 2014-07-30 10:59:24 -03:00
parent 07c7fdb9df
commit 1aa5262634
1 changed files with 7 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: lstrlib.c,v 1.198 2014/04/27 14:42:26 roberto Exp roberto $
** $Id: lstrlib.c,v 1.199 2014/07/29 16:01:00 roberto Exp roberto $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
@ -685,7 +685,8 @@ static int gmatch (lua_State *L) {
static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
const char *e) {
size_t l, i;
const char *news = lua_tolstring(ms->L, 3, &l);
lua_State *L = ms->L;
const char *news = lua_tolstring(L, 3, &l);
for (i = 0; i < l; i++) {
if (news[i] != L_ESC)
luaL_addchar(b, news[i]);
@ -693,14 +694,16 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
i++; /* skip ESC */
if (!isdigit(uchar(news[i]))) {
if (news[i] != L_ESC)
luaL_error(ms->L, "invalid use of " LUA_QL("%c")
" in replacement string", L_ESC);
luaL_error(L, "invalid use of " LUA_QL("%c")
" in replacement string", L_ESC);
luaL_addchar(b, news[i]);
}
else if (news[i] == '0')
luaL_addlstring(b, s, e - s);
else {
push_onecapture(ms, news[i] - '1', s, e);
luaL_tolstring(L, -1, NULL); /* if number, convert it to string */
lua_remove(L, -2); /* remove original value */
luaL_addvalue(b); /* add capture to accumulated result */
}
}