diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-07-30 10:59:24 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-07-30 10:59:24 -0300 |
commit | 1aa526263405fb4906eafab3011b13de4e5daf73 (patch) | |
tree | 812defd680a2ed7a046a8377773c2a618029de54 /lstrlib.c | |
parent | 07c7fdb9df82c7fc4ae501e21262f781df4ae5e5 (diff) | |
download | lua-1aa526263405fb4906eafab3011b13de4e5daf73.tar.gz lua-1aa526263405fb4906eafab3011b13de4e5daf73.tar.bz2 lua-1aa526263405fb4906eafab3011b13de4e5daf73.zip |
do not assume numbers are coercible to strings
Diffstat (limited to 'lstrlib.c')
-rw-r--r-- | lstrlib.c | 11 |
1 files changed, 7 insertions, 4 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.198 2014/04/27 14:42:26 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.199 2014/07/29 16:01:00 roberto Exp roberto $ |
3 | ** Standard library for string operations and pattern-matching | 3 | ** Standard library for string operations and pattern-matching |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -685,7 +685,8 @@ static int gmatch (lua_State *L) { | |||
685 | static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, | 685 | static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, |
686 | const char *e) { | 686 | const char *e) { |
687 | size_t l, i; | 687 | size_t l, i; |
688 | const char *news = lua_tolstring(ms->L, 3, &l); | 688 | lua_State *L = ms->L; |
689 | const char *news = lua_tolstring(L, 3, &l); | ||
689 | for (i = 0; i < l; i++) { | 690 | for (i = 0; i < l; i++) { |
690 | if (news[i] != L_ESC) | 691 | if (news[i] != L_ESC) |
691 | luaL_addchar(b, news[i]); | 692 | luaL_addchar(b, news[i]); |
@@ -693,14 +694,16 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, | |||
693 | i++; /* skip ESC */ | 694 | i++; /* skip ESC */ |
694 | if (!isdigit(uchar(news[i]))) { | 695 | if (!isdigit(uchar(news[i]))) { |
695 | if (news[i] != L_ESC) | 696 | if (news[i] != L_ESC) |
696 | luaL_error(ms->L, "invalid use of " LUA_QL("%c") | 697 | luaL_error(L, "invalid use of " LUA_QL("%c") |
697 | " in replacement string", L_ESC); | 698 | " in replacement string", L_ESC); |
698 | luaL_addchar(b, news[i]); | 699 | luaL_addchar(b, news[i]); |
699 | } | 700 | } |
700 | else if (news[i] == '0') | 701 | else if (news[i] == '0') |
701 | luaL_addlstring(b, s, e - s); | 702 | luaL_addlstring(b, s, e - s); |
702 | else { | 703 | else { |
703 | push_onecapture(ms, news[i] - '1', s, e); | 704 | push_onecapture(ms, news[i] - '1', s, e); |
705 | luaL_tolstring(L, -1, NULL); /* if number, convert it to string */ | ||
706 | lua_remove(L, -2); /* remove original value */ | ||
704 | luaL_addvalue(b); /* add capture to accumulated result */ | 707 | luaL_addvalue(b); /* add capture to accumulated result */ |
705 | } | 708 | } |
706 | } | 709 | } |