notes: skip oversized files on import instead of OOMing
- Commit
49b63a82e198a02337fe28ee53ee582833786736- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:23 UTC (28 days ago)
- Parents
eb2ae03
importFiles read any shared file unbounded via readText(), so importing a very large file could OOM the app; a multi-MB note would also wedge the Room row and the export/share path. Imports now stream through a bounded 1MB reader: files beyond MAX_IMPORT_BYTES are skipped with a log line, never truncated, while normal-sized files import exactly as before. Covered by importFilesSkipsOversizedFile (1.2MB file skipped, small file imports). Verified: full :notes:testDevUnitTest suite green (153 tests, 0 failures).
Diff
notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.kt b/notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.ktindex bff522f7cc..972b89a98b 100644--- a/notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.kt+++ b/notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.kt@@ -39,6 +39,7 @@ import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch import kotlinx.coroutines.withContext+import java.io.ByteArrayOutputStream import java.io.File class NotesViewModel(@@ -181,6 +182,25 @@ class NotesViewModel( private val _shareRequests = MutableSharedFlow<NoteShare>(extraBufferCapacity = 1) val shareRequests: SharedFlow<NoteShare> = _shareRequests.asSharedFlow() + // Reads at most [maxBytes] from [uri]; null when the stream is missing or+ // larger (callers skip oversized files instead of OOMing on readText()).+ private fun readBoundedText(ctx: Application, uri: Uri, maxBytes: Int): String? {+ ctx.contentResolver.openInputStream(uri)?.use { stream ->+ val out = ByteArrayOutputStream()+ val buf = ByteArray(8_192)+ var total = 0+ while (true) {+ val n = stream.read(buf)+ if (n < 0) break+ total += n+ if (total > maxBytes) return null+ out.write(buf, 0, n)+ }+ return out.toString(Charsets.UTF_8.name())+ }+ return null+ }+ // Best-effort import of external files: any failure mode means "skip this // file" per this function's contract, and each failure is logged below. @Suppress("TooGenericExceptionCaught")@@ -190,11 +210,12 @@ class NotesViewModel( viewModelScope.launch(Dispatchers.IO) { uris.forEach { uri -> try {- val content = ctx.contentResolver.openInputStream(uri)- ?.bufferedReader()?.use { it.readText() }+ val content = readBoundedText(ctx, uri, MAX_IMPORT_BYTES) if (content != null) { val name = IntentHelper.getFileName(ctx, uri) ?: "Imported Note" repository.upsert(Note(0, name, content))+ } else {+ Log.status(TAG, "skipping oversized import (> $MAX_IMPORT_BYTES bytes): $uri") } } catch (e: Exception) { Log.error(TAG, "Error importing file: $uri", e)@@ -285,6 +306,10 @@ class NotesViewModel( companion object { private const val TAG = "NotesViewModel" private const val SAVE_DEBOUNCE_MS = 300L+ // Largest importable file: readText() on an arbitrary shared file is an+ // OOM vector, and a multi-MB note would also wedge the Room row and the+ // export/share path. Oversized files are skipped, never truncated.+ private const val MAX_IMPORT_BYTES = 1_000_000 } }
notes/src/test/java/com/vayunmathur/notes/platform/NotesViewModelTest.kt
diff --git a/notes/src/test/java/com/vayunmathur/notes/platform/NotesViewModelTest.kt b/notes/src/test/java/com/vayunmathur/notes/platform/NotesViewModelTest.ktindex 306cd98c43..08d721c013 100644--- a/notes/src/test/java/com/vayunmathur/notes/platform/NotesViewModelTest.kt+++ b/notes/src/test/java/com/vayunmathur/notes/platform/NotesViewModelTest.kt@@ -144,6 +144,19 @@ class NotesViewModelTest { } + @Test fun importFilesSkipsOversizedFile() {+ // A file over the import cap must be skipped, not readText() into memory+ // and stored as a multi-MB note; a small file alongside still imports.+ val vm = vm()+ val dir = File(app.cacheDir, "import-test").apply { mkdirs() }+ val big = File(dir, "oversized-skip.txt").apply { writeText("x".repeat(1_200_000)) }+ val small = File(dir, "tiny-ok.txt").apply { writeText("tiny body") }+ vm.importFiles(listOf(Uri.fromFile(big), Uri.fromFile(small)))+ await { runBlocking { repo.getAll().any { it.content == "tiny body" } } }+ val titles = runBlocking { repo.getAll().map { it.title } }+ assertTrue(titles.none { it.contains("oversized-skip") }, "titles=$titles")+ }+ @Test fun factoryCreatesAndRejects() { val factory = NotesViewModelFactory(app, repo) assertTrue(factory.create(NotesViewModel::class.java) is NotesViewModel)