Skip to content

Commit c6010d2

Browse files
committed
ui: warn before bounded string search
1 parent 2554d71 commit c6010d2

4 files changed

Lines changed: 53 additions & 9 deletions

File tree

app/src/main/java/com/kyhsgeekcode/disassembler/ui/MainPage.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ fun MainScreen(viewModel: MainViewModel) {
7777

7878
val showSearchForStringsDialog =
7979
viewModel.showSearchForStringsDialog.collectAsState()
80-
if (showSearchForStringsDialog.value == ShowSearchForStringsDialog.Shown) {
81-
SearchForStringsDialog(viewModel)
80+
val dialogState = showSearchForStringsDialog.value
81+
if (dialogState is ShowSearchForStringsDialog.Shown) {
82+
SearchForStringsDialog(viewModel, dialogState.notice)
8283
}
8384
}
8485
}

app/src/main/java/com/kyhsgeekcode/disassembler/ui/tabs/StringTab.kt

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ internal fun buildStringSearchNotice(
6767
}
6868
}
6969

70+
internal fun buildStringSearchDialogNotice(
71+
originalSize: Long,
72+
maxBytes: Int = MAX_SEARCHED_STRING_BYTES
73+
): String? {
74+
if (originalSize <= maxBytes) {
75+
return null
76+
}
77+
return "Large file detected. String search will only scan the first $maxBytes bytes of $originalSize bytes."
78+
}
79+
7080
internal fun clipFoundStringForRendering(
7181
result: FoundString,
7282
maxChars: Int = MAX_RENDERED_STRING_CHARS
@@ -187,7 +197,7 @@ fun StringTab(data: TabData, viewModel: MainViewModel) {
187197
}
188198

189199
@Composable
190-
fun SearchForStringsDialog(viewModel: MainViewModel) {
200+
fun SearchForStringsDialog(viewModel: MainViewModel, notice: String?) {
191201
var from by remember { mutableStateOf("0") }
192202
var to by remember { mutableStateOf("0") }
193203
AlertDialog(
@@ -198,10 +208,18 @@ fun SearchForStringsDialog(viewModel: MainViewModel) {
198208
Text(text = "Search for strings with length ? to ?")
199209
},
200210
text = {
201-
Row {
202-
NumberTextField(from, { from = it }, modifier = Modifier.weight(1f))
203-
Text(text = "to..")
204-
NumberTextField(to, { to = it }, modifier = Modifier.weight(1f))
211+
Column {
212+
notice?.let {
213+
Text(
214+
text = it,
215+
modifier = Modifier.padding(bottom = 8.dp)
216+
)
217+
}
218+
Row {
219+
NumberTextField(from, { from = it }, modifier = Modifier.weight(1f))
220+
Text(text = "to..")
221+
NumberTextField(to, { to = it }, modifier = Modifier.weight(1f))
222+
}
205223
}
206224
},
207225
confirmButton = {

app/src/main/java/com/kyhsgeekcode/disassembler/viewmodel/MainViewModel.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ internal fun openedProjectWorkspaceState(
7272

7373
sealed class ShowSearchForStringsDialog {
7474
object NotShown : ShowSearchForStringsDialog()
75-
object Shown : ShowSearchForStringsDialog()
75+
data class Shown(val notice: String?) : ShowSearchForStringsDialog()
7676
}
7777

7878
class MainViewModel(application: Application) : AndroidViewModel(application) {
@@ -359,7 +359,10 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
359359

360360
fun searchForStrings() {
361361
Timber.d("Will be shown strings dialog")
362-
_showSearchForStrings.value = ShowSearchForStringsDialog.Shown
362+
val notice = getCurrentRelPath()
363+
?.let { ProjectDataStorage.resolveToRead(it)?.length() }
364+
?.let { buildStringSearchDialogNotice(it) }
365+
_showSearchForStrings.value = ShowSearchForStringsDialog.Shown(notice)
363366
}
364367

365368
fun analyze() {

app/src/test/java/com/kyhsgeekcode/disassembler/ui/tabs/StringSearchInputPolicyTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,26 @@ class StringSearchInputPolicyTest {
5353
)
5454
)
5555
}
56+
57+
@Test
58+
fun `buildStringSearchDialogNotice is null for files within the search limit`() {
59+
assertEquals(
60+
null,
61+
buildStringSearchDialogNotice(
62+
originalSize = MAX_SEARCHED_STRING_BYTES.toLong(),
63+
maxBytes = MAX_SEARCHED_STRING_BYTES
64+
)
65+
)
66+
}
67+
68+
@Test
69+
fun `buildStringSearchDialogNotice warns when only a prefix will be searched`() {
70+
assertEquals(
71+
"Large file detected. String search will only scan the first 4 bytes of 10 bytes.",
72+
buildStringSearchDialogNotice(
73+
originalSize = 10,
74+
maxBytes = 4
75+
)
76+
)
77+
}
5678
}

0 commit comments

Comments
 (0)