Skip to content

Commit

Permalink
Merge pull request #4401 from tgolsson/ts/cbor-unmarshal-dynarray
Browse files Browse the repository at this point in the history
cbor: fix capacity and ptr calculation for dynarray unmarshal
  • Loading branch information
laytan authored Oct 20, 2024
2 parents 9f609dd + 90a0c83 commit 7c1922b
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions core/encoding/cbor/unmarshal.odin
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,6 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
loc := #caller_location,
) -> (out_of_space: bool, err: Unmarshal_Error) {
for idx: uintptr = 0; length == -1 || idx < uintptr(length); idx += 1 {
elem_ptr := rawptr(uintptr(da.data) + idx*uintptr(elemt.size))
elem := any{elem_ptr, elemt.id}

hdr := _decode_header(d.reader) or_return

// Double size if out of capacity.
Expand All @@ -459,6 +456,10 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
if !ok { return false, .Out_Of_Memory }
}

// Set ptr after potential resizes to avoid invalidation.
elem_ptr := rawptr(uintptr(da.data) + idx*uintptr(elemt.size))
elem := any{elem_ptr, elemt.id}

err = _unmarshal_value(d, elem, hdr, allocator=allocator, loc=loc)
if length == -1 && err == .Break { break }
if err != nil { return }
Expand Down Expand Up @@ -509,7 +510,7 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
raw := (^mem.Raw_Dynamic_Array)(v.data)
raw.data = raw_data(data)
raw.len = 0
raw.cap = length
raw.cap = scap
raw.allocator = context.allocator

_ = assign_array(d, raw, t.elem, length) or_return
Expand Down

0 comments on commit 7c1922b

Please sign in to comment.