Skip to content

Commit 7be06ba

Browse files
committed
Rename state to stateData and fix precondition
1 parent 7b5dd3a commit 7be06ba

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

Sources/MachOKit/LoadCommand/ThreadCommand.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ extension ThreadCommand {
4545
return count
4646
}
4747

48-
public func state(cmdsStart: UnsafeRawPointer) -> Data? {
48+
public func stateData(cmdsStart: UnsafeRawPointer) -> Data? {
4949
guard let count = count(cmdsStart: cmdsStart) else {
5050
return nil
5151
}
52+
5253
let stateSizeExpected = Int(count) * MemoryLayout<UInt32>.size
5354
let stateSize = Int(layout.cmdsize) - layoutSize - 2 * MemoryLayout<UInt32>.size
54-
guard stateSizeExpected == stateSize else { return nil }
55+
56+
// consider alignment
57+
guard stateSizeExpected <= stateSize else { return nil }
58+
5559
let ptr = cmdsStart
5660
.advanced(by: offset)
5761
.advanced(by: layoutSize)
@@ -89,13 +93,17 @@ extension ThreadCommand {
8993
return count
9094
}
9195

92-
public func state(in machO: MachOFile) -> Data? {
96+
public func stateData(in machO: MachOFile) -> Data? {
9397
guard let count = count(in: machO) else {
9498
return nil
9599
}
100+
96101
let stateSizeExpected = Int(count) * MemoryLayout<UInt32>.size
97102
let stateSize = Int(layout.cmdsize) - layoutSize - 2 * MemoryLayout<UInt32>.size
98-
guard stateSizeExpected == stateSize else { return nil }
103+
104+
// consider alignment
105+
guard stateSizeExpected <= stateSize else { return nil }
106+
99107
let offset = machO.cmdsStartOffset + offset + layoutSize + 2 * MemoryLayout<UInt32>.size
100108

101109
return machO.fileHandle.readData(

Tests/MachOKitTests/MachOFilePrintTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ extension MachOFilePrintTests {
473473
)?.description ?? "unknown"
474474
)
475475
print("Count:", command.count(in: machO) ?? 0)
476-
if let state = command.state(in: machO) {
476+
if let state = command.stateData(in: machO) {
477477
print(
478478
"State:",
479479
state.withUnsafeBytes {

Tests/MachOKitTests/MachOPrintTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ extension MachOPrintTests {
400400
)?.description ?? "unknown"
401401
)
402402
print("Count:", command.count(cmdsStart: machO.cmdsStartPtr) ?? 0)
403-
if let state = command.state(cmdsStart: machO.cmdsStartPtr) {
403+
if let state = command.stateData(cmdsStart: machO.cmdsStartPtr) {
404404
print(
405405
"State:",
406406
state.withUnsafeBytes {

0 commit comments

Comments
 (0)