Module Name: src Committed By: rillig Date: Fri Feb 16 21:25:46 UTC 2024
Modified Files: src/common/lib/libutil: snprintb.c Log Message: snprintb: do not modify bufsize when producing multiple lines To generate a diff of this commit: cvs rdiff -u -r1.33 -r1.34 src/common/lib/libutil/snprintb.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/common/lib/libutil/snprintb.c diff -u src/common/lib/libutil/snprintb.c:1.33 src/common/lib/libutil/snprintb.c:1.34 --- src/common/lib/libutil/snprintb.c:1.33 Fri Feb 16 19:53:40 2024 +++ src/common/lib/libutil/snprintb.c Fri Feb 16 21:25:46 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: snprintb.c,v 1.33 2024/02/16 19:53:40 rillig Exp $ */ +/* $NetBSD: snprintb.c,v 1.34 2024/02/16 21:25:46 rillig Exp $ */ /*- * Copyright (c) 2002 The NetBSD Foundation, Inc. @@ -41,7 +41,7 @@ # include <sys/cdefs.h> # if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: snprintb.c,v 1.33 2024/02/16 19:53:40 rillig Exp $"); +__RCSID("$NetBSD: snprintb.c,v 1.34 2024/02/16 21:25:46 rillig Exp $"); # endif # include <sys/types.h> @@ -51,7 +51,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.33 2024 # include <errno.h> # else /* ! _KERNEL */ # include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.33 2024/02/16 19:53:40 rillig Exp $"); +__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.34 2024/02/16 21:25:46 rillig Exp $"); # include <sys/param.h> # include <sys/inttypes.h> # include <sys/systm.h> @@ -92,10 +92,6 @@ snprintb_m(char *buf, size_t bufsize, co goto internal; } - /* Reserve space for trailing blank line if needed */ - if (line_max > 0) - bufsize--; - int val_len = snprintf(buf, bufsize, num_fmt, (uintmax_t)val); if (val_len < 0) goto internal; @@ -268,13 +264,12 @@ snprintb_m(char *buf, size_t bufsize, co if (sep != '<') STORE('>'); if (line_max > 0) { - bufsize++; STORE('\0'); - if (total_len >= bufsize && bufsize > 1) + if (bufsize >= 2 && total_len > bufsize - 2) buf[bufsize - 2] = '\0'; } STORE('\0'); - if (total_len >= bufsize && bufsize > 0) + if (bufsize >= 1 && total_len > bufsize - 1) buf[bufsize - 1] = '\0'; return (int)(total_len - 1); internal: