From b51b44e2cf39b71d9932c67cfb8964a42b4ab1bb Mon Sep 17 00:00:00 2001 From: Edouard Leurent Date: Sat, 30 Apr 2022 23:12:50 +0100 Subject: [PATCH] Fix a bug in validate_fen. 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. --- __tests__/chess.test.js | 4 ++++ chess.js | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/__tests__/chess.test.js b/__tests__/chess.test.js index 02947988..f201013a 100644 --- a/__tests__/chess.test.js +++ b/__tests__/chess.test.js @@ -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, diff --git a/chess.js b/chess.js index caf12efb..ecd83b79 100644 --- a/chess.js +++ b/chess.js @@ -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] } }