fix: check return values of malloc in xbm2fb and realloc in cfg_desc_append#132
Open
Rampage270906 wants to merge 3 commits intofossasia:masterfrom
Open
fix: check return values of malloc in xbm2fb and realloc in cfg_desc_append#132Rampage270906 wants to merge 3 commits intofossasia:masterfrom
Rampage270906 wants to merge 3 commits intofossasia:masterfrom
Conversation
Contributor
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds 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 handlinggraph 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]
Flow diagram for updated cfg_desc_append realloc handlinggraph 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]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
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.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); |
Contributor
Author
There was a problem hiding this comment.
Sorry about that, accidentally removed it while editing. Restored now.
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.
Two unchecked heap allocations fixed:
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.
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: