The Zero That strncpy Skips
No instruction-set claim is made. The guarantee is a C loop invariant: decrementing the count before the loop test reserves the last byte before the first byte is copied. No instruction, CSR, or privilege behavior is asserted or measured.
No claim depends on how any core implements anything. The byte dumps came from compiling the actual xv6 functions on the build VM and running them as a userspace test; no cache, pipeline, or timing behavior is asserted.
No boot code, machine-mode service, or privilege handoff takes part. Both functions are plain C from the kernel string library, exercised outside any firmware context.
The verdict is a kernel string-copy contract. xv6's kernel/string.c carries both functions side by side: strncpy leaves the destination unterminated when the source is longer than n, while safestrcpy decrements the count before copying and then writes the terminator unconditionally, so the last byte always holds the zero, which is exactly what the cited source shows.
The claim is one loop invariant checked against two observed byte dumps, not a computation proved against an independent oracle. No invariant is proved beyond the observed output.
The buffer starts full of X. One call through the actual strncpy in xv6's kernel/string.c, strncpy(dst, "hello world", 5), and the first five bytes read 68 65 6c 6c 6f: "hello", exactly as promised. The sixth byte is still 58: an X. No zero landed anywhere in the first five bytes, so anything that prints the result as a string walks straight past the end. [1]
The same call through safestrcpy, the other function in the same file, writes 68 65 6c 6c 00: "hell" plus the zero. The whole difference is where the decrement lands. strncpy tests n-- > 0: the decrement happens after the test, so all five bytes stay available for copying, and the terminator lands only when the source ends early. safestrcpy tests --n > 0: the decrement happens first, so the loop can consume at most four bytes, and the last byte is reserved before the first byte is copied. [1]
The reservation, in three lines:
while (--n > 0 && (*s++ = *t++) != 0)
;
*s = 0; /* the last byte was reserved before the first was copied */
The run, with both functions compiled from the real kernel/string.c and run on the build VM: destination pre-filled with X, source "hello world", n = 5. strncpy left 68 65 6c 6c 6f 58 58 58. safestrcpy left 68 65 6c 6c 00 58 58 58. The short-source case shows the other side: strncpy(dst, "hi", 5) gives 68 69 00 00 00, padding zeros once the source ends, while safestrcpy gives 68 69 00 and stops, leaving the old fill behind the terminator. One pads but does not guarantee; the other guarantees but does not pad. [1]
Diagram: hand-built by Chris Dillinger for this post
The bench habit: in kernel code a bounded copy has to guarantee its own terminator, because the next reader downstream will treat the buffer as a string. The question is not "did the source fit"; it is "can I point at the byte that ends it". In xv6 that question has one answer: safestrcpy.
Sources
- 01kernel/string.c: strncpy and safestrcpy, plus the firsthand runChris Dillinger
Both functions read directly from the xv6 source: strncpy decrements the count after the loop test, so all n bytes stay copyable and the terminator lands only when the source ends early; safestrcpy decrements before the test, so the loop can consume at most n-1 bytes, then writes the terminator unconditionally. Both compiled from this file on the build VM and run with dst pre-filled with X, src 'hello world', n=5: strncpy left 68 65 6c 6c 6f with the sixth byte still 58; safestrcpy left 68 65 6c 6c 00.