Skip to content

Commit 48cd587

Browse files
committed
PR feedback: add Process.readRawString to get byte count in GetStringLengthFunction
1 parent 5a277d4 commit 48cd587

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,34 @@ public class Process {
3535
}
3636

3737
// read a null-terminated string from the target process
38-
public func readString(address: UInt64) throws -> String {
39-
var accumulatedBytes = [UInt8]()
38+
public func readString(address: UInt64, encoding: String.Encoding = .utf8) throws -> String {
39+
let rawBytes = try readRawString(address: address)
40+
guard let result = String(bytes: rawBytes, encoding: encoding) else {
41+
throw Error.InvalidString(address: address)
42+
}
43+
44+
return result
45+
}
46+
47+
// read bytes from the remote process until a zero-byte is encountered; the
48+
// zero-byte is not included in the result
49+
public func readRawString(address: UInt64) throws -> [UInt8] {
4050
var readAddress: UInt64 = address
4151
let chunkSize: UInt = 64
52+
var result: [UInt8] = []
4253

4354
while true {
4455
let chunk: [UInt8] = try readArray(address: readAddress, upToCount: chunkSize)
4556

4657
if let nullIndex = chunk.firstIndex(of: 0) {
47-
accumulatedBytes.append(contentsOf: chunk.prefix(nullIndex))
58+
result.append(contentsOf: chunk.prefix(nullIndex))
4859
break
4960
}
5061

51-
accumulatedBytes.append(contentsOf: chunk)
62+
result.append(contentsOf: chunk)
5263
readAddress += UInt64(chunkSize)
5364
}
5465

55-
guard let result = String(bytes: accumulatedBytes, encoding: .utf8) else {
56-
throw Error.InvalidString(address: address)
57-
}
58-
5966
return result
6067
}
6168

tools/swift-inspect/Sources/swift-inspect/LinuxRemoteProcess.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
let process: LinuxRemoteProcess = LinuxRemoteProcess.fromOpaque(context!)
7979

8080
// copy the string from the remote proces to get its length
81-
guard let string = try? process.process.readString(address: address),
82-
let len = UInt64(exactly: string.count)
81+
guard let bytes = try? process.process.readRawString(address: address),
82+
let len = UInt64(exactly: bytes.count)
8383
else { return 0 }
8484
return len
8585
}

0 commit comments

Comments
 (0)