Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#30: Fix segfault from invalid pointer reference in grib_set_double_array() #31

Merged
merged 2 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions gribapi/gribapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,16 +1140,18 @@ def grib_set_double_array(msgid, key, inarray):
"""
h = get_handle(msgid)
length = len(inarray)
a = inarray
if isinstance(inarray, np.ndarray):
nd = inarray
if length > 0:
if not isinstance(a[0], float):
if not isinstance(nd[0], float):
# ECC-1042: input array of integers
a = a.astype(float)
nd = nd.astype(float)
# ECC-1007: Could also call numpy.ascontiguousarray
if not inarray.flags["C_CONTIGUOUS"]:
a = a.copy(order="C")
a = ffi.cast("double*", a.ctypes.data)
nd = nd.copy(order="C")
a = ffi.cast("double*", nd.ctypes.data)
else:
a = inarray

GRIB_CHECK(lib.grib_set_double_array(h, key.encode(ENC), a, length))

Expand Down
8 changes: 8 additions & 0 deletions tests/test_eccodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@ def test_grib_ecc_1007():
codes_release(gid)


def test_grib_float_array():
gid = codes_grib_new_from_samples("regular_ll_sfc_grib2")
values = np.ones((100000,), np.float32)
codes_set_values(gid, values)
getvals = codes_get_values(gid)
assert (getvals == 1.0).all()


def test_gribex_mode():
codes_gribex_mode_on()
codes_gribex_mode_off()
Expand Down