fix: guard the text editor against opening very large files (OOM)#457
Open
MiMoHo wants to merge 1 commit into
Open
fix: guard the text editor against opening very large files (OOM)#457MiMoHo wants to merge 1 commit into
MiMoHo wants to merge 1 commit into
Conversation
ReadTextActivity.checkIntent() read the whole file into memory before setting it on the EditText. For file:// URIs it took the file.readText() branch, which only caught Exception; a 100+ MB file threw OutOfMemoryError (an Error, not an Exception), so it was uncaught and the app crashed. Guard checkIntent() with a 1 MB size check (mirroring Fossify Notes) before reading. If the file exceeds the limit, show a 'File too large' toast and finish the activity instead of loading it. Applies to both file:// (File.length()) and content:// (getSizeFromContentUri) sources. Fixes FossifyOrg#123
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type of change(s)
What changed and why
Opening a large text file from the file list crashed the app.
ReadTextActivity.checkIntent()reads the entire file into memory before setting it on theEditText. When a file is opened from the File-Manager list, commonsopenPathIntentadds theREAL_FILE_PATHextra, so the activity builds afile://Uri and takes thefile.readText()branch. That branch only catchesException, but reading a 100+ MB file throwsjava.lang.OutOfMemoryError(anError, not anException), so it goes uncaught and the app crashes. (Thecontent://branch already catchesOutOfMemoryError, so the behaviour was inconsistent.) Even if the read succeeded,setText()of such large text on the UI thread would OOM/ANR.This adds a 1 MB size guard at the start of
checkIntent()— the same limit used by Fossify Notes — checked before any read. If the file exceeds the limit, a "File too large, the limit is 1MB" toast is shown and the activity finishes instead of loading the file. The check covers bothfile://(viaFile.length()) andcontent://(viagetSizeFromContentUri) sources. As requested by @Aga-C on the issue, the message string mirrors Fossify Notes.Changes:
ReadTextActivity.kt: addFILE_SIZE_LIMIT = 1000 * 1000constant and a size check incheckIntent().strings.xml: addfile_too_largestring.CHANGELOG.md: note the fix under Unreleased / Fixed and add the[#123]link reference.No new imports were needed:
Fileis already imported andgetSizeFromContentUri,toastandfinishare available via the existingorg.fossify.commons.extensions.*import.Tests performed
Built
fossDebug, installed on an Android 15 (API 35) emulator.ReadTextActivity): the activity now finishes immediately with a "File too large" toast and no OutOfMemory/crash (logcat clean). Before the fix this OOM-crashed.detekt,lint, unit tests and build all pass locally (CI-equivalent).Closes the following issue(s)
Checklist
CHANGELOG.md(if applicable).Coded with Opus 4.8 ultracode.