Skip to content

fix: check return values of malloc in xbm2fb and realloc in cfg_desc_append#132

Open
Rampage270906 wants to merge 3 commits intofossasia:masterfrom
Rampage270906:fix/unchecked-malloc-xbm-and-usb
Open

fix: check return values of malloc in xbm2fb and realloc in cfg_desc_append#132
Rampage270906 wants to merge 3 commits intofossasia:masterfrom
Rampage270906:fix/unchecked-malloc-xbm-and-usb

Conversation

@Rampage270906
Copy link
Copy Markdown
Contributor

@Rampage270906 Rampage270906 commented Mar 21, 2026

Two unchecked heap allocations fixed:

  1. xbm2fb() in xbm.c
    malloc return value was not checked. If allocation fails, the subsequent memset dereferences NULL. xbm2fb() is called every
    frame in the animation loop. Fixed by switching to calloc (which zeroes memory, removing the need for memset) and returning early if allocation fails.

  2. cfg_desc_append() in usb/setup.c
    realloc return value was not checked, with a TODO comment left by the original author acknowledging the missing check. If realloc fails, the original cfg_desc pointer is overwritten with NULL(memory leak) and the subsequent memcpy dereferences NULL. Fixed by storing the result in a temporary pointer, checking for NULL, and only updating cfg_desc on success.

Summary by Sourcery

Ensure heap allocations in graphics and USB configuration code safely handle allocation failures.

Bug Fixes:

  • Handle potential allocation failure in xbm2fb temporary framebuffer allocation to avoid NULL dereference in the animation loop.
  • Guard cfg_desc_append against realloc failure to prevent losing the original buffer and dereferencing a NULL pointer.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Mar 21, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds proper heap allocation failure handling in xbm2fb() and cfg_desc_append() by checking calloc/realloc results before use and avoiding NULL dereferences or leaks.

Flow diagram for updated xbm2fb heap allocation handling

graph TD
    A[xbm2fb entry] --> B[Compute W using ALIGN_8BIT]
    B --> C[Call calloc for tmpfb with W elements of uint16_t]
    C --> D{tmpfb is NULL?}
    D -- Yes --> E[Return early from xbm2fb]
    D -- No --> F{xbm height and row plus height are nonnegative and col nonnegative?}
    F -- No --> G[Function continues without rendering loop]
    F -- Yes --> H[Loop over h from 0 to xbm.h]
    H --> I[Loop over w from 0 to W]
    I --> J[Update tmpfb index w based on xbm bits]
    J --> K[Copy tmpfb row into framebuffer fb]
    K --> L[Advance framebuffer pointer and repeat loops]
    G --> M[xbm2fb exit]
    L --> M[xbm2fb exit]
Loading

Flow diagram for updated cfg_desc_append realloc handling

graph TD
    A[cfg_desc_append entry] --> B[If cfg_desc is NULL set cfg_len from 0 else read wTotalLength]
    B --> C[Compute newlen as cfg_len plus len]
    C --> D[Call realloc on cfg_desc into tmp]
    D --> E{tmp is NULL?}
    E -- Yes --> F[Return early from cfg_desc_append without modifying cfg_desc]
    E -- No --> G[Assign cfg_desc from tmp]
    G --> H[Copy len bytes from desc into cfg_desc offset cfg_len]
    H --> I[cfg_desc_append exit]
Loading

File-Level Changes

Change Details Files
Harden xbm2fb() against allocation failure and simplify initialization of the temporary framebuffer buffer.
  • Replace malloc+memset with calloc when allocating the temporary framebuffer line buffer
  • Check the allocation result and return early if the allocation fails to avoid NULL dereference
  • Use sizeof(*tmpfb) instead of hardcoding the element type size for the allocation
src/xbm.c
Make cfg_desc_append() safely handle realloc failures and avoid losing the original buffer pointer.
  • Store realloc result in a temporary pointer before updating the global buffer pointer
  • Check the temporary pointer for NULL and return early if reallocation fails
  • Only update cfg_desc on successful reallocation to avoid memory leaks and NULL dereferences
src/usb/setup.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Both new early returns on allocation failure silently do nothing; consider propagating an error (or at least a status indication) so callers can detect and respond to out-of-memory conditions instead of proceeding as if the operations succeeded.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Both new early returns on allocation failure silently do nothing; consider propagating an error (or at least a status indication) so callers can detect and respond to out-of-memory conditions instead of proceeding as if the operations succeeded.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

*/
void xbm2fb(xbm_t *xbm, uint16_t *fb, int col, int row)
{
int W = ALIGN_8BIT(xbm->w);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that, accidentally removed it while editing. Restored now.

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