Skip to content

Commit

Permalink
fix: more wild edge cases in objc parsing #30
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Dec 22, 2023
1 parent 7d075b0 commit 66efa4a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
6 changes: 5 additions & 1 deletion objc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,11 @@ func (f *File) GetObjCIvars(vmaddr uint64) ([]objc.Ivar, error) {

var o uint32
if err := binary.Read(f.cr, f.ByteOrder, &o); err != nil {
return nil, fmt.Errorf("failed to read ivar.offset: %v", err)
if err == io.EOF {
o = 0 // I've seen this happen when this points to the zero-filled __DATA __common section
} else {
return nil, fmt.Errorf("failed to read ivar.offset: %v", err)
}
}
n, err := f.GetCString(ivar.NameVMAddr)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion types/objc/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *Category) dump(verbose, addrs bool) string {
if addrs {
comment += fmt.Sprintf(" // %#x", c.VMAddr)
}
if c.Class.IsSwift() {
if c.Class != nil && c.Class.IsSwift() {
if len(comment) > 0 {
comment += " (Swift)"
} else {
Expand Down
54 changes: 38 additions & 16 deletions types/objc/type_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,25 +150,44 @@ func getMethodWithArgs(method, returnType string, args []string) string {
return fmt.Sprintf("(%s)%s;", returnType, method)
}

func getPropertyType(attrs string) string {
var typ string

for _, attr := range strings.Split(attrs, ",") {
if strings.HasPrefix(attr, propertyType) {
attr = strings.TrimPrefix(attr, propertyType)
if strings.HasPrefix(attr, "@\"") {
typ = strings.Trim(attr, "@\"")
if strings.HasPrefix(typ, "<") {
typ = "NSObject" + typ
}
typ += " *"
} else {
typ = decodeType(attr) + " "
func getPropertyType(attrs string) (typ string) {
if strings.HasPrefix(attrs, propertyType) {
var attr string
var typParts []string
parts := strings.Split(attrs, ",")
for i := len(parts) - 1; i >= 0; i-- {
sub := parts[i]
switch string(sub[0]) {
case propertyReadOnly:
case propertyBycopy:
case propertyByref:
case propertyDynamic:
case propertyGetter:
case propertySetter:
case propertyIVar:
case propertyWeak:
case propertyStrong:
case propertyAtomic:
case propertyNonAtomic:
case propertyType:
typParts = append([]string{strings.TrimPrefix(sub, propertyType)}, typParts...)
attr = strings.Join(typParts, ",")
default:
typParts = append([]string{sub}, typParts...)
}
break
}
if strings.HasPrefix(attr, "@\"") {
typ = strings.Trim(attr, "@\"")
if strings.HasPrefix(typ, "<") {
typ = "NSObject" + typ
}
typ += " *"
} else {
typ = decodeType(attr) + " "
}
} else {
typ = "?"
}

return typ
}

Expand Down Expand Up @@ -287,6 +306,9 @@ func decodeType(encType string) string {
inner := encType[strings.IndexByte(encType, '[')+1 : strings.LastIndexByte(encType, ']')]
s += decodeArray(inner)
} else if strings.HasPrefix(encType, "{") { // STRUCT
if !(strings.Contains(encType, "{") && strings.Contains(encType, "}")) {
return "?"
}
inner := encType[strings.IndexByte(encType, '{')+1 : strings.LastIndexByte(encType, '}')]
s += decodeStructure(inner)
} else if strings.HasPrefix(encType, "(") { // UNION
Expand Down

0 comments on commit 66efa4a

Please sign in to comment.