lua-users home
lua-l archive

tonumber() in Lua3.2.1

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


While using Lua3.2.1 for a very large (>2MB) file processing,
tonumber(" ") sometimes returns 0 instead of nil.
It was fixed by following patch:
--- org/src/lvm.c Wed Jun 23 05:37:23 1999
+++ src/lvm.c Fri Oct 6 10:17:06 2000
@@ -62,6 +62,9 @@
 sig = -1;
 }
 else if (*e == '+') e++;
+ /** avoid checking *(e+1) **/
+ if(*e==NULL)
+ return 2;
 /* no digit before or after decimal point? */
 if (!isdigit((unsigned char)*e) && !isdigit((unsigned char)*(e+1)))
 return 2;
When *e is a empty string, *(e+1) is a problem. It may not happen,
depending on C library and how the Lua program reuses string data.
Lua 4.0 beta seems to be ok.
We may needs another patch for:
Dave Bollinger <DBollinger@compuserve.com> wrote :
> Third, erroneous zero returns: tonumber() should handle the
>zero-as-error return of strtol(). The empty string is the only example
>that I'm aware of, which could be checked for in advance or check the
>endptr return value against the original string pointer for equality. 
>Example:
>
> print(tonumber("",8))
> > 0
>
> should have returned nil according to all documentation since "" is
>neither a number or convertible string.
--- org/src/lbuiltin.c Fri Jun 18 02:04:03 1999
+++ src/lbuiltin.c Fri Oct 6 11:03:17 2000
@@ -149,6 +149,12 @@
 char *s = luaL_check_string(1);
 long n;
 luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
+ /** bug fix **/
+ while (isspace((unsigned char)*s)) s++; /* skip heading spaces */
+ if(*s==NULL) {
+ lua_pushnil();
+ return;
+ }
 n = strtol(s, &s, base);
 while (isspace((unsigned char)*s)) s++; /* skip trailing spaces */
 if (*s) lua_pushnil(); /* invalid format: return nil */
---ueno

AltStyle によって変換されたページ (->オリジナル) /