geotiff: validate VRT SrcRect dimensions and offsets (#1784)#1788
Merged
Conversation
The DstRect validation added for #1737 covered only one half of the SimpleSource rectangle pair. A malformed `<SrcRect xSize="-100"/>` (or negative offset) reached `read_to_array` as a bad window, raised `ValueError` for the out-of-range window, and was then swallowed by the lenient source-read try/except meant to handle missing or unreadable source files. Result: malformed XML produced a single warning plus a zero-filled hole, and strict mode surfaced the swallowed error inside the try rather than at the malformed-XML site. Reject `sr.x_size < 0`, `sr.y_size < 0`, `sr.x_off < 0`, and `sr.y_off < 0` up front, before the overlap math and before the source-read try, so the malformed VRT always reaches the caller. Error messages name the offending field and value, matching the existing DstRect error. Upper-bound validation against the source raster extent requires reading the source header and is left for a future change; this commit addresses the negatives only.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR tightens read_vrt input validation for GDAL VRT SimpleSource elements by rejecting negative SrcRect sizes and offsets up front, preventing malformed VRT XML from being misclassified as a missing/unreadable source and silently zero-filled in lenient mode.
Changes:
- Add early
SrcRectvalidation inxrspatial/geotiff/_vrt.pyfor negativexSize,ySize,xOff, andyOff, raisingValueErrorwith the offending field values. - Add a focused regression test suite covering negative
SrcRectsizes/offsets, error message contents, strict-mode parity, and ensuring the missing-source lenient warning path still works for validSrcRect.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
xrspatial/geotiff/_vrt.py |
Rejects negative SrcRect dimensions/offsets before overlap math and before the lenient source-read fallback. |
xrspatial/geotiff/tests/test_geotiff_vrt_srcrect_validation_1784.py |
Adds regression tests ensuring malformed SrcRect raises consistently (lenient + strict) while preserving valid missing-source fallback behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Closes #1784.
Summary
read_vrtnow rejects negative<SrcRect>xSize,ySize,xOff, andyOffvalues up front, with aValueErrorthat names the offending field and value.<SrcRect xSize="-100"/>reachedread_to_arrayas a bad window, raisedValueErrorfor the out-of-range window, and was then swallowed by the lenient source-readtry/exceptmeant to handle missing or unreadable files. Net effect: malformed XML became a single warning plus a zero-filled hole in lenient mode, and surfaced as the swallowed error in strict mode.try, so it raises in both lenient and strict modes.Test plan
xrspatial/geotiff/tests/test_geotiff_vrt_srcrect_validation_1784.pycover negativexSize,ySize,xOff,yOff, error message content, and strict-mode parity.pytest xrspatial/ -k vrtpasses 294 tests; the one failure (test_size_param_validation_gpu_vrt_1776.py::test_tile_size_positive_works) is a pre-existing failure onmainfrom the geotiff: require tile_size to be a multiple of 16 (#1767) #1781 tile_size multiple-of-16 enforcement, unrelated to this change.