Some changes the vendor didn't want:

- Accept the '0x' prefix so strtod("nan(0x...)", NULL) returns the same
  thing as gcc's builtin nan("0x...") for such strings.
- Don't return uninitialized memory.
- Finish processing the string up to the closing ')' (provided it's
  lexically valid) for compatibility with C99 and *scanf().
This commit is contained in:
David Schultz 2007-12-16 21:13:54 +00:00
parent de272a0a53
commit 2ca7a12a81

View File

@ -29,6 +29,8 @@ THIS SOFTWARE.
/* Please send bug reports to David M. Gay (dmg at acm dot org,
* with " at " changed at "@" and " dot " changed to "."). */
/* $FreeBSD$ */
#include "gdtoaimp.h"
static void
@ -71,8 +73,14 @@ hexnan( CONST char **sp, FPI *fpi, ULong *x0)
x1 = xe = x;
havedig = hd0 = i = 0;
s = *sp;
/* FreeBSD local: Accept (but ignore) the '0x' prefix. */
if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X'))
s += 2;
while(c = *(CONST unsigned char*)++s) {
if (!(h = hexdig[c])) {
#if 0
if (c <= ' ') {
if (hd0 < havedig) {
if (x < x1 && i < 8)
@ -92,7 +100,8 @@ hexnan( CONST char **sp, FPI *fpi, ULong *x0)
*sp = s + 1;
break;
}
return STRTOG_NaN;
#endif
break;
}
havedig++;
if (++i > 8) {
@ -103,9 +112,7 @@ hexnan( CONST char **sp, FPI *fpi, ULong *x0)
}
*x = (*x << 4) | h & 0xf;
}
if (!havedig)
return STRTOG_NaN;
if (x < x1 && i < 8)
if (havedig && x < x1 && i < 8)
L_shift(x, x1, i);
if (x > x0) {
x1 = x0;
@ -119,6 +126,7 @@ hexnan( CONST char **sp, FPI *fpi, ULong *x0)
if ( (i = nbits & (ULbits-1)) !=0)
*xe &= ((ULong)0xffffffff) >> (ULbits - i);
}
if (havedig) {
for(x1 = xe;; --x1) {
if (*x1 != 0)
break;
@ -127,5 +135,22 @@ hexnan( CONST char **sp, FPI *fpi, ULong *x0)
break;
}
}
}
/*
* FreeBSD local: Accept all the sequences allowed by C99 and update
* the tail pointer correctly. Don't accept any invalid sequences.
*/
if (c == '\0') /* nan() calls this, too; tolerate a missing ')' */
return STRTOG_NaNbits;
if (c != ')') {
while(c = *(CONST unsigned char*)++s) {
if (c == ')')
break;
if (!isalnum(c) && c != '_')
return STRTOG_NaNbits;
}
}
*sp = s + 1;
return STRTOG_NaNbits;
}