Undefined behavior

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
hgm
Posts: 27790
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Undefined behavior

Post by hgm »

I have been using the MinGW compiler recently, because my virus scanner removed Cygwin from my machine. And I am running into a curious warning:

Code: Select all

C:\cygwin\home\egtb>gcc -O2 4menXQ.c -o XQ.exe
4menXQ.c: In function 'InitXQ':
4menXQ.c:177:37: warning: iteration 45u invokes undefined behavior [-Waggressive
-loop-optimizations]
   for&#40;i=0; i<=BSIZE; i++) sqrNr&#91;Ox88&#91;i&#93;&#93; = i;
                                     ^
4menXQ.c&#58;177&#58;3&#58; note&#58; containing loop
   for&#40;i=0; i<=BSIZE; i++) sqrNr&#91;Ox88&#91;i&#93;&#93; = i;
   ^
Can I safely ignore that, or is it something where a malicious compiler now can feel justified to slip in code to format my hard disk?
mar
Posts: 2554
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Undefined behavior

Post by mar »

Code: Select all

for&#40;i=0; i<=BSIZE; i++) sqrNr&#91;Ox88&#91;i&#93;&#93; = i;
Hard to say without context. BSIZE is probably a constant and
Ox88 is a LUT and sqNr probably as well (but bounds/types?).
Or maybe a minimum working example?
Can I safely ignore that, or is it something where a malicious compiler now can feel justified to slip in code to format my hard disk?
I very much doubt this is the case :) Most likely your AV just got a false positive.
User avatar
hgm
Posts: 27790
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Undefined behavior

Post by hgm »

Indeed, they are LUT to translate a contiguous range of square numbers (0 to BSIZE-1) to 0x88-style square numbering of a twice-as-wide board. For the patch I was making I also needed to do the reverse lookup.

This doesn't look as undefined behavior in terms of C semantics to me. So it must be a problem in the optimizer, that is might not have preserved the order of the iterations (which could lead to trouble if Ox88[] contained duplicats). I suppose that a compiler is not allowed to slip in malicious code if the undefined behavior is merely a consequence of its own decision to optimize.

BTW, the AV problem was not due to this code. It occurred on a program that opened a socket for listening. But I was testing it under Cygwin (because I was also compiling it there), so the AV did not only delete the .exe, but also many essential Cygwin components (such as the shell).
Joost Buijs
Posts: 1563
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: Undefined behavior

Post by Joost Buijs »

hgm wrote:

Code: Select all

4menXQ.c&#58;177&#58;37&#58; warning&#58; iteration 45u invokes undefined behavior &#91;-Waggressive
-loop-optimizations&#93;
   for&#40;i=0; i<=BSIZE; i++) sqrNr&#91;Ox88&#91;i&#93;&#93; = i;
                                     ^
4menXQ.c&#58;177&#58;3&#58; note&#58; containing loop
   for&#40;i=0; i<=BSIZE; i++) sqrNr&#91;Ox88&#91;i&#93;&#93; = i;
   ^
It probably means that on iteration 45 you'll get an array bound violation, may be the number of elements of Ox88[] is 45?
How large is the constant BSIZE?
mar
Posts: 2554
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Undefined behavior

Post by mar »

Just a guess: maybe some of the types is signed 8-bit type when it should be unsigned?
But that would mean the program wouldn't work at all, hmm. The compiler may be wrong with the warning as well.
Joost Buijs
Posts: 1563
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: Undefined behavior

Post by Joost Buijs »

Most of the time the compilers are right though, at least it is something that you shouldn't ignore.

Strange AV, I've never had my AV remove anything that it shouldn't, and as a precaution I always have to confirm before it removes something.
User avatar
hgm
Posts: 27790
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Undefined behavior

Post by hgm »

Joost Buijs wrote:It probably means that on iteration 45 you'll get an array bound violation, may be the number of elements of Ox88[] is 45?
How large is the constant BSIZE?
Ah, that is what the 45u means!

Yes, you are right, BSIZE is 45. So the <= should have been <, as the square numbering starts at 0. A bit confusing that it doesn't simply say the array bounds are violated, when it apparently sees that they are.

Anyway, thanks!
Daniel Anulliero
Posts: 759
Joined: Fri Jan 04, 2013 4:55 pm
Location: Nice

Re: Undefined behavior

Post by Daniel Anulliero »

I had an "undefined behavior" with an array without éléments defined ( like for exemple : table [ ] )
But I don't know if it's the case here ...
mar
Posts: 2554
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Undefined behavior

Post by mar »

Joost Buijs wrote:Strange AV, I've never had my AV remove anything that it shouldn't, and as a precaution I always have to confirm before it removes something.
Yes you can always change this in the settings.

I used to work for an AV company and the problem is AV tests.
We for instance had bad score due to the fact that a guy pressed "ignore threat" instead of "protect me" button and counted it as a detection miss.
So first we hid the ignore button and later on we added silent automatic removal of stuff that was "confirmed malware" according to remote databases.

It's obvious that one has to do well in various comparative tests. That they're usually conducted by incompetent people is another story.

There are always some false positives so one has to balance between low amount of FPs and high detection ratio. Those two don't always go hand in hand.
Joost Buijs
Posts: 1563
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: Undefined behavior

Post by Joost Buijs »

hgm wrote: A bit confusing that it doesn't simply say the array bounds are violated, when it apparently sees that they are.
GCC has very mysterious and cryptic error messages sometimes.
That is why I prefer the Intel compiler over GCC.