Skip to content

fix: address decoder memory leaks, ELF parser safety, and python tool… - #615

Closed
premkumar0184 wants to merge 1 commit into
boykopovar:mainfrom
premkumar0184:fix/safety-and-quality-improvements
Closed

premkumar0184 wants to merge 1 commit into
boykopovar:mainfrom
premkumar0184:fix/safety-and-quality-improvements

Conversation

@premkumar0184

@premkumar0184 premkumar0184 commented Oct 5, 2026 •

Copy link
Copy Markdown

… bugs

  • Decoders (JPEG/PNG): Wrap stbi_uc raw pointers in std::unique_ptr RAII guards to prevent memory leaks on allocation failure. Lower PNG MAX_DIMENSION from 2.1B to 16384 and enforce dimension checks in Decode/ParseHeader paths to protect against image bomb DoS attacks.
  • Relinker: Add bounds checking on source ELF buffer in LinuxElfPatcher before reading offsets. Replace strict-aliasing-violating reinterpret_cast with std::memcpy. Use std::tolower and std::memcpy in main.cpp.
  • Tools: Fix pr_overlap.py pathspec to 'core/libs/prx/**/*.cpp' so PR export collisions are detected in subdirectories. Add safety checks for deleted GitHub accounts and empty git output in pr_overlap.py. Allow WINLIBS_BIN environment variable override in package_release.py. Guard zero division in progress.py.

What

Tested

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on:
  • AI-assisted:

… bugs

- Decoders (JPEG/PNG): Wrap stbi_uc raw pointers in std::unique_ptr RAII guards to prevent memory leaks on allocation failure. Lower PNG MAX_DIMENSION from 2.1B to 16384 and enforce dimension checks in Decode/ParseHeader paths to protect against image bomb DoS attacks.
- Relinker: Add bounds checking on source ELF buffer in LinuxElfPatcher before reading offsets. Replace strict-aliasing-violating reinterpret_cast with std::memcpy. Use std::tolower and std::memcpy in main.cpp.
- Tools: Fix pr_overlap.py pathspec to 'core/libs/prx/**/*.cpp' so PR export collisions are detected in subdirectories. Add safety checks for deleted GitHub accounts and empty git output in pr_overlap.py. Allow WINLIBS_BIN environment variable override in package_release.py. Guard zero division in progress.py.
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks for looking through these, and welcome. I read it file by file. Some of it is useful, but it can't go in as one PR, and one change breaks the PR overlap check. Details below.

One topic per PR. CONTRIBUTING asks for one topic per PR, and this one has seven unrelated changes: the JPEG and PNG decoders, the Linux ELF patcher, relinker/main.cpp, and three tools scripts. Could you split it, and fill in the template for each (What, Tested, the checklist and the AI-assisted line)? The CI run also still needs workflow approval, so nothing has been built or tested in CI yet.

tools/pr_overlap.py, merge(): this inverts the conflict check. git merge-tree --write-tree --name-only exits with 1 when there are conflicts and then lists the conflicted files after the tree id. For example, #569 against main exits with 1 and lists four files. The old line returned those files when the exit code was non-zero. The new line returns them only when it is 0, and then the list is always empty. So scan() would treat every conflicting PR as clean, and conflicts() would never report a pair. Please drop this hunk. The if not lines guard can stay, if you have seen a case where git prints nothing.

The core/libs/prx/**/*.cpp pathspec doesn't change anything. Without :(glob), a * in a git pathspec also matches /, so core/libs/prx/*.cpp already matches every .cpp below prx/ (git ls-files gives the same 502 files for both). Exports in subdirectories are already found.

PNG MAX_DIMENSION lowered to 16384. This changes what the guest sees: scePngDec would now reject valid PNGs larger than 16384 pixels on a side, which the PNG format allows (up to 2^31 - 1). If the console's libScePngDec has a lower limit, it needs a source, a test and the console's error code. Without one, please leave the limit as it is.

JPEG dimension checks. A JPEG header stores width and height in 16 bits, and the file already has MAX_DIMENSION = 0xFFFF, so the new checks in ParseHeader and Decode can never trigger.

unique_ptr guards. The only leak was if image.pixels.assign threw bad_alloc, so the guard is a small improvement. If you keep it, create the guard right after the null check and drop the manual stbi_image_free before it, so there is only one owner.

LinuxElfPatcher.cpp. The memcpy reads are fine. The new size check covers up to e_phnum, but the function also writes e_shnum and e_shstrndx, which come after it. As far as I can see, the input has already been parsed by then (result.OriginalHeaders), so a buffer that short can't reach Patch. If you want the check anyway, it should cover the whole 64-byte header and come with a test.

relinker/main.cpp, package_release.py, progress.py. The tolower and memcpy edits don't change behaviour. The WINLIBS_BIN override and the zero-total guard are separate small topics. Each of them could be its own PR, with a line on when it's needed (for progress.py, which group can have zero functions?).

So my suggestion: drop the pr_overlap.py and pathspec changes and the PNG limit, and send the rest as separate PRs, each with its own test or a note on how you checked it.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks again for going through these. I'm closing this one. It bundles seven unrelated changes, and one of them (tools/pr_overlap.py) inverts the conflict check, as I described on 10-05. There has been no update since.

Two parts are worth having on their own:

  • the LinuxElfPatcher.cpp size check, extended to cover e_shnum and e_shstrndx;
  • the PNG decoder's unique_ptr guard, created right after the null check, as the only owner.

If you decide to continue, please open each as its own PR with the template filled in. You can also reopen this one if you'd rather split it from here.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants