NO

Author Topic: Strict-aliasing incorrect warning.  (Read 5008 times)

neo313

  • Guest
Strict-aliasing incorrect warning.
« on: July 19, 2014, 07:42:05 PM »
When using variable length arrays as parameters in a function I get the following warning: warning #2805: Possible violation of strict-aliasing rules.

Code: [Select]
#include <stdlib.h>
#include <stdio.h>

void UnitTest2( int y , int x , int d[][x] ) ;

void UnitTest2( int y , int x , int d[][x] )
{
int n = 0 ;
for( int i = 0 ; i < y ; i++ )
{
for( int j = 0 ; j < x ; j++ )
{
n = n + d[i][j] ; // : warning #2805: Possible violation of strict-aliasing rules.

}
}
printf("%d" , n ) ;
}

int main( void )
{
int d[2][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 }  } ;
UnitTest2( 2 , 3 , d ) ;

return 0 ;
}


The warning appears only if compiler optimizations are enabled and warnings are at level 2. Other setting do not matter. Type probably doesn't matter( same happens with size_t or double ).

Using: Pelles RC5, 32bit, Windows 7
« Last Edit: July 19, 2014, 07:47:16 PM by neo313 »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Strict-aliasing incorrect warning.
« Reply #1 on: July 19, 2014, 09:22:25 PM »
This doesn't seem a bug to me.
The warning give an advice on a possible strict aliasing rules, the code can address items out of real array size in other words the pointer can be used for an array of different dimensions (de-facto a different variable).
For those interested on strict-aliasing issue read this.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

neo313

  • Guest
Re: Strict-aliasing incorrect warning.
« Reply #2 on: July 20, 2014, 08:37:25 AM »
Quote
the code can address items out of real array size

Show me where can that happen in my code?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Strict-aliasing incorrect warning.
« Reply #3 on: July 20, 2014, 12:24:55 PM »
Quote
the code can address items out of real array size

Show me where can that happen in my code?
Your code is perfect.  ;)
The point is that the compiler cannot be sure that when accessing the array the indices do not exceed the bounds. So simply give you an advice that this could happen...  :)
The compiler is not so intelligent to check that you call the function one time and always with correct bounds.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

neo313

  • Guest
Re: Strict-aliasing incorrect warning.
« Reply #4 on: July 21, 2014, 08:25:07 AM »
Therefore the warning is invalid. Strict-aliasing checking should not be so myopic. The word possible might make it correct if the input was determined at runtime.

Even so, a correct usage of c should not produce any warning.
« Last Edit: July 21, 2014, 08:42:51 AM by neo313 »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Strict-aliasing incorrect warning.
« Reply #5 on: July 21, 2014, 10:22:24 PM »
Therefore the warning is invalid. Strict-aliasing checking should not be so myopic. The word possible might make it correct if the input was determined at runtime.

Even so, a correct usage of c should not produce any warning.
Well, the point is rather that it is arguable that the usage is right or not...

In any way, it's a warning, making you aware of the potential of creating problems, correct usage of C or not. It's in this case totally up to you to either ignore the warning or modify you code, what ever floats you boat...

Ralf

neo313

  • Guest
Re: Strict-aliasing incorrect warning.
« Reply #6 on: July 22, 2014, 12:00:55 AM »
I like strict-aliasing warnings, but detection is hard, and the current system is experimental, therefore my post in this forum.  You must be aware of the work-in-progress state of this feature, don't be ignorant please.

I hope at least one person appreciates the feedback.

Code: [Select]
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

double UnitTest2( size_t y , size_t x , void* data )
{
double (*d)[x] = data ;  //same problem, no warning

double sum = 0.0 ;
for( size_t i = 0 ; i < y ; i++ )
{
for( size_t j = 0 ; j < x ; j++ )
{
sum = sum + d[i][j] ;
}
}

return sum ;
}

int main( void )
{
double d[2][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 }  } ;
printf("%lf" , UnitTest2( 2 , 3 , d ) ) ;

return 0 ;
}

If I get the time I might post a unit test of aliasing violations( something non-trivial ), to see what gets detected.
« Last Edit: July 22, 2014, 12:08:25 AM by neo313 »

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Strict-aliasing incorrect warning.
« Reply #7 on: July 22, 2014, 11:12:46 AM »
The warning seems wrong, I will try to fix it. The feature is exprimental and incomplete (because it's hard), but shouldn't give false positives - I rather have the compiler say too little than too much.
/Pelle

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Strict-aliasing incorrect warning.
« Reply #8 on: July 22, 2014, 10:46:09 PM »
The warning seems wrong, I will try to fix it. The feature is exprimental and incomplete (because it's hard), but shouldn't give false positives - I rather have the compiler say too little than too much.
+1
That's pretty much what I tried to mention...

Ralf