Undefined behavior in ef_exp.c

Karl-Johan Karlsson karl-johan.karlsson@ericsson.com
Wed Apr 23 06:03:22 GMT 2025


If compiling Newlib with GCC Undefined Behavior Sanitizer (ubsan) you
will find an undefined behavior in ef_exp.c. 

Reproducer test program ee.c:

int __errno;
float __ieee754_expf(float);
float f;
int main(void) {
  f = __ieee754_expf(-46.3000107);
}

Compile with gcc (e.g. version 13.3.0):

$ gcc newlib-cygwin/newlib/libm/math/ef_exp.c newlib-
cygwin/newlib/libm/common/math_errf.c -I newlib-
cygwin/newlib/libm/common/ -I newlib-cygwin/newlib/libc/include ee.c -
fsanitize=undefined

Run the executable:

$ ./a.out 
newlib-cygwin/newlib/libm/math/ef_exp.c:93:6: runtime error: left shift
of negative value -67

It should be easy to fix the problem by temporary casting the
expression to an unsigned type while doing the left shift (when doing
add k to y's exponent).
The same problem exist in e_exp.c.

Regards
/ Karl-Johan Karlsson



More information about the Newlib mailing list