Skip to content

fix(android): fix resource leak in FileUtils.readFileToByteArray()#4553

Open
hobostay wants to merge 1094 commits intoTencent:mainfrom
hobostay:fix/fileutils-resource-leak
Open

fix(android): fix resource leak in FileUtils.readFileToByteArray()#4553
hobostay wants to merge 1094 commits intoTencent:mainfrom
hobostay:fix/fileutils-resource-leak

Conversation

@hobostay
Copy link
Copy Markdown

Summary

  • Fix resource leak in FileUtils.readFileToByteArray() where FileInputStream and ByteArrayOutputStream may not be properly closed if an exception occurs during reading
  • The original code used a nested try-catch that swallowed exceptions during read, then called close() outside the inner catch — but if close() itself threw, the second stream would leak
  • Refactored to use the same try-finally pattern as the sibling readFile() method, ensuring both streams are always closed in the finally block

Before (buggy)

try {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    FileInputStream fileReader = new FileInputStream(file);
    try {
        // read data...
    } catch (Throwable e) {
        LogUtils.d("FileUtils", "readFileToByteArray: " + e.getMessage());
    }
    // If read threw, these close() calls could also throw,
    // leaving the other stream unclosed
    fileReader.close();
    data = byteArrayOutputStream.toByteArray();
    byteArrayOutputStream.close();
} catch (Exception e) {
    e.printStackTrace();
}

Test plan

  • Run existing unit tests for FileUtils
  • Verify that file reading still works correctly
  • Verify that resources are properly closed even when exceptions occur

🤖 Generated with Claude Code

ozonelmy and others added 30 commits February 20, 2023 07:53
Tencent#2849)

refactor(andr): prod remotedebug feature optimized

fix(andr): prod remotedebug createDebugUrl fixed

fix(andr): prod remotedebug optimize

(cherry picked from commit 25cce417d76c8cc729e0be1c6909076e7cad79db)

fix(andr): vue add tagName for debug in prod

fix(andr): change variable mProdRemoteDebugMode to mPureDebugMode

refactor(andr): add enum DebugMode to manage different mode

fix(andr): optimize debugMode code

fix(andr): logical judgment instead of instance variables to optimize

fix(andr): fix No correct copyright header;

fix(hippy-vue): fix unit test tagName fail
* feat(ios): add and correct some entries in performance logger

* docs(performance): update performance guide

* feat(android): record separate event for `TimeMonitor`

* feat(docs): add android time monitor docs

* feat(docs): add startup performance guide

* feat(ios): add performanceLogger module

* fix(android,js): update performance api

* refactor(android): load time report

* fix(android): code style problems

* refactor(android): change monitor api

* refactor(ios): update performance module with turbo imp

* chore(android): add copyright header

* refactor(android,ios): remove instanceId implement

* feat(docs): add minimum support version

* refactor(android): rename nativeCallback method

* refactor(android): use single payload param in nativeCallback method

* feat(bridge): revert style changed

---------

Co-authored-by: iPel <pel20121221@gmail.com>
Co-authored-by: OpenHippy <124017524+open-hippy@users.noreply.github.com>
…ng (Tencent#2521)

* feat(ios): rich text images support vertical alignment setting

* feat(ios): add rich text's vertical align offset support

* feat(ios): margin support for nested component in richText

* feat(android): add `verticalAlign` for `Image` in rich text

* fix(android): backgroundColor not working in nested text

* feat(android): add properties for `Image` in rich text

`tintColor`, `backgroundColor`, `margin` and `padding`

* feat(ios): text in rich text support verticalAlign setting

* feat(android): verticalAlign for textnode

* feat(android): add backgroundColor in legacy mode

* fix(android): baseline align offset wrong

* feat(ios): support nested text verticalAlign style setting

* fix(android): verticalAlign for boringLayout

* chore(docs): add verticalAlign docs

* chore(ios): make verticalAlign default value baseline

* feat(vue): update text demo

* chore(ios): add verticalAlign and update demo

* chore(docs): update verticalAlign minimum supported version

---------

Co-authored-by: iPel <pel20121221@gmail.com>
Co-authored-by: luomy <ozonelmy@163.com>
Co-authored-by: OpenHippy <124017524+open-hippy@users.noreply.github.com>
* fix(android): textInput may throw IllegalStateException

* fix(android): textInput may throw IllegalStateException

---------

Co-authored-by: OpenHippy <124017524+open-hippy@users.noreply.github.com>
* fix(android): fix unit test case

* fix(android): add license header
feat(docs): add en docs for UserLocal debug mode;
zealotchen0 and others added 13 commits October 29, 2024 17:44
* feat(react): add animation stop

* feat(web): fix image local path

* feat(web): remove unused code
* feat(react): add animation stop

* feat(chore): romove security check

* chore(react): remove unused code
The readFileToByteArray() method did not properly close FileInputStream
and ByteArrayOutputStream when exceptions occurred. If reading threw an
exception, the inner catch block swallowed it and then fileReader.close()
could also throw, leaving byteArrayOutputStream unclosed.

Refactored to use try-finally pattern consistent with the sibling
readFile() method, ensuring both streams are always closed.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@hippy-service
Copy link
Copy Markdown

hippy-service Bot commented Apr 22, 2026

Hi, @hobostay. Thanks for your PR! 👏

🏷️ You can leave a comment in this PR with #help tag when you need help (e.g. some status checks run failed due to internal issue), admin team members will help asap.

@tencent-adm
Copy link
Copy Markdown
Member

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
2 out of 5 committers have signed the CLA.

✅ zealotchen0
✅ wwwcg
❌ 1780243524
❌ etkmao
❌ Test User


Test User seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.