Skip to content

curvature() returns inf when res=(0, 0) instead of raising ValueError #1270

@brendancol

Description

@brendancol

Describe the bug

curvature() divides by cellsize * cellsize in every backend kernel with no zero guard. cellsize comes from get_dataarray_resolution(agg), which reads agg.attrs["res"]. If a caller passes a raster whose res attribute is (0, 0) (or one component is 0), the output is inf for every interior pixel instead of a clean error.

Cat 3 (NaN/Inf as logic error) finding from the recent security sweep, MEDIUM severity.

Steps to reproduce

import numpy as np
import xarray as xr
from xrspatial import curvature

data = np.zeros((5, 5), dtype=np.float32)
agg = xr.DataArray(data, attrs={'res': (0, 0)})
result = curvature(agg)
print(result.data)  # all inf instead of an error

Expected behavior

curvature() should raise ValueError when either cellsize component is zero or non-finite. The check needs to run before backend dispatch so all four backends (numpy, cupy, dask+numpy, dask+cupy) inherit it.

Fix

Validate the resolution returned by get_dataarray_resolution(agg) in the public curvature() API and raise ValueError if either component is zero or non-finite. Do not change the inner numba/cupy kernels. Do not substitute a fallback cellsize.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething 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