Re: [llvm-dev] Confusing UB warning

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Re: [llvm-dev] Confusing UB warning

Yaron Keren via cfe-dev
Adding cfe-dev. Removing llvm-dev.

On Tue, Apr 18, 2017 at 7:55 AM Manuel Rigger via llvm-dev <[hidden email]> wrote:

the C11 standard provides the following example for variable-length arrays and pointer arithmetics (see §6.5.6:10):

int main() {
  int n = 4, m = 3;
  int a[n][m];
  int(*p)[m] = a;
  assert(p == &a[0]);
  p += 1;
  assert(p == &a[1]);
  (*p)[2] = 99;
  assert(a[1][2] == 99);
  n = p - a;
  assert(n == 1);

When compiling the program with Clang 3.9, I get the following warning: 

warning: subtraction of pointers to type 'int [m]' of zero size has undefined behavior [-Wpointer-arith]

This is confusing (and probably a bug), since m is not zero and both pointers refer to the same object. If I define a as int a[n][3] the warning no longer appears.

- Manuel
LLVM Developers mailing list
[hidden email]

cfe-dev mailing list
[hidden email]