Skip to content

Commit 29e5021

Browse files
committed
fix UB - index out of bounds caused by typo and don't print garbage when parsed board is shorter then expected.
1 parent 8de4293 commit 29e5021

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

game.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,14 @@ setup_game( const char *file_name, int *side_to_move ) {
242242
break;
243243
default:
244244
#if TEXT_BASED
245-
printf( "%s '%c' %s\n", BAD_CHARACTER_ERROR, buffer[pos],
245+
if (buffer[token] != '\0') // Don't log if string ended
246+
printf( "%s '%c' %s\n", BAD_CHARACTER_ERROR, buffer[token],
246247
GAME_FILE_TEXT);
247248
#endif
248249
break;
249250
}
250-
token++;
251+
if (buffer[token] != '\0') // Don't increment if string ended
252+
token++;
251253
}
252254

253255
fgets( buffer, 10, stream );

0 commit comments

Comments
 (0)