Skip to content

MFD hydrology functions don't validate companion raster shape, causing OOB reads #2863

@brendancol

Description

@brendancol

Describe the bug

The MFD hydrology functions validate that secondary/companion rasters are 2-D DataArrays, but they never check that the companion raster's shape matches the primary MFD grid shape (H, W). Pass a mismatched companion raster and the CPU kernel reads out of bounds and returns uninitialized garbage instead of raising.

Reproduced with an (8, 3, 3) MFD flow-direction grid and a (1, 1) start_points raster:

import numpy as np, xarray as xr
from xrspatial.hydro import flow_path_mfd
fr = xr.DataArray(np.zeros((8, 3, 3)), dims=('d', 'y', 'x'))
sp = xr.DataArray(np.array([[5.0]]), dims=('y', 'x'))
out = flow_path_mfd(fr, sp)  # no error; returns garbage

The same missing-shape-check shows up in:

  • flow_path_mfd (start_points)
  • watershed_mfd (pour_points)
  • hand_mfd (flow_accum, elevation)
  • stream_order_mfd (flow_accum)
  • stream_link_mfd (flow_accum)

Expected behavior

Each function should check that the companion raster shape matches the primary grid (H, W) and raise a clear ValueError before any kernel indexing happens.

Additional context

Follow-up to #1425, which added _validate_raster on the secondary args but only checked type and ndim, not shape.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinginput-validationInput validation and error messages

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions