Undefined behavior in ef_exp.c

Karl-Johan Karlsson <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.