Writing bugs

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
User avatar
Rebel
Posts: 4660
Joined: Thu Aug 18, 2011 10:04 am

Writing bugs

Post by Rebel » Sun Jan 06, 2019 4:09 pm

Today I had a new one, took me hours to see it.

While parsing a PGN file looking for the moves of a certain engine I wrote the following (pseudo) code:

Code: Select all

printf ("Type engine"); gets(player);

if ((process_pgn_file())!=0) { all done }

if (strcmp(player,white)==0) engine=0;    // decide the color
if (strcmp(player,black)==0) engine=1; 

// Processing the moves

for (x=0; x<=MAX_MOVES; x++) { 
if (x&1 != engine) continue;
do_something_with_the_move_of_player();
}
So what was wrong?
Everybody is unique, except me.

brianr
Posts: 356
Joined: Thu Mar 09, 2006 2:01 pm

Re: Writing bugs

Post by brianr » Sun Jan 06, 2019 4:36 pm

Well, a few of things look like potential issues.
A) the "player" might not match either one for the current game being processed.
B) MAX_MOVES looks like a constant, and not the number of moves in the current game.
C) Loop depends on how move numbers are counted (full moves v half moves).

elpapa
Posts: 203
Joined: Sun Jan 18, 2009 10:27 pm
Location: Sweden
Full name: Patrik Karlsson

Re: Writing bugs

Post by elpapa » Sun Jan 06, 2019 5:12 pm

x <= MAX_MOVES
x < MAX_MOVES

mar
Posts: 1992
Joined: Fri Nov 26, 2010 1:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Writing bugs

Post by mar » Sun Jan 06, 2019 5:41 pm

aside from what has been mentioned already, C operator precedence:
& has lower priority than != (I never understood why bitwise ops are lower than relational to be honest),
so the expression evaluates as x & (1 != engine) instead of the intended (x & 1) != engine
or maybe it was intended? :)
Martin Sedlak

User avatar
Rebel
Posts: 4660
Joined: Thu Aug 18, 2011 10:04 am

Re: Writing bugs

Post by Rebel » Sun Jan 06, 2019 5:52 pm

mar wrote:
Sun Jan 06, 2019 5:41 pm
aside from what has been mentioned already,
These were deliberately left out in the pseudo code but are covered in the real code, so those are not the issue.
mar wrote:
Sun Jan 06, 2019 5:41 pm
C operator precedence:
& has lower priority than != (I never understood why bitwise ops are lower than relational to be honest),
so the expression evaluates as x & (1 != engine) instead of the intended (x & 1) != engine
or maybe it was intended? :)
You got it.

if (x&1 != engine) continue;

is bad C code, the x&1 only affects the flags.

The right way of doing is:

Code: Select all

y = x & 1;
if (y != engine) continue;
Everybody is unique, except me.

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

Re: Writing bugs

Post by hgm » Sun Jan 06, 2019 7:43 pm

Nothing wring with

if((x&1) != engine) ...

If you dislike parentheses, you could also do

if(x-engine & 1) ...

I wouldn't say adding unnecessary dummy variables is "the right way to do it".

Sesse
Posts: 180
Joined: Mon Apr 30, 2018 9:51 pm
Contact:

Re: Writing bugs

Post by Sesse » Sun Jan 06, 2019 8:30 pm

Rebel wrote:
Sun Jan 06, 2019 4:09 pm
Today I had a new one, took me hours to see it.
Why didn't your compiler warn you?

User avatar
Rebel
Posts: 4660
Joined: Thu Aug 18, 2011 10:04 am

Re: Writing bugs

Post by Rebel » Sun Jan 06, 2019 9:21 pm

Sesse wrote:
Sun Jan 06, 2019 8:30 pm
Rebel wrote:
Sun Jan 06, 2019 4:09 pm
Today I had a new one, took me hours to see it.
Why didn't your compiler warn you?
I asked myself the same question.
Everybody is unique, except me.

lauriet
Posts: 162
Joined: Sun Nov 03, 2013 8:32 am
Contact:

Re: Writing bugs

Post by lauriet » Sun Jan 06, 2019 10:01 pm

I've spotted the error. You are using C.

It's like using the large hadron collide to change a flat tyre.

Sven
Posts: 3822
Joined: Thu May 15, 2008 7:57 pm
Location: Berlin, Germany
Full name: Sven Schüle
Contact:

Re: Writing bugs

Post by Sven » Sun Jan 06, 2019 10:05 pm

Rebel wrote:
Sun Jan 06, 2019 5:52 pm

Code: Select all

if (x&1 != engine) continue;
is bad C code, the x&1 only affects the flags.
Just to make it clear for all those who are not fully aware of the consequences of this bug: the expression "x & (1 != engine)" is evaluated differently from the intended "(x & 1) != engine" if engine=1 and x is an even number (including x=0). In all three other combinations it "works by accident".
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)

Post Reply