Skip to content

Commit 627f787

Browse files
committed
PR feedback in ElfFile.init
1 parent b121882 commit 627f787

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

tools/swift-inspect/Sources/SwiftInspectLinux/ElfFile.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,16 @@ class ElfFile {
3030
public init(filePath: String) throws {
3131
self.filePath = filePath
3232

33-
let fileData = try Data(contentsOf: URL(fileURLWithPath: filePath), options: .alwaysMapped)
34-
self.fileData = fileData
33+
self.fileData = try Data(contentsOf: URL(fileURLWithPath: filePath), options: .alwaysMapped)
3534

36-
let identLen = Int(EI_NIDENT)
37-
let identMagic = String(bytes: fileData[0..<identLen].prefix(Int(SELFMAG)), encoding: .utf8)
38-
guard identMagic == ELFMAG else {
39-
throw ELFError.notELF64(filePath)
35+
let ident = fileData.prefix(upTo: Int(EI_NIDENT))
36+
37+
guard String(bytes: ident.prefix(Int(SELFMAG)), encoding: .utf8) == ELFMAG else {
38+
throw ELFError.notELF64(filePath, "\(ident.prefix(Int(SELFMAG))) != ELFMAG")
4039
}
4140

42-
let identClass = fileData[Int(EI_CLASS)]
43-
guard identClass == ELFCLASS64 else {
44-
throw ELFError.notELF64(filePath, "\(identClass) != ELFCLASS64")
41+
guard ident[Int(EI_CLASS)] == ELFCLASS64 else {
42+
throw ELFError.notELF64(filePath, "\(ident[Int(EI_CLASS)]) != ELFCLASS64")
4543
}
4644

4745
let ehdrSize = MemoryLayout<Elf64_Ehdr>.size

0 commit comments

Comments
 (0)