Skip to content

Commit b445252

Browse files
committed
stability: clip oversized analyzer string matches
1 parent 92e704a commit b445252

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

app/src/main/java/com/kyhsgeekcode/disassembler/Analyzer.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import kotlin.math.exp
1515
import kotlin.math.ln
1616
import kotlin.math.pow
1717

18+
private const val MAX_EMITTED_FOUND_STRING_CHARS = 4_096
1819

1920
@ExperimentalUnsignedTypes
2021
class Analyzer(private val bytes: ByteArray) {
@@ -50,7 +51,18 @@ class Analyzer(private val bytes: ByteArray) {
5051
val length = i - strstart
5152
val offset = strstart
5253
if (length in min..max) {
53-
val str = String(bytes, strstart, length)
54+
val previewLength = minOf(length, MAX_EMITTED_FOUND_STRING_CHARS)
55+
val str = String(bytes, strstart, previewLength).let {
56+
if (length > MAX_EMITTED_FOUND_STRING_CHARS) {
57+
if (previewLength <= 3) {
58+
it.take(previewLength)
59+
} else {
60+
it.take(previewLength - 3) + "..."
61+
}
62+
} else {
63+
it
64+
}
65+
}
5466
val fs = FoundString(length, offset.toLong(), str)
5567
// Log.v(TAG,str);
5668
progress(i, bytes.size, fs)
@@ -400,4 +412,4 @@ fun Simpson3_8(f: (Double) -> Double, a: Double, b: Double, N: Int, gamma: Doubl
400412

401413
private fun log2(a: Double): Double {
402414
return ln(a) / ln(2.0)
403-
}
415+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.kyhsgeekcode.disassembler
2+
3+
import org.junit.jupiter.api.Assertions.assertNotNull
4+
import org.junit.jupiter.api.Assertions.assertTrue
5+
import org.junit.jupiter.api.Test
6+
7+
@OptIn(ExperimentalUnsignedTypes::class)
8+
class AnalyzerStringSearchTest {
9+
@Test
10+
fun `searchStrings clips oversized string matches before emitting them`() {
11+
val analyzer = Analyzer(ByteArray(2 * 1024 * 1024) { 'A'.code.toByte() } + byteArrayOf(0))
12+
var foundString: FoundString? = null
13+
14+
analyzer.searchStrings(1, Int.MAX_VALUE) { _, _, result ->
15+
if (result != null) {
16+
foundString = result
17+
}
18+
}
19+
20+
assertNotNull(foundString)
21+
assertTrue(
22+
foundString!!.string.length <= 4_096,
23+
"Analyzer should not allocate oversized string matches before handing them to the UI layer"
24+
)
25+
}
26+
}

0 commit comments

Comments
 (0)