Modern Apps git

← Modern-Apps.git

chess: reject off-board UCI squares and contain sign-in failure

Commit
42730978560383b775c820eb692fd9cf059f6af5
Author
Vayun Mathur <[email protected]>
When
2026-11-19 02:30 UTC (17 days ago)
Parents
087cf65
squareFromUci built a Position from any two characters, so a
length-valid server token like i9i9 or a0a0 produced a row/col
outside 0..7 and board.isValidMove indexed the pieces array out
of bounds. applyGameState only runCatching-wrapped parseUci, so
the crash escaped the stream loops despite the code intending to
skip unknown tokens. Squares are now required to be a1..h8 inside
parseUci, so bad tokens are skipped as designed.

Also, Custom Tabs launchUrl in sign-in threw
ActivityNotFoundException on devices with no browser provider;
signIn now catches it and reports SignInFailed.

Verified: ./gradlew :games:chess:testDevUnitTest (pass),
./check games/chess (pass).

Diff

games/chess/src/main/java/com/vayunmathur/games/chess/network/LichessModels.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/network/LichessModels.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/network/LichessModels.ktindex a1d4d079db..3fe963bbc7 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/network/LichessModels.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/network/LichessModels.kt@@ -29,8 +29,15 @@ private const val MOVE_END = 4 private const val PROMOTION_INDEX = 4  /** `e2` → [Position]: row 0 is rank 8 (matches [com.vayunmathur.games.chess.data.Board]). */-internal fun squareFromUci(square: String): Position =-    Position(RANK_COUNT - (square[1] - '0'), square[0] - 'a')+internal fun squareFromUci(square: String): Position {+    // Server-supplied squares outside a1..h8 are a protocol violation,+    // rejected here so parseUci's callers can skip the token instead of+    // indexing the board out of bounds.+    require(square.length == 2 && square[0] in 'a'..'h' && square[1] in '1'..'8') {+        "Bad UCI square: $square"+    }+    return Position(RANK_COUNT - (square[1] - '0'), square[0] - 'a')+}  /** [Position] → `e2`. */ internal fun Position.toUciSquare(): String =
games/chess/src/main/java/com/vayunmathur/games/chess/platform/OnlineChessViewModel.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/platform/OnlineChessViewModel.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/platform/OnlineChessViewModel.ktindex 24e1e6e5cc..01a9cfa869 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/platform/OnlineChessViewModel.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/platform/OnlineChessViewModel.kt@@ -113,7 +113,13 @@ class OnlineChessViewModel(application: Application) : AndroidViewModel(applicat     fun signIn() {         if (uiStateFlow.value.auth is OnlineAuth.SigningIn) return         uiStateFlow.update { it.copy(auth = OnlineAuth.SigningIn, authError = null) }-        authManager.startSignIn()+        // No Custom Tab/browser provider (minimal devices) must fail the+        // sign-in, not crash the Online tab.+        try {+            authManager.startSignIn()+        } catch (_: android.content.ActivityNotFoundException) {+            failAuth(OnlineAuth.Error.SignInFailed)+        }     }      fun signOut() {