diff --git a/sbin/dmesg/dmesg.c b/sbin/dmesg/dmesg.c index f0fcb8125877..9fee41664b30 100644 --- a/sbin/dmesg/dmesg.c +++ b/sbin/dmesg/dmesg.c @@ -111,8 +111,10 @@ main(int argc, char *argv[]) if (memf == NULL) { /* - * Running kernel. Use sysctl. This gives an unwrapped - * buffer as a side effect. + * Running kernel. Use sysctl. This gives an unwrapped buffer + * as a side effect. Remove nulterm (if present) so the value + * returned by sysctl is formatted as the rest of the code + * expects (the same as the value read from a core file below). */ if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1) err(1, "sysctl kern.msgbuf"); @@ -120,6 +122,8 @@ main(int argc, char *argv[]) errx(1, "malloc failed"); if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1) err(1, "sysctl kern.msgbuf"); + if (buflen > 0 && bp[buflen - 1] == '\0') + buflen--; if (clear) if (sysctlbyname("kern.msgbuf_clear", NULL, NULL, &clear, sizeof(int))) err(1, "sysctl kern.msgbuf_clear");