Skip to content

Commit 9b374ab

Browse files
committed
Do a little more validation of FENs
1 parent 8213a2e commit 9b374ab

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

js/boardui.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ function EnsureAnalysisStopped() {
3737
}
3838

3939
function UIAnalyzeToggle() {
40-
if (GenerateValidMoves().length == 0) {
41-
UpdatePVDisplay(g_inCheck ? "Checkmate" : "Stalemate");
42-
return;
43-
}
44-
4540
if (InitializeBackgroundEngine()) {
4641
if (!g_analyzing) {
4742
g_backgroundEngine.postMessage("analyze");
@@ -171,7 +166,10 @@ function InitializeBackgroundEngine() {
171166
g_backgroundEngine.onmessage = function (e) {
172167
if (e.data.match("^pv") == "pv") {
173168
UpdatePVDisplay(e.data.substr(3, e.data.length - 3));
174-
} else {
169+
} else if (e.data.match("^message") == "message") {
170+
EnsureAnalysisStopped();
171+
UpdatePVDisplay(e.data.substr(8, e.data.length - 8));
172+
}
175173
UIPlayMove(GetMoveFromString(e.data), null);
176174
}
177175
}

js/garbochess.js

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ function SetHash() {
14911491
return result;
14921492
}
14931493

1494-
function InitializeFromFen(fen){
1494+
function InitializeFromFen(fen) {
14951495
var chunks = fen.split(' ');
14961496

14971497
for (var i = 0; i < 256; i++)
@@ -1511,7 +1511,7 @@ function InitializeFromFen(fen){
15111511
else {
15121512
if (c >= '0' && c <= '9') {
15131513
for (var j = 0; j < parseInt(c); j++) {
1514-
g_board[((row + 2) * 0x10) + (col + 4)] = 0;
1514+
g_board[MakeSquare(row, col)] = 0;
15151515
col++;
15161516
}
15171517
}
@@ -1541,7 +1541,7 @@ function InitializeFromFen(fen){
15411541
break;
15421542
}
15431543

1544-
g_board[((row + 2) * 0x10) + (col + 4)] = piece;
1544+
g_board[MakeSquare(row, col)] = piece;
15451545
col++;
15461546
}
15471547
}
@@ -1550,22 +1550,43 @@ function InitializeFromFen(fen){
15501550
InitializePieceList();
15511551

15521552
g_toMove = chunks[1].charAt(0) == 'w' ? colorWhite : 0;
1553+
var them = 8 - g_toMove;
15531554

15541555
g_castleRights = 0;
1555-
if (chunks[2].indexOf('K') != -1)
1556+
if (chunks[2].indexOf('K') != -1) {
1557+
if (g_board[MakeSquare(7, 4)] != pieceKing | colorWhite ||
1558+
g_board[MakeSquare(7, 7)] != pieceRook | colorWhite) {
1559+
return 'Invalid FEN: White kingside castling not allowed';
1560+
}
15561561
g_castleRights |= 1;
1557-
if (chunks[2].indexOf('Q') != -1)
1562+
}
1563+
if (chunks[2].indexOf('Q') != -1) {
1564+
if (g_board[MakeSquare(7, 4)] != pieceKing | colorWhite ||
1565+
g_board[MakeSquare(7, 0)] != pieceRook | colorWhite) {
1566+
return 'Invalid FEN: White queenside castling not allowed';
1567+
}
15581568
g_castleRights |= 2;
1559-
if (chunks[2].indexOf('k') != -1)
1569+
}
1570+
if (chunks[2].indexOf('k') != -1) {
1571+
if (g_board[MakeSquare(0, 4)] != pieceKing | colorBlack ||
1572+
g_board[MakeSquare(0, 7)] != pieceRook | colorBlack) {
1573+
return 'Invalid FEN: Black kingside castling not allowed';
1574+
}
15601575
g_castleRights |= 4;
1561-
if (chunks[2].indexOf('q') != -1)
1576+
}
1577+
if (chunks[2].indexOf('q') != -1) {
1578+
if (g_board[MakeSquare(0, 4)] != pieceKing | colorBlack ||
1579+
g_board[MakeSquare(0, 0)] != pieceRook | colorBlack) {
1580+
return 'Invalid FEN: Black queenside castling not allowed';
1581+
}
15621582
g_castleRights |= 8;
1583+
}
15631584

15641585
g_enPassentSquare = -1;
15651586
if (chunks[3].indexOf('-') == -1) {
15661587
var col = chunks[3].charAt(0).charCodeAt() - 'a'.charCodeAt();
15671588
var row = 8 - (chunks[3].charAt(1).charCodeAt() - '0'.charCodeAt());
1568-
g_enPassentSquare = ((row + 2) * 0x10) + (col + 4);
1589+
g_enPassentSquare = MakeSquare(row, col);
15691590
}
15701591

15711592
var hashResult = SetHash();
@@ -1585,7 +1606,19 @@ function InitializeFromFen(fen){
15851606
if (!g_toMove) g_baseEval = -g_baseEval;
15861607

15871608
g_move50 = 0;
1588-
g_inCheck = IsSquareAttackable(g_pieceList[(g_toMove | pieceKing) << 4], 8 - g_toMove);
1609+
g_inCheck = IsSquareAttackable(g_pieceList[(g_toMove | pieceKing) << 4], them);
1610+
1611+
// Check for king capture (invalid FEN)
1612+
if (IsSquareAttackable(g_pieceList[(them | pieceKing) << 4], g_toMove)) {
1613+
return 'Invalid FEN: Can capture king';
1614+
}
1615+
1616+
// Checkmate/stalemate
1617+
if (GenerateValidMoves().length == 0) {
1618+
return g_inCheck ? 'Checkmate' : 'Stalemate';
1619+
}
1620+
1621+
return ''
15891622
}
15901623

15911624
var g_pieceIndex = new Array(256);
@@ -2466,7 +2499,10 @@ self.onmessage = function (e) {
24662499
}
24672500
if (e.data.match("^position") == "position") {
24682501
ResetGame();
2469-
InitializeFromFen(e.data.substr(9, e.data.length - 9));
2502+
var result = InitializeFromFen(e.data.substr(9, e.data.length - 9));
2503+
if (result.length != 0) {
2504+
postMessage("message " + result);
2505+
}
24702506
} else if (e.data.match("^search") == "search") {
24712507
g_timeout = parseInt(e.data.substr(7, e.data.length - 7), 10);
24722508
Search(FinishMoveLocalTesting, 99, FinishPlyCallback);

0 commit comments

Comments
 (0)