mirror of
https://github.com/lua/lua.git
synced 2025-01-28 06:03:00 +08:00
do not assume numbers are coercible to strings
This commit is contained in:
parent
07c7fdb9df
commit
1aa5262634
11
lstrlib.c
11
lstrlib.c
@ -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
|
** Standard library for string operations and pattern-matching
|
||||||
** See Copyright Notice in lua.h
|
** 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,
|
static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
|
||||||
const char *e) {
|
const char *e) {
|
||||||
size_t l, i;
|
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++) {
|
for (i = 0; i < l; i++) {
|
||||||
if (news[i] != L_ESC)
|
if (news[i] != L_ESC)
|
||||||
luaL_addchar(b, news[i]);
|
luaL_addchar(b, news[i]);
|
||||||
@ -693,14 +694,16 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
|
|||||||
i++; /* skip ESC */
|
i++; /* skip ESC */
|
||||||
if (!isdigit(uchar(news[i]))) {
|
if (!isdigit(uchar(news[i]))) {
|
||||||
if (news[i] != L_ESC)
|
if (news[i] != L_ESC)
|
||||||
luaL_error(ms->L, "invalid use of " LUA_QL("%c")
|
luaL_error(L, "invalid use of " LUA_QL("%c")
|
||||||
" in replacement string", L_ESC);
|
" in replacement string", L_ESC);
|
||||||
luaL_addchar(b, news[i]);
|
luaL_addchar(b, news[i]);
|
||||||
}
|
}
|
||||||
else if (news[i] == '0')
|
else if (news[i] == '0')
|
||||||
luaL_addlstring(b, s, e - s);
|
luaL_addlstring(b, s, e - s);
|
||||||
else {
|
else {
|
||||||
push_onecapture(ms, news[i] - '1', s, e);
|
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 */
|
luaL_addvalue(b); /* add capture to accumulated result */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user