Skip to content

Commit

Permalink
Fix a bug in validate_fen.
Browse files Browse the repository at this point in the history
When the move number field is empty (trailing space at the end),
the validation returned valid since isNaN('') is false and
(parseInt('') <= 0) is NaN <= 0, which is false.

Added the new (previously failing) test case.
  • Loading branch information
eleurent committed Apr 30, 2022
1 parent 426e7ed commit b51b44e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions __tests__/chess.test.js
Expand Up @@ -1371,6 +1371,10 @@ describe('Validate FEN', () => {
fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 x',
error_number: 2,
},
{
fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 ',
error_number: 2,
},
{
fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 0',
error_number: 2,
Expand Down
4 changes: 2 additions & 2 deletions chess.js
Expand Up @@ -432,12 +432,12 @@ export const Chess = function (fen) {
}

/* 2nd criterion: move number field is a integer value > 0? */
if (isNaN(tokens[5]) || parseInt(tokens[5], 10) <= 0) {
if (isNaN(parseInt(tokens[5])) || parseInt(tokens[5], 10) <= 0) {
return { valid: false, error_number: 2, error: errors[2] }
}

/* 3rd criterion: half move counter is an integer >= 0? */
if (isNaN(tokens[4]) || parseInt(tokens[4], 10) < 0) {
if (isNaN(parseInt(tokens[4])) || parseInt(tokens[4], 10) < 0) {
return { valid: false, error_number: 3, error: errors[3] }
}

Expand Down

0 comments on commit b51b44e

Please sign in to comment.