Skip to content

Comments

Fix memory leaks and improve heap safety in NFC plugins#89

Open
baalpeteor2 wants to merge 2 commits intoluu176:mainfrom
baalpeteor2:fix-memory-leaks
Open

Fix memory leaks and improve heap safety in NFC plugins#89
baalpeteor2 wants to merge 2 commits intoluu176:mainfrom
baalpeteor2:fix-memory-leaks

Conversation

@baalpeteor2
Copy link

What's new

Inspiration: contributing to codebases I like to help make them better.

Summary

This PR fixes multiple memory leaks and improves memory safety in the Metroflip codebase to prevent crashes and out-of-memory errors on Flipper Zero devices.

Changes Made

DESFire AID variant added (Orca)

  • Added new Orca AID variant 0xF013F2 (detected as bytes F0 13 F2) based on an Orca plastic card I had lying around.
  • Fixed array size from 89 to 90 to accommodate new AID entry

Ventra/Ultralight File Loading Fix

  • Added "NTAG/Ultralight" device type string (used by Flipper NFC library)
  • Now loads any Ultralight card even without Ventra signature
  • Files saved by Metroflip (at least Ventra) now load properly

Buffer Size Verification

  • Verified all static buffer sizes are sufficient for their data formats
  • Fixed get_navigo_service_provider() buffer from 8 to 12 bytes

Memory Leak Fixes

  • read_calypso_data() - Fixed memory leak when data not found
  • get_country_string() - Changed from malloc to static buffer
  • Unused nfc_scanner_alloc() calls - Removed from 8 plugins
  • Transit display functions - Converted to static buffers

Memory Safety Improvements

  • CalypsoCardData initialization - Initialize pointers to NULL for safe cleanup

  • Allocation failure handling - Added NULL checks for malloc returns

  • Cleanup on failure - Added proper cleanup path for card data on early exit

  • NULL checks in cleanup - Added check for ctx->card in calypso_on_exit

  • Not fixed: this app should adopt a license, like the standard MIT license or something.

Verification

  • Test saving/reloading a ventra pass, and it will now show (was broken before)
  • Read many cards, open and close the app, read more... usually it would crash before I could reopen the app. Many crashes gone
  • This came as a response to 2 issue tickets on the Metroflip repo, for Navigo and Opus crashes. Those should be resolved with this PR, but I didn't have those cards to test.

Checklist (For Reviewer)

  • PR has description of feature/bug
  • Description contains actions to verify feature/bugfix
  • I've built this code, uploaded it to the device and verified feature/bugfix

Father Divine and others added 2 commits January 29, 2026 19:45
@baalpeteor2
Copy link
Author

compiled well in momentum, RM, and if I remember OFW last time I checked, and the compiled fap ran in all 3. If there's a problem compiling (an error w/ FelicaData* or FelicaSystem) let me know and I can upload the difference (basically changing FelicaData to FelicaSystem depending on OFW fw).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants