Skip to content

Commit

Permalink
fix: decoding struct and union fields with ambiguous types (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
t0rr3sp3dr0 committed Jun 2, 2024
1 parent 05c1fd2 commit b5c7468
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions types/objc/type_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,15 @@ func decodeStructOrUnion(typ, kind string) string {
field, rest, ok := CutType(rest)

for ok {
// Although technically possible, binaries produced by clang never have a
// mix of named and unnamed fields in the same struct. This assumption is
// necessary to disambiguate {"x0"@"x1"c}.
if fieldName != "" && rest != "" && strings.HasSuffix(field, `"`) && !strings.HasPrefix(rest, `"`) {
penultQuoteIdx := strings.LastIndex(strings.TrimRight(field, `"`), `"`)
rest = field[penultQuoteIdx:] + rest
field = field[:penultQuoteIdx]
}

if fieldName == "" {
fieldName = fmt.Sprintf("x%d", idx)
}
Expand Down
14 changes: 14 additions & 0 deletions types/objc/type_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ func Test_decodeType(t *testing.T) {
},
want: "struct __CFRuntimeBase { unsigned long long x0; _Atomic unsigned long long x1; }",
},
{
name: "Test struct 4",
args: args{
encType: "{__cfobservers_t=\"slot\"@\"next\"^{__cfobservers_t}}",
},
want: "struct __cfobservers_t { id slot; struct __cfobservers_t *next; }",
},
{
name: "Test union 0",
args: args{
Expand All @@ -95,6 +102,13 @@ func Test_decodeType(t *testing.T) {
},
want: "void * /* union */",
},
{
name: "Test union 3",
args: args{
encType: "(?=\"xpc\"@\"NSObject<OS_xpc_object>\"\"remote\"@\"OS_xpc_remote_connection\")",
},
want: "union { NSObject<OS_xpc_object> *xpc; OS_xpc_remote_connection *remote; }",
},
{
name: "Test block",
args: args{
Expand Down

0 comments on commit b5c7468

Please sign in to comment.