Closed Thomas-- closed 4 years ago
Hello!
I can't seem to reproduce the problem on my machine. I used the example you provided but in my tests it produces the correct output. It's been more than 3 years since I coded that. I don't know why I used sprintf (And even only on those cases). What you propose is much better, and it gets rid of the undefined behaviour of sprintf.
Honestly I never thought anyone would pay attention to my little program, much less look at the code. So, thank you very much for your kind feedback. I'm modifying it :wink:
Greetings, Juan.
Thanks for the fast reaction. And thanks for the program, it's pretty useful for creating presentation slides.
For some games the program produces incorrect output.
Input:
How to reproduce: For the above pgn the program produces an incorrect fen for black's 15th move: $ ./pgn2fen game.pgn 15 b 7/1p1rkp2/p2pbn1p/4p1p1/4P3/6B1/PPPN1PPP/2KR3R w - - 0 16 The correct output should start with r7/...
Bug: The program uses
sprintf(&rook[1],"%d", RANKS - i);
to convert the rank number into a character. However, sprintf appends a terminating null character. This null character is written into the memory location next to&rook[1]
which (most likely) happens to be&board[0][0]
.Bugfix: (It's been a while that I've written c/c++ code, but...) I think replacing the four sprintf lines in the source code with
rook[1] = '0' + (RANKS - i);
should fix the problem. I used$ sed -i 's/\([[:space:]]\)sprintf(&rook\[1\],"%d", RANKS - i);/\1rook\[1\] = '"'"'0'"'"' + (RANKS - i);/g' main.c
to alter the source code before compiling (as described, with make) and then the program produces the correct output: r7/1p1rkp2/p2pbn1p/4p1p1/4P3/6B1/PPPN1PPP/2KR3R w - - 0 16