Modern Apps git

← Modern-Apps.git

logicgate: sanitize pin widths and cap component recursion

Commit
fe1acc2658b730f68db24b5c22654e7b7f8524af
Author
Vayun Mathur <[email protected]>
When
2026-11-19 02:30 UTC (17 days ago)
Parents
4273097
Persisted sandbox components decode Pin widths with no validation
(and the store file is backup-restored). A width of -7 or less
made formatValue compute a negative padStart length and crash
rendering. Widths outside 1/4/8 now fall back to a 1-bit pin at
the store boundary, and formatValue itself treats width <= 1 as
plain decimal so no caller can crash on it.

ChipCatalog resolved custom components recursively with cycle
detection but no depth cap, so a deep persisted containment chain
overflowed the stack at startup. Resolution and uses() now cap
nesting at 32, matching Flattener's existing cap.

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

Diff

games/logicgate/src/main/java/com/vayunmathur/games/logicgate/data/ComponentStore.kt
diff --git a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/data/ComponentStore.kt b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/data/ComponentStore.ktindex d27a7a7e90..56726f3b5c 100644--- a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/data/ComponentStore.kt+++ b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/data/ComponentStore.kt@@ -69,7 +69,11 @@ class ComponentStore(context: Context, dispatcher: CoroutineDispatcher) {  private fun Pin.toDto() = PinSpecDto(name, width) -private fun PinSpecDto.toDomain() = Pin(name, width)+// Persisted widths are only ever 1/4/8 in-app; anything else is a+// corrupt or cross-version restore and falls back to a 1-bit pin.+private fun PinSpecDto.toDomain() = Pin(name, if (width in VALID_WIDTHS) width else 1)++private val VALID_WIDTHS = setOf(1, 4, 8)  private fun CustomChip.toDto() = ComponentDto(     id = id,
games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/ChipCatalog.kt
diff --git a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/ChipCatalog.kt b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/ChipCatalog.ktindex 4ff7e02ece..ca2cdef705 100644--- a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/ChipCatalog.kt+++ b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/ChipCatalog.kt@@ -12,13 +12,16 @@ class ChipCatalog(val customs: List<CustomChip> = emptyList()) {     init {         val resolved = HashMap<String, ChipSpec?>()         val resolving = HashSet<String>()-        fun resolve(id: String): ChipSpec? {+        fun resolve(id: String, depth: Int = 0): ChipSpec? {             Chips[id]?.let { return it }             if (id in resolved) return resolved[id]+            // A deep containment chain from persisted components must not+            // overflow the stack at startup; Flattener caps at 32 too.+            if (depth >= MAX_DEPTH) return null             val chip = byId[id] ?: return null             if (!resolving.add(id)) return null             val gates = chip.board.circuit.nodes.filterIsInstance<Gate>()-            val inner = gates.map { resolve(it.chipId) }+            val inner = gates.map { resolve(it.chipId, depth + 1) }             resolving.remove(id)             val cyclic = gates.zip(inner).any { (g, s) -> s == null && byId[g.chipId] != null }             val spec = if (cyclic) null else chip.toSpec(inner.sumOf { it?.nandCost ?: 0 })@@ -38,16 +41,20 @@ class ChipCatalog(val customs: List<CustomChip> = emptyList()) {     /** True when [id] is [target] or contains it at any depth. */     fun uses(id: String, target: String): Boolean {         val seen = HashSet<String>()-        fun visit(c: String): Boolean {+        fun visit(c: String, depth: Int): Boolean {             if (c == target) return true+            if (depth >= MAX_DEPTH) return false             if (!seen.add(c)) return false-            return byId[c]?.board?.circuit?.nodes?.any { it is Gate && visit(it.chipId) } == true+            return byId[c]?.board?.circuit?.nodes?.any { it is Gate && visit(it.chipId, depth + 1) } == true         }-        return visit(id)+        return visit(id, 0)     }      /** Built-ins only. */     companion object {         val Builtin = ChipCatalog()++        /** Matches Flattener's nesting cap. */+        const val MAX_DEPTH = 32     } }
games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/ValueFormat.kt
diff --git a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/ValueFormat.kt b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/ValueFormat.ktindex 4626400bc0..853cdc6f3f 100644--- a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/ValueFormat.kt+++ b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/ValueFormat.kt@@ -2,6 +2,8 @@ package com.vayunmathur.games.logicgate.domain.model  /** Signal text: 1-bit as 0/1, buses as zero-padded upper-case hex (one digit per nibble). */ fun formatValue(value: Int, width: Int): String =-    if (width == 1) value.toString() else value.toString(HEX).uppercase().padStart((width + 3) / 4, '0')+    // Widths come from persisted sandbox components; a corrupt width+    // must not turn padStart negative and crash rendering.+    if (width <= 1) value.toString() else value.toString(HEX).uppercase().padStart((width + 3) / 4, '0')  private const val HEX = 16