No portable way to detect string-to-float conversion errors in standard C
Recorded: Oct. 12, 2026, 4:10 a.m.
| Original | Summarized |
oh, apparently it's not possible to portably check for string-to-float conversion errors in standard c sebsite oh, apparently it's not possible to portably check for string-to-float conversion errors in standard c this is kinda a sequel-ish to my previous post, in which i go over the math_errhandling macro, and how math error handling is done in glibc and musl (as well as how it's specified in the standard). to summarize: math_errhandling is a macro which indicates which error-handling mechanisms are supported by math.h functions: errno (MATH_ERRNO) and/or floating-point exceptions (MATH_ERREXCEPT). there's one thing i didn't mention in my previous post: there's one family of functions which is affected by math_errhandling but which isn't in math.h: the string to float conversion functions strtod, strtof, strtold, strtod32, strtod64, and strtod128: If the correct value overflows and default rounding is in effect (7.12.2), plus or minus HUGE_VAL, HUGE_VALF, or HUGE_VALL is returned (according to the return type and sign of the value); if the integer expression math_errhandling & MATH_ERRNO is nonzero, the integer expression errno acquires the value of ERANGE; if the integer expression math_errhandling & MATH_ERREXCEPT is nonzero, the "overflow" floating-point exception is raised. If the result underflows (7.12.2), the functions return a value whose magnitude is no greater than the smallest normalized positive number in the return type; if the integer expression math_errhandling & MATH_ERRNO is nonzero, whether errno acquires the value ERANGE is implementation-defined; if the integer expression math_errhandling & MATH_ERREXCEPT is nonzero, whether the "underflow" floating-point exception is raised is implementation-defined. the linux man pages for these functions literally don't mention this at all, and there actually is a reason why which i'll get to in a sec, but what the standard is saying is, if math_errhandling doesn't advertise support for errno (as is the case on musl, for instance), the string to float conversion functions don't set errno on error. furthermore, if the result underflows, the function isn't required to report an error at all. keep in mind that the return value alone isn't enough to determine if an error occurred, so to test for overflow, you need to use one of the two error handling mechanisms. here is my attempt at a standards-blessed way of portably checking for overflow/underflow errors in the string to float functions: feclearexcept(FE_OVERFLOW | FE_UNDERFLOW); the reason you've never done this ever (and the reason the man pages don't mention this) is that posix specifies the functions differently: If the correct value would cause an underflow, a value whose magnitude is no greater than the smallest normalized positive number in the return type shall be returned and errno set to [ERANGE]. whether or not posix's behavior is even compatible with standard c is... unclear. at least for math.h functions, setting errno regardless of the value of math_errhandling is permitted: If a domain, pole, or range error occurs and the integer expression math_errhandling & MATH_ERRNO is zero, then errno shall either be set to the value corresponding to the error or left unmodified. but earlier on, when specifying errno.h, the standard says this: [...] The value of errno may be set to nonzero by a library function call whether or not there is an error, provided the use of errno is not documented in the description of the function in this document. errno is documented in the description for these functions, and that description makes no mention of setting errno when math_errhandling & MATH_ERRNO is zero. so that suggests that posix's behavior is non-conformant. but hang on! everything i've talked about so far is only for overflow and underflow. but if the string is malformed and can't be parsed as a number, then the standard doesn't specify any error at all: The functions return the converted value, if any. If no conversion could be performed, positive or unsigned zero is returned. instead, you're supposed to use the endptr parameter, and check afterward if endptr == nptr (i.e. the end pointer is the same as the start pointer, so no data was parsed): If the subject sequence is empty or does not have the expected form, no conversion is performed; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a null pointer. the man page strtod(3) says something similar: errno = 0; errno = 0; it's also, from my reading of the standard, not conformant to standard c, because it's setting errno to a nonzero value in a function with other documented error conditions (and as far as standard c is concerned, invalid input isn't an error condition). musl could defend itself by saying that, because it doesn't set errno in its math.h functions, the errno condition in strtod's description no longer applies, therefore there's no documented use for errno (since the use of errno is dependent on the value of math_errhandling). but that's clearly a stretch. either way, it's clear to me that the standard needs clearer wording here. conclusion overflow if you're targeting posix, set errno to 0 before the call, and afterward if you're targeting just one implementation, and you know which error-reporting methods it supports beforehand, you can skip the math_errhandling check and only support one of the two methods. if you're using feclearexcept/fetestexcept, make sure that the compiler knows you may be accessing the floating point environment: on gcc the relevant flag is -ftrapping-math, which is the default (unless you're using -ffast-math). there's also a standard pragma for this purpose: #pragma STDC FENV_ACCESS ON. this pragma isn't supported by gcc though. underflow if you're targeting posix, set errno to 0 before the call, and afterward check result == 0.0 && errno == ERANGE. be sure to check specifically that errno is ERANGE, not just that it's nonzero. otherwise you'll end up with nonportable behavior. but if it is documented, and it reports underflow errors, then, depending on the implementation's value of math_errhandling, either set errno to 0 before the call and check result == 0.0 && errno == ERANGE after the call, or call feclearexcept(FE_UNDERFLOW) before the call and check fetestexcept(FE_UNDERFLOW) after the call. if you reeally need to check for underflow, and you need to use the libc function for some reason, here's the only option i can think of: check if result == 0.0, and if so, check yourself if the input string contains any nonzero digits before the exponent. if so, underflow occurred. this is difficult to get right though; read the description of strtod et al from the standard and make sure you cover all edge-cases. invalid input (no conversion) pass in &endptr as a second argument to the function, and then check if endptr == nptr. errno can't be relied on. |
The handling of mathematical errors in the C standard is managed through the macro math_errhandling, which signals whether functions in math.h support error reporting via errno (MATH_ERRNO) and floating-point exceptions (MATH_ERREXCEPT). This mechanism extends beyond functions declared in math.h to affect string-to-float conversion functions such as strtod, strtof, strtold, strtod32, strtod64, and strtod128. The standard specifies behavior for overflow and underflow during these conversions. If a correct value overflows, or if default rounding is applied in conjunction with the values HUGE_VAL, HUGE_VALF, or HUGE_VALL, the functions return these special values according to the return type and sign. Furthermore, if the integer expression math_errhandling and MATH_ERRNO is non-zero, errno is set to ERANGE. Similarly, if math_errhandling and MATH_ERREXCEPT are non-zero, the floating-point exception is raised. Underflow results in the function returning a value whose magnitude is no greater than the smallest normalized positive number in the return type. For underflow, the setting of errno to ERANGE is implementation-defined, and the raising of the underflow exception is also implementation-defined. A significant point of divergence exists between the abstract standard and the implementations found in systems like POSIX, which often specify stricter requirements for error reporting. While the standard permits the setting of errno regardless of the value of math_errhandling in certain circumstances, the POSIX specification dictates that if a domain, pole, or range error occurs and math_errhandling and MATH_ERRNO is zero, errno must either be set to the corresponding error value or left unmodified. This suggests a non-conformant behavior regarding the use of errno, particularly when compared to the general standard. A separate class of errors involves input parsing failures during string-to-float conversion. Where no conversion can be performed because the input string is malformed, the standard typically dictates returning zero. However, the functions utilize an endptr parameter for error reporting; the calling program must check if the endptr equals the start pointer (nptr) to determine if any conversion actually occurred. This contrasts with the standard description of functions like strtod, which suggests returning zero on failure, but the POSIX behavior allows for the setting of errno to EINVAL (Invalid Argument) upon failure, which is not explicitly documented in the standard’s description. This ambiguity introduces severe portability issues, as the behavior when checking for errors by inspecting errno is inconsistent across different library implementations, such as glibc and musl. Consequently, achieving portable error checking necessitates considering the implementation-defined nature of these error mechanisms. For overflow, portability requires careful examination of whether errno is set, and whether the floating-point exception is raised, depending on the specific implementation's compliance with the standard versus POSIX conventions. For underflow, the uncertainty is even greater, as the behavior is implementation-defined, meaning reliance on specific error settings must be guarded by knowledge of the target environment’s behavior. Ultimately, the standard requires clearer specification regarding error conditions for string conversion, especially concerning the interaction between math_errhandling and the setting of errno, to eliminate the non-conformant differences observed in practice. |