|
| 1 | +package agents_engine.generation |
| 2 | + |
| 3 | +import kotlin.test.Test |
| 4 | +import kotlin.test.assertEquals |
| 5 | +import kotlin.test.assertNull |
| 6 | + |
| 7 | +/** |
| 8 | + * #1028 — `LenientJsonParser` infinite-loop / OOM regression guard. |
| 9 | + * |
| 10 | + * In 0.2.2, `parseValue()` fell through to `parseNumber()` on any unrecognized |
| 11 | + * character. `parseNumber()` returned 0 without advancing `pos` when the input |
| 12 | + * had no digits. `parseArray()` / `parseObject()` spun forever, growing the |
| 13 | + * accumulator until the heap was exhausted. |
| 14 | + * |
| 15 | + * The fix has two layers: |
| 16 | + * 1. `parseValue()` throws on a non-JSON-prefix char instead of falling |
| 17 | + * through to `parseNumber()`. Caught by `parse(input)` → returns null. |
| 18 | + * 2. `parseArray()` / `parseObject()` throw on zero-progress in their loop |
| 19 | + * body — defense-in-depth against any future regression. |
| 20 | + * |
| 21 | + * These tests are bounded by `Thread`-side time guards because a regression |
| 22 | + * would manifest as OOM (slow) rather than as an assertion failure. |
| 23 | + */ |
| 24 | +class LenientJsonParserOomGuardTest { |
| 25 | + |
| 26 | + private fun assertCompletesIn(maxMs: Long, block: () -> Any?): Any? { |
| 27 | + var result: Any? = null |
| 28 | + val thread = Thread { result = block() } |
| 29 | + thread.isDaemon = true |
| 30 | + thread.start() |
| 31 | + thread.join(maxMs) |
| 32 | + if (thread.isAlive) { |
| 33 | + // Don't try to interrupt — the bug spins on heap allocation, not on a |
| 34 | + // checkpoint that responds to interrupts. Just fail the test. |
| 35 | + throw AssertionError("LenientJsonParser did not complete within ${maxMs}ms — likely an infinite loop") |
| 36 | + } |
| 37 | + return result |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun `array with non-JSON content does not OOM`() { |
| 42 | + assertNull(assertCompletesIn(2_000) { LenientJsonParser.parse("[abc]") }) |
| 43 | + assertNull(assertCompletesIn(2_000) { LenientJsonParser.parse("[<html>]") }) |
| 44 | + assertNull(assertCompletesIn(2_000) { LenientJsonParser.parse("[1, abc, 3]") }) |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + fun `object with unquoted bare-word value does not OOM`() { |
| 49 | + assertNull(assertCompletesIn(2_000) { LenientJsonParser.parse("{\"k\": abc}") }) |
| 50 | + assertNull(assertCompletesIn(2_000) { LenientJsonParser.parse("{\"x\": <html>, \"y\": 1}") }) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun `nested unquoted bare-word values do not OOM`() { |
| 55 | + assertNull(assertCompletesIn(2_000) { LenientJsonParser.parse("{\"outer\": [abc, def]}") }) |
| 56 | + assertNull(assertCompletesIn(2_000) { LenientJsonParser.parse("[[abc], [def]]") }) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `legitimate parses still work`() { |
| 61 | + assertEquals(listOf(1, 2, 3), LenientJsonParser.parse("[1, 2, 3]")) |
| 62 | + assertEquals(mapOf("k" to "v"), LenientJsonParser.parse("{\"k\":\"v\"}")) |
| 63 | + assertEquals(listOf("a", "b"), LenientJsonParser.parse("[\"a\", \"b\"]")) |
| 64 | + assertEquals(listOf(1, "two", true, null), LenientJsonParser.parse("[1, \"two\", true, null]")) |
| 65 | + assertEquals(emptyList<Any?>(), LenientJsonParser.parse("[]")) |
| 66 | + assertEquals(emptyMap<String, Any?>(), LenientJsonParser.parse("{}")) |
| 67 | + assertEquals(mapOf("nested" to listOf(1, 2)), LenientJsonParser.parse("{\"nested\":[1, 2]}")) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `negative numbers and decimals still parse`() { |
| 72 | + assertEquals(listOf(-1, -2.5), LenientJsonParser.parse("[-1, -2.5]")) |
| 73 | + assertEquals(mapOf("temp" to -40), LenientJsonParser.parse("{\"temp\":-40}")) |
| 74 | + } |
| 75 | +} |
0 commit comments