Fix IndexError in read_game on an unmatched ) after an illegal move - #1204
Open
eeshsaxena wants to merge 1 commit into
Open
Fix IndexError in read_game on an unmatched ) after an illegal move#1204eeshsaxena wants to merge 1 commit into
eeshsaxena wants to merge 1 commit into
Conversation
read_game recovers from an illegal move by entering skip mode, and a later unmatched ')' closes that skip by calling end_variation(). When the error happened on the mainline there was no matching begin_variation(), so the unconditional variation_stack.pop() removed the root game node, leaving the stack empty and making the next visit_move() raise IndexError. Keep the root on the stack, mirroring the assert in begin_variation().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Came across this feeding some scraped PGNs through read_game: a short game like
raises
IndexError: list index out of rangefromGameBuilder.visit_moveinstead of being parsed with the bad move recorded ingame.errors.What happens:
Nf3is illegal for Black, so the parser callshandle_errorand setsskip_variation_depth = 1to skip to the end of the (assumed) variation. The following)closes that skip and callsend_variation(), but since the error was on the mainline there was never a matchingbegin_variation().end_variation()popsvariation_stackunconditionally, so it removes the root game node and empties the stack; the next move (e5) then doesself.variation_stack[-1]and raises IndexError.read_gamealready guards the normal)path withlen(board_stack) > 1, andbegin_variation()asserts the root is never pushed as a variation, so I madeend_variation()symmetric: it keeps the root on the stack. With that, the example parses to thee4 e5mainline with one recorded error, and real variations are unaffected.Extended test_variation_stack with this case (it sits right next to the existing superfluous-bracket cases). It raises IndexError on master and passes with the change; the full PGN test suite still passes. Found it by fuzzing read_game with mutated PGNs.