Skip to content

Broken bounds-check guard in viewshed _calculate_event_row_col() #2793

@brendancol

Description

@brendancol

Describe the bug

_calculate_event_row_col() in xrspatial/viewshed.py ends with an internal corruption guard:

if abs(x - event_col > 1) or abs(y - event_row > 1):
    raise ValueError("_calculate_event_row_col()")

Operator precedence binds > before abs(), so this evaluates as abs((x - event_col) > 1): abs() applied to a boolean comparison result, which is always 0 or 1. The guard is meant to assert that the computed (x, y) stays within one cell of (event_col, event_row), but it never actually checks that.

The function is @ngjit-compiled, so the dead guard is silent at runtime. Normal output is unaffected because the algorithm already keeps results within one cell, but the guard does not do its job.

Expected behavior

The comparison should sit outside abs() so the guard raises when the computed neighbour drifts more than one cell from the event cell:

if abs(x - event_col) > 1 or abs(y - event_row) > 1:
    raise ValueError("_calculate_event_row_col()")

Additional context

Every branch of the function assigns x to event_col, event_col + 1, or event_col - 1, and likewise for y relative to event_row. The corrected guard holds for all valid inputs and will not fire spuriously. Pure internal bug fix, no public API change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingseverity:highSweep finding: HIGH

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions