Skip to content

Guard landforms() against unbounded kernel allocations (#1302)#1304

Merged
brendancol merged 3 commits into
mainfrom
issue-1302
Apr 29, 2026
Merged

Guard landforms() against unbounded kernel allocations (#1302)#1304
brendancol merged 3 commits into
mainfrom
issue-1302

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Summary

  • Fixes landforms() outer_radius is unbounded — _circular_kernel can request hundreds of GB #1302
  • landforms() passed inner_radius and outer_radius straight into _circular_kernel, which allocates a (2r+1)^2 float64 array. outer_radius=20000 requested ~12.8 GB; outer_radius=100000 requested ~320 GB
  • Adds _check_kernel_memory() mirroring the convolution.py:_check_kernel_memory pattern and calls it for both radii after the existing validation
  • Raises MemoryError before allocation when the kernel would exceed half of available RAM

Test plan

  • pytest xrspatial/tests/test_terrain_metrics.py (194 passed)
  • New test_landforms_outer_radius_memory_guard checks outer_radius=200000 raises MemoryError
  • New test_landforms_inner_radius_memory_guard checks inner_radius=200000 raises MemoryError

slope: clean (no findings)
polygonize: MEDIUM only (Cat 1 missing memory guard, Cat 6 missing _validate_raster)

sky_view_factor will land separately via PR #1300 (HIGH, Cat 1, fixed).
`landforms()` passed `inner_radius` and `outer_radius` straight into
`_circular_kernel`, which allocates a (2r+1)^2 float64 array. With no
upper bound, `outer_radius=20000` would request ~12.8 GB and
`outer_radius=100000` would request ~320 GB.

Add `_check_kernel_memory()` mirroring the convolution.py pattern and
call it for both radii after the existing validation. Raises
MemoryError before allocation when the kernel would exceed half of
available RAM.

Tests cover both inner_radius and outer_radius paths.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Apr 29, 2026
@brendancol
Copy link
Copy Markdown
Contributor Author

@copilot resolve the merge conflicts in this pull request

Co-authored-by: brendancol <433221+brendancol@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 29, 2026

@copilot resolve the merge conflicts in this pull request

Done. Merged origin/main into this branch in commit 929db18 — no conflicts arose since the diverged files (multispectral.py, sky_view_factor.py and their tests) were untouched by this PR. All terrain-metrics tests still pass.

@brendancol brendancol merged commit 0c99d29 into main Apr 29, 2026
1 check was pending
@brendancol brendancol deleted the issue-1302 branch May 4, 2026 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance PR touches performance-sensitive code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

landforms() outer_radius is unbounded — _circular_kernel can request hundreds of GB

2 participants