Skip to content

Avoid integer overflow in bounds_to_vertices - #684

Open
Don-Yin wants to merge 1 commit into
xarray-contrib:mainfrom
Don-Yin:fix-integer-bounds-order
Open

Don-Yin wants to merge 1 commit into
xarray-contrib:mainfrom
Don-Yin:fix-integer-bounds-order

Conversation

@Don-Yin

@Don-Yin Don-Yin commented Oct 23, 2026

Copy link
Copy Markdown

I got [5, 15, 15, 25] from these descending uint64 bounds, rather than [35, 25, 15, 5]:

import cf_xarray
import numpy as np
import xarray as xr

bounds = xr.DataArray(
    np.array([[25, 35], [15, 25], [5, 15]], dtype="uint64"),
    dims=("x", "bounds"),
    coords={"x": np.array([30, 20, 10], dtype="uint64")},
)
cf_xarray.bounds_to_vertices(bounds, "bounds").values

I replaced the two difference-based order checks with adjacent comparisons, so unsigned wraparound and large signed differences cannot flip the order. This also removes the timedelta-to-float conversions: datetime and cftime values compare directly, without changing the output dtype.

I ran pytest -n auto --cov=./ --cov-report=xml, the CI mypy command, pre-commit run --all-files, and python -m build; I also checked the helpers with NumPy 1.26.4 and xarray 2024.7.0.

Vertices returned for the same descending uint64 bounds before and after the fix

Signed-off-by: Don Yin <Don_Yin@outlook.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant