c++gccc++14constexprgcc6

GCC Bug - In/Decrement array access in constexpr


I found a bug in GCC 6 and 7 (not in GCC 5) inside constexpr functions, which leads to different results if either the function gets evaluates at compile time (wrong result) or runtime (correct result).

#include <iostream>

constexpr int bar(int *b) {
  int i = 0;
  b[i++] = 1; // GCC produce here an failure.

  return 0;
}

constexpr int foo()
{
  int tmp[] = {0};
  bar(tmp);

  return tmp[0];
}

constexpr int cexprI = foo();

int main()
{
  std::cout << cexprI << " " << foo() << "\n";

  return 0;
}

Live Example

The problem is the increment (also happens for decrement) operation inside the array access.

The compile time result of the constant expression is 0 (wrong) and the runtime result is 1 (correct).

Could anyone confirm this bug and report this to: https://gcc.gnu.org/bugzilla/

I cannot create an account there User account creation has been restricted.. I contacted the adminstrator, but the bug for me is major to critial. So it wanted to also inform you. Thank you!


Solution

  • I've opened https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77553 . Thank you for reporting the issue.