Skip to content

Commit 5379b48

Browse files
committed
os: Fix os_log() handling of '%.*P'
- When a count was part of the format string, we looked at the wrong character when forming the count. - The OSLF_CMD_TYPE_DATA command type expects a pointer preceded by a OSLF_CMD_TYPE_COUNT command, not 'count' bytes inline. Fixes <rdar://problem/38080623>.
1 parent 6c29efa commit 5379b48

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

stdlib/public/SDK/os/os_log.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
percent++;
150150
} else {
151151
while (isdigit(percent[1])) {
152-
precision = 10 * precision + (ch - '0');
152+
precision = 10 * precision + (percent[1] - '0');
153153
percent++;
154154
}
155155
if (precision > 1024) precision = 1024;
@@ -219,9 +219,9 @@
219219
if (precision > 0) { // only encode a pointer if we have been given a length
220220
hdr.hdr_flags |= OSLF_HDR_FLAG_HAS_NON_SCALAR;
221221
cmd.cmd_type = OSLF_CMD_TYPE_DATA;
222-
cmd.cmd_size = precision;
222+
cmd.cmd_size = sizeof(void *);
223223
void *p = va_arg(args, void *);
224-
_os_log_encode_arg(ob, &cmd, p);
224+
_os_log_encode_arg(ob, &cmd, &p);
225225
hdr.hdr_cmd_cnt++;
226226
precision = 0;
227227
done = true;

test/stdlib/os.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ if #available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
1616
os_log("test: %d", 42)
1717
os_log("test2: %@", "test")
1818
}
19+
20+
osAPI.test("logData") {
21+
let data = "hello logging world".data(using: .utf8)!
22+
23+
data.withUnsafeBytes { (bytes: UnsafePointer<CChar>) in
24+
os_log("%.3P", OpaquePointer(bytes))
25+
os_log("%.10P", OpaquePointer(bytes))
26+
os_log("%.*P", OpaquePointer(bytes))
27+
}
28+
}
1929
osAPI.test("newLog") {
2030
let newLog = OSLog(subsystem: "com.apple.Swift", category: "Test")
2131
os_log("test", log: newLog)

0 commit comments

Comments
 (0)