lua-users home
lua-l archive

luaO_str2d, strtod and hexadecimal numbers under IRIX

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


Lua fails to parse hexadecimal numbers under IRIX (6.5.30), 
evidently caused by a buggy strtod implementation. strtod just 
doesn't advance the endpointer exactly if s starts with '0x' 
('0y' or '1x' yield the expected result), so that (endptr == s) 
holds and the conversion fails. 
The modified luaO_str2d, the example program and its output
below illustrate the problem and a potential workaround:
int luaO_str2d (const char *s, lua_Number *result) {
 char *endptr;
 *result = lua_str2number(s, &endptr);
 *result = strtod(s, &endptr);
 printf("result %li\n", *result);
 printf("endptr %s\n", endptr);
 if ((*s == '0') &&
 (*(s+1) == 'x' || *(s+1) == 'X')) /* maybe an hexadecimal constant? */
 {
 printf("hex %s\n", endptr);
 *result = cast_num(strtoul(s, &endptr, 16));
 }
 if (endptr == s) return 0; /* conversion failed */
 if (*endptr == '0円') return 1; /* most common case */
 while (isspace(cast(unsigned char, *endptr))) endptr++;
 if (*endptr != '0円') return 0; /* invalid trailing characters? */
 return 1;
}
hex.lua: 
print(0xff)
print(1xff)
Output:
result 0
endptr 0xff
hex 0xff
result 0
endptr xff
result 0
endptr xff
./lua: ../../hex.lua:2: malformed number near '1xff'

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