https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81281

            Bug ID: 81281
           Summary: UBSAN: false positive, dropped promotion to long type.
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: sanitizer
          Assignee: unassigned at gcc dot gnu.org
          Reporter: babokin at gmail dot com
                CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
                    jakub at gcc dot gnu.org, kcc at gcc dot gnu.org, marxin at 
gcc dot gnu.org
  Target Milestone: ---

gcc x86_64, rev249877.

Looks like this one is deferent from other currently open ubsan bugs.

Promotion to long was dropped, so -2024172551 - (long)ci overflows. Also note,
that removing const modifier from ci definition hides the problem.

> cat f_init.cpp
extern const int ci = 1716607962;
int i = -943738830;
long long ll = -43165919987465092LL;

> cat f.cpp
extern const int ci;
extern int i;
extern long long ll;

int foo() {
  int a = int(-2024172551 - i - (ci - ll)) -
        (int(-2024172551 - i - (ci - ll)) -
         int(-2024172551 - (long)ci));
  return a;
}

int main() {
  foo();
  return 0;
}

> g++ -fsanitize=undefined -O0 f.cpp f_init.cpp -o out
> ./out
f.cpp:6:7: runtime error: signed integer overflow: -2024172551 - 1716607962
cannot be represented in type 'int'

Reply via email to