Skip to content

Commit d1fee09

Browse files
QuLogickmuehlbauerpre-commit-ci[bot]
authored
Avoid unsafe casts from float to unsigned int (#9964)
* Avoid unsafe casts from float to unsigned int Fixes #9815 * Update xarray/coding/variables.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update duck_array_ops.py * Update duck_array_ops.py * Update whats-new.rst * Update variables.py add comment --------- Co-authored-by: Kai Mühlbauer <kai.muehlbauer@uni-bonn.de> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 8a96426 commit d1fee09

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Bug fixes
6262
calendar of the datetimes from ``"proleptic_gregorian"`` to ``"gregorian"``
6363
and prevents round-tripping them as :py:class:`numpy.datetime64` values
6464
(:pull:`10352`). By `Spencer Clark <https://github.com/spencerkclark>`_.
65+
- Avoid unsafe casts from float to unsigned int in CFMaskCoder (:issue:`9815`, :pull:`9964`).
66+
By ` Elliott Sales de Andrade <https://github.com/QuLogic>`_.
6567

6668
Performance
6769
~~~~~~~~~~~

xarray/coding/variables.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,17 @@ def encode(self, variable: Variable, name: T_Name = None):
345345
if fill_value is not None and has_unsigned:
346346
pop_to(encoding, attrs, "_Unsigned")
347347
# XXX: Is this actually needed? Doesn't the backend handle this?
348-
data = duck_array_ops.astype(duck_array_ops.around(data), dtype)
348+
# two-stage casting to prevent undefined cast from float to unsigned int
349+
# first float -> int with corresponding itemsize
350+
# second int -> int/uint to final itemsize
351+
signed_dtype = np.dtype(f"i{data.itemsize}")
352+
data = duck_array_ops.astype(
353+
duck_array_ops.astype(
354+
duck_array_ops.around(data), signed_dtype, copy=False
355+
),
356+
dtype,
357+
copy=False,
358+
)
349359
attrs["_FillValue"] = fill_value
350360

351361
return Variable(dims, data, attrs, encoding, fastpath=True)

0 commit comments

Comments
 (0)