Skip to content

mesh_utils: divide-by-zero on all-zero raster in create_triangulation #1378

@brendancol

Description

@brendancol

Summary

create_triangulation() in xrspatial/gpu_rtx/mesh_utils.py (line 19) computes scale = maxDim / maxH where maxH = float(cupy.amax(raster.data)). On an all-zero raster maxH is 0.0 and scale becomes inf; on an all-NaN raster maxH is nan and scale becomes nan. Either value lands in every vertex z-coordinate via verts[offset+2] = val * scale, so the OptiX raytracer sees garbage geometry and hillshade_rtx / viewshed_gpu return garbage instead of an error.

Context

Deferred Cat 3 finding from the gpu_rtx security audit. The HIGH-severity unbounded VRAM allocation in the same module was fixed in #1308 / PR #1310. The audit flagged this as MEDIUM and deferred to a follow-up:

MEDIUM not fixed (Cat 3): mesh_utils.py:19 scale = maxDim / maxH divides by zero on an all-zero raster, propagating inf/NaN into mesh vertex z-coords; separate follow-up.

Reproducer

import cupy
import numpy as np
import xarray as xr
from xrspatial import hillshade

# All-zero raster (e.g. ocean tile, padding, mask result)
flat = xr.DataArray(cupy.zeros((16, 16), dtype=np.float32), dims=["y", "x"])
out = hillshade(flat, shadows=True)
# scale = 16 / 0.0 = inf, mesh z-coords become inf, garbage shadows

Proposed fix

Check maxH before the divide and raise ValueError if it is non-positive or non-finite. An all-zero or all-NaN raster has no elevation to triangulate, so the mesh carries no useful information for hillshade/viewshed. A clear error beats silent garbage.

Affected entry points

  • xrspatial.hillshade(..., shadows=True) on a cupy raster (routes through hillshade_rtx)
  • xrspatial.viewshed() on a cupy raster (routes through viewshed_gpu)

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