Skip to content

Local eager band parameter accepts negative and out-of-range values silently #1673

@brendancol

Description

@brendancol

Describe the bug

xrspatial/geotiff/_reader.py:1963-1964:

```python
if arr.ndim == 3 and ifd.samples_per_pixel > 1 and band is not None:
arr = arr[:, :, band]
```

No validation. band=-1 silently selects the last channel via numpy negative indexing. band=N where N >= samples_per_pixel raises a raw IndexError. The dask and GPU paths validate band against samples_per_pixel and raise ValueError; the local eager path does not.

Same public call (open_geotiff, read_to_array), different error behavior per backend.

Expected behavior

band outside [0, samples_per_pixel - 1] raises ValueError regardless of which backend resolves the read. The message should name samples_per_pixel so users can correct the call.

Suggested fix

Add a validation block before the slice that matches the dask path. Cover the HTTP path at the same time so backend parity holds across local, HTTP, dask, and GPU.

Add a backend-parity test that calls each path with band=-1 and band=samples_per_pixel and asserts the same ValueError text shape.

Additional context

Reported during a code review of the geotiff module.

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiAPI design and consistencybackend-coverageAdding missing dask/cupy/dask+cupy backend supportbugSomething isn't workinginput-validationInput validation and error messages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions