Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,25 @@ func FuzzTestPushNotationMove(f *testing.F) {
})
}

func TestInvalidPushNotationMove(t *testing.T) {
fen := "r1bqk1nr/pp1pppbp/6p1/1Bp1P3/P2n1P2/2N2N2/1PPP2PP/R1BQK2R w KQkq - 0 1"
bogusMv := "Kxh1"
opt, err := FEN(fen)
if err != nil {
t.Fatalf("FEN(fen) failed")
}
game := NewGame(opt)

err = game.PushNotationMove(bogusMv, UCINotation{}, nil)
if err == nil {
t.Errorf("PushNotationMove() (uci) succeeded in pushing bogus mv when it should have failed")
}
err = game.PushNotationMove(bogusMv, AlgebraicNotation{}, nil)
if err == nil {
t.Errorf("PushNotationMove() (alg) succeeded in pushing bogus mv when it should have failed")
}
}

func validateSplit(t *testing.T, origPgn string, expectedLastLines []string) {
reader := strings.NewReader(origPgn)
scanner := NewScanner(reader)
Expand Down
10 changes: 10 additions & 0 deletions notation.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ func (UCINotation) Decode(pos *Position, s string) (*Move, error) {
if l < 4 || l > 5 {
return nil, fmt.Errorf("chess: invalid UCI notation length %d in %q", l, s)
}
for idx := 0; idx < 2; idx += 2 {
if s[idx+0] < 'a' || s[idx+0] > 'h' {
return nil, fmt.Errorf("chess: invalid UCI notation sq:%v file:%v",
idx/2, s[0])
}
if s[idx+1] < '1' || s[idx+1] > '8' {
return nil, fmt.Errorf("chess: invalid UCI notation sq:%v rank:%v",
idx/2, s[0])
}
}

// Convert directly instead of using map lookups
s1 := Square((s[0] - 'a') + (s[1]-'1')*8)
Expand Down
Loading