summaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-30 10:59:24 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-30 10:59:24 -0300
commit1aa526263405fb4906eafab3011b13de4e5daf73 (patch)
tree812defd680a2ed7a046a8377773c2a618029de54 /lstrlib.c
parent07c7fdb9df82c7fc4ae501e21262f781df4ae5e5 (diff)
downloadlua-github-1aa526263405fb4906eafab3011b13de4e5daf73.tar.gz
do not assume numbers are coercible to strings
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c11
1 files changed, 7 insertions, 4 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 5289e8b7..daf86ffb 100644
--- a/lstrlib.c
+++ b/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
** 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 */
}
}