From 94043a3a1a3108a9ce52dd135b7847f5e72bd51d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 28 Oct 2010 13:39:03 -0200 Subject: [PATCH] more robust implementation for 'luaO_str2d' --- lobject.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lobject.c b/lobject.c index 5cc4c58a..67039640 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.39 2010/04/15 19:44:43 roberto Exp roberto $ +** $Id: lobject.c,v 2.40 2010/04/18 13:22:48 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -106,14 +106,19 @@ lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) { } +static int checkend (const char *s, const char *endptr) { + if (endptr == s) return 0; /* no characters converted */ + while (lisspace(cast(unsigned char, *endptr))) endptr++; + return (*endptr == '\0'); /* OK if no trailing characters */ +} + + int luaO_str2d (const char *s, lua_Number *result) { char *endptr; *result = lua_str2number(s, &endptr); - if (endptr == s) return 0; /* conversion failed */ - if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */ - *result = cast_num(strtoul(s, &endptr, 16)); - while (lisspace(cast(unsigned char, *endptr))) endptr++; - return (*endptr == '\0'); /* OK if no trailing characters */ + if (checkend(s, endptr)) return 1; /* convertion OK? */ + *result = cast_num(strtoul(s, &endptr, 0)); /* try hexadecimal */ + return checkend(s, endptr); }