Skip to content

Commit 8a41169

Browse files
authored
(bugfix) UCINotation.Decode() does not validate rank/file (#60)
This commit fixes a bug in UCINotation.Decode() which would cause it to incorrectly return a non-nil error when given an input move containing a bogus rank or file.
1 parent 7b24122 commit 8a41169

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

game_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,25 @@ func FuzzTestPushNotationMove(f *testing.F) {
12281228
})
12291229
}
12301230

1231+
func TestInvalidPushNotationMove(t *testing.T) {
1232+
fen := "r1bqk1nr/pp1pppbp/6p1/1Bp1P3/P2n1P2/2N2N2/1PPP2PP/R1BQK2R w KQkq - 0 1"
1233+
bogusMv := "Kxh1"
1234+
opt, err := FEN(fen)
1235+
if err != nil {
1236+
t.Fatalf("FEN(fen) failed")
1237+
}
1238+
game := NewGame(opt)
1239+
1240+
err = game.PushNotationMove(bogusMv, UCINotation{}, nil)
1241+
if err == nil {
1242+
t.Errorf("PushNotationMove() (uci) succeeded in pushing bogus mv when it should have failed")
1243+
}
1244+
err = game.PushNotationMove(bogusMv, AlgebraicNotation{}, nil)
1245+
if err == nil {
1246+
t.Errorf("PushNotationMove() (alg) succeeded in pushing bogus mv when it should have failed")
1247+
}
1248+
}
1249+
12311250
func validateSplit(t *testing.T, origPgn string, expectedLastLines []string) {
12321251
reader := strings.NewReader(origPgn)
12331252
scanner := NewScanner(reader)

notation.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ func (UCINotation) Decode(pos *Position, s string) (*Move, error) {
135135
if l < 4 || l > 5 {
136136
return nil, fmt.Errorf("chess: invalid UCI notation length %d in %q", l, s)
137137
}
138+
for idx := 0; idx < 2; idx += 2 {
139+
if s[idx+0] < 'a' || s[idx+0] > 'h' {
140+
return nil, fmt.Errorf("chess: invalid UCI notation sq:%v file:%v",
141+
idx/2, s[0])
142+
}
143+
if s[idx+1] < '1' || s[idx+1] > '8' {
144+
return nil, fmt.Errorf("chess: invalid UCI notation sq:%v rank:%v",
145+
idx/2, s[0])
146+
}
147+
}
138148

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

0 commit comments

Comments
 (0)