diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-12-06 19:08:36 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-12-06 19:08:36 -0200 |
| commit | ccc4fc9cf001c19eac5be4453b76a6c438b5b1d4 (patch) | |
| tree | 141b6ae0e4dc883cd0036d66d50fb1afbc4ab2dd | |
| parent | c79b4a97aa7734369e10f09764d5b8f05e7bda24 (diff) | |
| download | lua-ccc4fc9cf001c19eac5be4453b76a6c438b5b1d4.tar.gz lua-ccc4fc9cf001c19eac5be4453b76a6c438b5b1d4.tar.bz2 lua-ccc4fc9cf001c19eac5be4453b76a6c438b5b1d4.zip | |
detection of erroneous numeric strings with \0 (such as "1\0")
| -rw-r--r-- | llex.c | 9 | ||||
| -rw-r--r-- | lobject.c | 13 | ||||
| -rw-r--r-- | lobject.h | 4 | ||||
| -rw-r--r-- | lvm.c | 4 |
4 files changed, 17 insertions, 13 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: llex.c,v 2.40 2010/10/25 12:24:36 roberto Exp roberto $ | 2 | ** $Id: llex.c,v 2.41 2010/11/18 18:38:44 roberto Exp roberto $ |
| 3 | ** Lexical Analyzer | 3 | ** Lexical Analyzer |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -200,6 +200,9 @@ static void buffreplace (LexState *ls, char from, char to) { | |||
| 200 | #define getlocaledecpoint() (localeconv()->decimal_point[0]) | 200 | #define getlocaledecpoint() (localeconv()->decimal_point[0]) |
| 201 | #endif | 201 | #endif |
| 202 | 202 | ||
| 203 | |||
| 204 | #define buff2d(b,e) luaO_str2d(luaZ_buffer(b), luaZ_bufflen(b) - 1, e) | ||
| 205 | |||
| 203 | /* | 206 | /* |
| 204 | ** in case of format error, try to change decimal point separator to | 207 | ** in case of format error, try to change decimal point separator to |
| 205 | ** the one defined in the current locale and check again | 208 | ** the one defined in the current locale and check again |
| @@ -208,7 +211,7 @@ static void trydecpoint (LexState *ls, SemInfo *seminfo) { | |||
| 208 | char old = ls->decpoint; | 211 | char old = ls->decpoint; |
| 209 | ls->decpoint = getlocaledecpoint(); | 212 | ls->decpoint = getlocaledecpoint(); |
| 210 | buffreplace(ls, old, ls->decpoint); /* try new decimal separator */ | 213 | buffreplace(ls, old, ls->decpoint); /* try new decimal separator */ |
| 211 | if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) { | 214 | if (!buff2d(ls->buff, &seminfo->r)) { |
| 212 | /* format error with correct decimal point: no more options */ | 215 | /* format error with correct decimal point: no more options */ |
| 213 | buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ | 216 | buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ |
| 214 | lexerror(ls, "malformed number", TK_NUMBER); | 217 | lexerror(ls, "malformed number", TK_NUMBER); |
| @@ -226,7 +229,7 @@ static void read_numeral (LexState *ls, SemInfo *seminfo) { | |||
| 226 | } while (lislalnum(ls->current) || ls->current == '.'); | 229 | } while (lislalnum(ls->current) || ls->current == '.'); |
| 227 | save(ls, '\0'); | 230 | save(ls, '\0'); |
| 228 | buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ | 231 | buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ |
| 229 | if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */ | 232 | if (!buff2d(ls->buff, &seminfo->r)) /* format error? */ |
| 230 | trydecpoint(ls, seminfo); /* try to update decimal point separator */ | 233 | trydecpoint(ls, seminfo); /* try to update decimal point separator */ |
| 231 | } | 234 | } |
| 232 | 235 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.c,v 2.42 2010/10/29 11:13:14 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 2.43 2010/10/29 15:54:55 roberto Exp roberto $ |
| 3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -106,19 +106,20 @@ lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) { | |||
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | 108 | ||
| 109 | static int checkend (const char *s, const char *endptr) { | 109 | static int checkend (const char *s, const char *e, const char *endptr) { |
| 110 | if (endptr == s) return 0; /* no characters converted */ | 110 | if (endptr == s) return 0; /* no characters converted */ |
| 111 | while (lisspace(cast(unsigned char, *endptr))) endptr++; | 111 | while (lisspace(cast(unsigned char, *endptr))) endptr++; |
| 112 | return (*endptr == '\0'); /* OK if no trailing characters */ | 112 | return (endptr == e); /* OK if no trailing characters */ |
| 113 | } | 113 | } |
| 114 | 114 | ||
| 115 | 115 | ||
| 116 | int luaO_str2d (const char *s, lua_Number *result) { | 116 | int luaO_str2d (const char *s, size_t len, lua_Number *result) { |
| 117 | char *endptr; | 117 | char *endptr; |
| 118 | const char *e = s + len; /* string 's' ends here */ | ||
| 118 | *result = lua_str2number(s, &endptr); | 119 | *result = lua_str2number(s, &endptr); |
| 119 | if (checkend(s, endptr)) return 1; /* conversion OK? */ | 120 | if (checkend(s, e, endptr)) return 1; /* conversion OK? */ |
| 120 | *result = cast_num(strtoul(s, &endptr, 0)); /* try hexadecimal */ | 121 | *result = cast_num(strtoul(s, &endptr, 0)); /* try hexadecimal */ |
| 121 | return checkend(s, endptr); | 122 | return checkend(s, e, endptr); |
| 122 | } | 123 | } |
| 123 | 124 | ||
| 124 | 125 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.h,v 2.42 2010/07/26 15:53:23 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 2.43 2010/11/26 14:32:31 roberto Exp roberto $ |
| 3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -412,7 +412,7 @@ LUAI_FUNC int luaO_fb2int (int x); | |||
| 412 | LUAI_FUNC int luaO_ceillog2 (lu_int32 x); | 412 | LUAI_FUNC int luaO_ceillog2 (lu_int32 x); |
| 413 | LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2); | 413 | LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2); |
| 414 | LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2); | 414 | LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2); |
| 415 | LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result); | 415 | LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result); |
| 416 | LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, | 416 | LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, |
| 417 | va_list argp); | 417 | va_list argp); |
| 418 | LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); | 418 | LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 2.124 2010/10/25 19:01:37 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.125 2010/10/29 17:52:46 roberto Exp roberto $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -35,7 +35,7 @@ | |||
| 35 | const TValue *luaV_tonumber (const TValue *obj, TValue *n) { | 35 | const TValue *luaV_tonumber (const TValue *obj, TValue *n) { |
| 36 | lua_Number num; | 36 | lua_Number num; |
| 37 | if (ttisnumber(obj)) return obj; | 37 | if (ttisnumber(obj)) return obj; |
| 38 | if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) { | 38 | if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) { |
| 39 | setnvalue(n, num); | 39 | setnvalue(n, num); |
| 40 | return n; | 40 | return n; |
| 41 | } | 41 | } |
