[Bug c/97982] New: integer casting after abs() causes undefined behavior
mytbk920423 at gmail dot com
gcc-bugzilla@gcc.gnu.org
Wed Nov 25 09:26:42 GMT 2020
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97982
Bug ID: 97982
Summary: integer casting after abs() causes undefined behavior
Product: gcc
Version: 10.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: mytbk920423 at gmail dot com
Target Milestone: ---
The following code has different result when compiling with -O0/-O1 and -O2.
Also, ubsan will report an error. But I don't know why the
signed(INT_MIN)->unsigned data conversion is undefined. It's found in GCC
10.2.0 and 4.8.5.
clang does fine with both -O0 and -O2 and reports no ubsan errors.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main()
{
char str[20];
scanf("%s", str);
int32_t x = strtol(str, NULL, 0);
uint32_t u = abs(x); // x=0x80000000 will trigger an undefined behavior
uint64_t val = u;
printf("0x%lx\n", val);
}
More information about the Gcc-bugs
mailing list