Pelles C forum

Pelles C => Bug reports => Topic started by: neo313 on July 19, 2014, 07:42:05 PM

Title: Strict-aliasing incorrect warning.
Post by: neo313 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.


#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
Title: Re: Strict-aliasing incorrect warning.
Post by: frankie 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 (http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html).
Title: Re: Strict-aliasing incorrect warning.
Post by: neo313 on July 20, 2014, 08:37:25 AM
Quotethe code can address items out of real array size

Show me where can that happen in my code?
Title: Re: Strict-aliasing incorrect warning.
Post by: frankie on July 20, 2014, 12:24:55 PM
Quote from: neo313 on July 20, 2014, 08:37:25 AM
Quotethe 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.
Title: Re: Strict-aliasing incorrect warning.
Post by: neo313 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.
Title: Re: Strict-aliasing incorrect warning.
Post by: Bitbeisser on July 21, 2014, 10:22:24 PM
Quote from: neo313 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.
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
Title: Re: Strict-aliasing incorrect warning.
Post by: neo313 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.

#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.
Title: Re: Strict-aliasing incorrect warning.
Post by: Pelle 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.
Title: Re: Strict-aliasing incorrect warning.
Post by: Bitbeisser on July 22, 2014, 10:46:09 PM
Quote from: Pelle 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.
+1
That's pretty much what I tried to mention...

Ralf