[PATCH] Set errno in strtof variants when conversion from double overflows

Kyrill Tkachov kyrylo.tkachov@foss.arm.com
Thu Dec 15 16:47:00 GMT 2016


Hi all,

I've been investigating some libstdc++ execution failures on newlib bare-metal targets, in particular arm-none-eabi and aarch64-none-elf.
In particular:
21_strings/basic_string/numeric_conversions/char/stof.cc
21_strings/basic_string/numeric_conversions/wchar_t/stof.cc

These tests perform a conversion from string to a float and end up mapping down to the strtof/wcstof C functions.
They create a string from a large number that is representable in double precision but not in single precision and expect
strtof to return infinity and set errno to ERANGE so that libstdc++ can throw an exception.
But the implementation of strtof uses strtod internally to convert the string to a double and then cast the result to a float.
This works for getting the infinity result but doesn't set errno properly.
This patch adds a check after the conversion to see if the cast ended up generating an infinity from a non-infinity and sets errno
in that case.

With this patch the aforementioned tests pass on arm-none-eabi and aarch64-none-elf.

Is this ok?
If so, can someone please commit it on my behalf?

Thanks,
Kyrill

2016-12-15  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>

     * libc/stdlib/strtod.c (strtof_l): Set errno to ERANGE when double to
     float conversion results in infinity.
     (strtof): Likewise.
     * libc/stdlib/wcstod.c (wcstof_l): Likewise.
     (wcstof): Likewise.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: newlib-errno.patch
Type: text/x-patch
Size: 2241 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/newlib/attachments/20161215/1f2c922f/attachment.bin>


More information about the Newlib mailing list