-
Notifications
You must be signed in to change notification settings - Fork 15
Fix the wrong calculation of buffer of AVIF image which cause memory peak #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughAdjusted memory allocation in SDWebImageAVIFCoder’s CreateCGImage8 by sizing the result buffer to rowBytes multiplied by image height instead of components times rowBytes times height. No other logic or control flow was changed. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches🧪 Generate unit tests
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal). Please share your feedback with us on this Discord post. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
SDWebImageAVIFCoder/Classes/Conversion.m (1)
176-181
: Add overflow guard and reuse rowBytes for consistency.
- Guard against size_t overflow when computing total allocation.
- Reuse the computed rowBytes when initializing resultBuffer.rowBytes to avoid duplication and drift.
Apply this diff:
@@ - size_t const rowBytes = components * sizeof(uint8_t) * avif->width; + size_t const rowBytes = avif->width * components; @@ - resultBufferData = calloc(rowBytes * avif->height, sizeof(uint8_t)); + // Guard against overflow in allocation + if (avif->height != 0 && rowBytes > SIZE_MAX / avif->height) { + err = kvImageMemoryAllocationError; + goto end_all; + } + resultBufferData = calloc(avif->height, rowBytes); @@ - .rowBytes = avif->width * components, + .rowBytes = rowBytes,Add this include once at the top of the file to use SIZE_MAX:
+#include <stdint.h>
Also applies to: 189-191, 203-208
🧹 Nitpick comments (2)
SDWebImageAVIFCoder/Classes/Conversion.m (2)
29-31
: Avoid recomputing stride inside CreateImageFromBuffer.Rely on the caller-provided vImage stride to reduce mismatch risk if future callers use aligned/packed rows.
Apply this diff:
@@ - size_t bitsPerComponent = usesU16 ? 16 : 8; - size_t bitsPerPixel = components * bitsPerComponent; - size_t rowBytes = result->width * components * (usesU16 ? sizeof(uint16_t) : sizeof(uint8_t)); + size_t bitsPerComponent = usesU16 ? 16 : 8; + size_t bitsPerPixel = components * bitsPerComponent; + size_t rowBytes = result->rowBytes; // trust provided strideAlso applies to: 43-47
171-171
: Remove unused variable.scaledAlphaBufferData in the 8-bit path is never written/used. Safe to drop to reduce noise.
Apply this diff:
- uint8_t* scaledAlphaBufferData = NULL; @@ - if (scaledAlphaBufferData) free(scaledAlphaBufferData);Also applies to: 541-541
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
SDWebImageAVIFCoder/Classes/Conversion.m
(1 hunks)
🔇 Additional comments (1)
SDWebImageAVIFCoder/Classes/Conversion.m (1)
189-189
: Correct buffer sizing fix — resolves 4x overallocation.Allocating resultBufferData as rowBytes * height is the correct size for 8-bit paths. This should remove the inflated peak memory observed in #65 for animated frames.
If possible, profile an animated AVIF before/after this change and share the peak RSS/VM numbers to confirm the improvement.
This close #65
This close #63
Summary by CodeRabbit
Bug Fixes
Performance