Skip to content

Commit 6a2ab91

Browse files
waskyosipma
authored andcommitted
Don't assume that struct types have at least one field
In some cases that may not be true with the information at hand (e.g. grididps binary from AMP which had incomplete arduino types).
1 parent 31c49f2 commit 6a2ab91

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

CodeHawk/CHB/bchlib/bCHBCTypeUtil.ml

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -949,27 +949,31 @@ let get_compinfo_field (c: bcompinfo_t) (fname: string): bfieldinfo_t =
949949
let get_struct_field_at_offset
950950
(cinfo: bcompinfo_t) (offset: int): (bfieldinfo_t * int) option =
951951
let finfos = cinfo.bcfields in
952-
let field0 = List.hd finfos in
953-
if offset = 0 then
954-
Some (field0, 0)
952+
(* Some struct types may be incomplete and not have any fields at all *)
953+
if (List.length finfos) = 0 then
954+
None
955955
else
956-
let field0type = resolve_type field0.bftype in
957-
if offset < size_of_btype field0type then
958-
Some (field0, offset)
956+
let field0 = List.hd finfos in
957+
if offset = 0 then
958+
Some (field0, 0)
959959
else
960-
List.fold_left (fun acc finfo ->
961-
match acc with
962-
| Some _ -> acc
963-
| _ ->
964-
match finfo.bfieldlayout with
965-
| Some (foffset, size) ->
966-
if offset = foffset then
967-
Some (finfo, 0)
968-
else if offset > foffset && offset < foffset + size then
969-
Some (finfo, offset - foffset)
970-
else
971-
None
972-
| _ -> None) None finfos
960+
let field0type = resolve_type field0.bftype in
961+
if offset < size_of_btype field0type then
962+
Some (field0, offset)
963+
else
964+
List.fold_left (fun acc finfo ->
965+
match acc with
966+
| Some _ -> acc
967+
| _ ->
968+
match finfo.bfieldlayout with
969+
| Some (foffset, size) ->
970+
if offset = foffset then
971+
Some (finfo, 0)
972+
else if offset > foffset && offset < foffset + size then
973+
Some (finfo, offset - foffset)
974+
else
975+
None
976+
| _ -> None) None finfos
973977

974978

975979
let rec get_compinfo_scalar_type_at_offset

0 commit comments

Comments
 (0)