Fix input validation in image resampling and transform pipelines#1085
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the shared image preprocessing pipeline by adding defensive input validation around resampling and model-specific vision transforms, aiming to prevent OOB accesses and null dereferences when malformed inputs or allocation failures occur.
Changes:
- Tighten overflow protection in
precompute_coeffs()and usesize_tfor allocation size math. - Add output-size and crop-box validation in
ImagingResample()to reject invalid resize requests. - Add new null checks after
ImagingResample()in the generic resize path and Phi-4 vision preprocessing, and add a channel-count validation for Phi-3 HD transform.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| shared/api/image_resample.c | Adds overflow/parameter validation in resampling, and adjusts allocation arithmetic. |
| shared/api/image_transforms.hpp | Adds a null check after resampling in Resize::Compute(). |
| shared/api/image_transforms_phi_4.hpp | Adds a null check after resampling in Phi-4 dynamic preprocess resize path. |
| shared/api/image_transforms_phi_3.hpp | Adds channel-count validation to prevent output buffer overruns for non-RGB inputs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
apsonawane
approved these changes
Jun 26, 2026
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.
Fix input validation issues in image resampling and transform pipelines
Summary
This PR adds missing input validation to the image processing pipeline to prevent crashes and out-of-bounds memory access from malformed inputs.
Changes
shared/api/image_resample.cprecompute_coeffs(): the overflow-protection check itself usedintarithmetic that could wrap on extreme resize ratios (e.g., 50M pixels → 1 pixel). Replaced withsize_tarithmetic and added an early guard onsupportbefore computingksize.ImagingResample(): reject reversed coordinates (left >= rightortop >= bottom), out-of-image-bounds boxes, and non-positive output dimensions. Previously, reversed box values caused undersized buffer allocations leading to out-of-bounds reads.shared/api/image_transforms.hppImagingResample()return value inResize::Compute(). The function can return NULL on error (unsupported mode, allocation failure, invalid parameters) but the result was dereferenced unconditionally.shared/api/image_transforms_phi_4.hppImagingResample()return value in the Phi-4 vision resize path (same issue as above).shared/api/image_transforms_phi_3.hppphi3_hd_transform(). The function reads the channel count from the input tensor shape but allocates the output buffer with a hardcoded 3-channel layout. A non-3-channel input would cause writes past the allocated buffer. This matches the existing validation already present in the Phi-4 transform.Testing
These fixes are pure input-validation guards. The overflow path requires a ~50-million-pixel image, the NULL deref requires an allocation failure or unsupported mode (which upstream decoders already reject), and the channel mismatch requires bypassing the RGB decoder that hardcodes 3 channels. All are defense-in-depth checks.