Skip to content

Commit

Permalink
Bugfix: out of bounds cdf (OasisLMF#1113)
Browse files Browse the repository at this point in the history
* [gulpy] bugfix read only valid length of rec

* [gulpy] smarter fix, return valid rec by default
  • Loading branch information
mtazzari authored Sep 9, 2022
1 parent 27f7bfe commit 12a9f0e
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions oasislmf/pytools/gul/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def read_getmodel_stream(stream_in, item_map, coverages, compute, seeds, valid_a
if valid_area_peril_id is not None:
valid_area_peril_dict = gen_valid_area_peril(valid_area_peril_id)
else:
valid_area_peril_dict=None
valid_area_peril_dict = None

# init data structures
group_id_rng_index, rec_idx_ptr = gen_structs()
Expand Down Expand Up @@ -136,7 +136,7 @@ def read_getmodel_stream(stream_in, item_map, coverages, compute, seeds, valid_a
)

# persist the cdf records read from the stream
recs.append(rec[:rec_idx_ptr[-1]])
recs.append(rec)

if yield_event:
# event is fully read
Expand Down Expand Up @@ -207,6 +207,7 @@ def stream_to_data(int32_mv, valid_buf, size_cdf_entry, last_event_id, item_map,

# init a counter for the local `rec` array
last_rec_idx_ptr = 0
rec_valid_len = 0

while cursor * int32_mv.itemsize + size_cdf_entry <= valid_buf:

Expand Down Expand Up @@ -251,6 +252,7 @@ def stream_to_data(int32_mv, valid_buf, size_cdf_entry, last_event_id, item_map,

rec_idx_ptr.append(rec_idx_ptr[-1] + Nbins_to_read)
last_rec_idx_ptr = end_rec
rec_valid_len += Nbins_to_read

# register the items to their coverage
item_key = tuple((areaperil_id, vulnerability_id))
Expand Down Expand Up @@ -292,7 +294,7 @@ def stream_to_data(int32_mv, valid_buf, size_cdf_entry, last_event_id, item_map,

damagecdf_i += 1

return cursor, yield_event, event_id, rec, rec_idx_ptr, last_event_id, compute_i, items_data_i, items_data, rng_index, group_id_rng_index, damagecdf_i
return cursor, yield_event, event_id, rec[:rec_valid_len], rec_idx_ptr, last_event_id, compute_i, items_data_i, items_data, rng_index, group_id_rng_index, damagecdf_i


@njit(cache=True, fastmath=True)
Expand Down

0 comments on commit 12a9f0e

Please sign in to comment.