zonal: guard unbounded allocation in stats(return_type='xarray.DataArray') (#2523)#2533
Merged
Merged
Conversation
…ray') (#2523) The numpy backend's xarray.DataArray return path allocated `np.full((n_stats, values.size), nan)` (float64) with no memory check. `n_stats` is user-controlled via the `stats_funcs` dict and `values.size` grows with the input raster, so the working buffer scales linearly with both. The default 8 stats on a 20000x20000 input requires ~25.6 GB on top of the input rasters. Add `_check_stats_dataarray_memory(n_stats, values_shape)` that reuses the existing `_available_memory_bytes()` helper and raises `MemoryError` when `n_stats * H * W * 8` exceeds 50% of available RAM, with a message pointing at the user-tunable knobs (`stats_funcs`, raster size, `return_type='pandas.DataFrame'`). Pattern matches the per-module unbounded-allocation guards added in #1262 (cost_distance), #1287 (kde), #1288 (mahalanobis), #1291 (multispectral), #1295 (resample), #1296 (sieve), and others. Found by deep-sweep-security-zonal-2026-05-27.
Contributor
Author
PR Review: zonal: guard unbounded allocation in stats(return_type='xarray.DataArray')BlockersNone. SuggestionsNone. Nits
What looks good
Checklist
|
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.
Summary
_stats_numpyxarray.DataArray return path allocatednp.full((n_stats, values.size), nan)(float64) with no memory check;n_statsis user-controlled viastats_funcsandvalues.sizescales with the input raster._check_stats_dataarray_memory(n_stats, values_shape)that reuses the existing_available_memory_bytes()helper and raisesMemoryErrorbefore the allocation when the buffer would exceed 50% of available RAM.Closes #2523. Found by deep-sweep-security-zonal-2026-05-27.
Test plan
test_stats_dataarray_return_type_memory_guard_2523mocks_available_memory_bytesand asserts both the guard fires and the normal path still produces a correctxr.DataArrayxrspatial/tests/test_zonal.pysuite passes (126 tests)