diff options
-rw-r--r-- | fpconv.c | 18 |
1 files changed, 13 insertions, 5 deletions
@@ -81,6 +81,7 @@ static int strtod_buffer_size(const char *s) | |||
81 | * character. Guaranteed to be called at the start of any valid number in a string */ | 81 | * character. Guaranteed to be called at the start of any valid number in a string */ |
82 | double fpconv_strtod(const char *nptr, char **endptr) | 82 | double fpconv_strtod(const char *nptr, char **endptr) |
83 | { | 83 | { |
84 | char localbuf[FPCONV_G_FMT_BUFSIZE]; | ||
84 | char *buf, *endbuf, *dp; | 85 | char *buf, *endbuf, *dp; |
85 | int buflen; | 86 | int buflen; |
86 | double value; | 87 | double value; |
@@ -97,10 +98,16 @@ double fpconv_strtod(const char *nptr, char **endptr) | |||
97 | } | 98 | } |
98 | 99 | ||
99 | /* Duplicate number into buffer */ | 100 | /* Duplicate number into buffer */ |
100 | buf = malloc(buflen + 1); | 101 | if (buflen >= FPCONV_G_FMT_BUFSIZE) { |
101 | if (!buf) { | 102 | /* Handle unusually large numbers */ |
102 | fprintf(stderr, "Out of memory"); | 103 | buf = malloc(buflen + 1); |
103 | abort(); | 104 | if (!buf) { |
105 | fprintf(stderr, "Out of memory"); | ||
106 | abort(); | ||
107 | } | ||
108 | } else { | ||
109 | /* This is the common case.. */ | ||
110 | buf = localbuf; | ||
104 | } | 111 | } |
105 | memcpy(buf, nptr, buflen); | 112 | memcpy(buf, nptr, buflen); |
106 | buf[buflen] = 0; | 113 | buf[buflen] = 0; |
@@ -112,7 +119,8 @@ double fpconv_strtod(const char *nptr, char **endptr) | |||
112 | 119 | ||
113 | value = strtod(buf, &endbuf); | 120 | value = strtod(buf, &endbuf); |
114 | *endptr = (char *)&nptr[endbuf - buf]; | 121 | *endptr = (char *)&nptr[endbuf - buf]; |
115 | free(buf); | 122 | if (buflen >= FPCONV_G_FMT_BUFSIZE) |
123 | free(buf); | ||
116 | 124 | ||
117 | return value; | 125 | return value; |
118 | } | 126 | } |