Module Name:    src
Committed By:   rillig
Date:           Mon Mar 25 20:39:27 UTC 2024

Modified Files:
        src/common/lib/libutil: snprintb.c
        src/tests/lib/libutil: t_snprintb.c

Log Message:
snprintb: mark the end of the buffer if the buffer is too small

This avoids confusion in case the buffer ends with an incomplete number.


To generate a diff of this commit:
cvs rdiff -u -r1.43 -r1.44 src/common/lib/libutil/snprintb.c
cvs rdiff -u -r1.30 -r1.31 src/tests/lib/libutil/t_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.43 src/common/lib/libutil/snprintb.c:1.44
--- src/common/lib/libutil/snprintb.c:1.43	Tue Mar  5 07:37:08 2024
+++ src/common/lib/libutil/snprintb.c	Mon Mar 25 20:39:26 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: snprintb.c,v 1.43 2024/03/05 07:37:08 rillig Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.44 2024/03/25 20:39:26 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2024 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #  include <sys/cdefs.h>
 #  if defined(LIBC_SCCS)
-__RCSID("$NetBSD: snprintb.c,v 1.43 2024/03/05 07:37:08 rillig Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.44 2024/03/25 20:39:26 rillig Exp $");
 #  endif
 
 #  include <sys/types.h>
@@ -46,7 +46,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.43 2024
 #  include <errno.h>
 # else /* ! _KERNEL */
 #  include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.43 2024/03/05 07:37:08 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.44 2024/03/25 20:39:26 rillig Exp $");
 #  include <sys/param.h>
 #  include <sys/inttypes.h>
 #  include <sys/systm.h>
@@ -231,12 +231,20 @@ finish_buffer(state *s)
 {
 	if (s->line_max > 0) {
 		store_eol(s);
-		if (s->bufsize >= 2 && s->total_len > s->bufsize - 2)
+		store(s, '\0');
+		if (s->bufsize >= 3 && s->total_len > s->bufsize)
+			s->buf[s->bufsize - 3] = '#';
+		if (s->bufsize >= 2 && s->total_len > s->bufsize)
 			s->buf[s->bufsize - 2] = '\0';
+		if (s->bufsize >= 1 && s->total_len > s->bufsize)
+			s->buf[s->bufsize - 1] = '\0';
+	} else {
+		store(s, '\0');
+		if (s->bufsize >= 2 && s->total_len > s->bufsize)
+			s->buf[s->bufsize - 2] = '#';
+		if (s->bufsize >= 1 && s->total_len > s->bufsize)
+			s->buf[s->bufsize - 1] = '\0';
 	}
-	store(s, '\0');
-	if (s->bufsize >= 1 && s->total_len > s->bufsize - 1)
-		s->buf[s->bufsize - 1] = '\0';
 }
 
 int

Index: src/tests/lib/libutil/t_snprintb.c
diff -u src/tests/lib/libutil/t_snprintb.c:1.30 src/tests/lib/libutil/t_snprintb.c:1.31
--- src/tests/lib/libutil/t_snprintb.c:1.30	Mon Mar  4 21:35:28 2024
+++ src/tests/lib/libutil/t_snprintb.c	Mon Mar 25 20:39:27 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: t_snprintb.c,v 1.30 2024/03/04 21:35:28 rillig Exp $ */
+/* $NetBSD: t_snprintb.c,v 1.31 2024/03/25 20:39:27 rillig Exp $ */
 
 /*
  * Copyright (c) 2002, 2004, 2008, 2010, 2024 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
 #include <sys/cdefs.h>
 __COPYRIGHT("@(#) Copyright (c) 2008, 2010, 2024\
  The NetBSD Foundation, inc. All rights reserved.");
-__RCSID("$NetBSD: t_snprintb.c,v 1.30 2024/03/04 21:35:28 rillig Exp $");
+__RCSID("$NetBSD: t_snprintb.c,v 1.31 2024/03/25 20:39:27 rillig Exp $");
 
 #include <stdio.h>
 #include <string.h>
@@ -341,7 +341,7 @@ ATF_TC_BODY(snprintb, tc)
 	// old style, buffer too small for nonzero value
 	h_snprintb_len(
 	    3, "\020", 0x07,
-	    3, "0x");
+	    3, "0#");
 
 	// old style, buffer large enough for nonzero value
 	h_snprintb_len(
@@ -351,17 +351,17 @@ ATF_TC_BODY(snprintb, tc)
 	// old style, buffer too small for '<'
 	h_snprintb_len(
 	    4, "\020\001lsb", 0x07,
-	    8, "0x7");
+	    8, "0x#");
 
 	// old style, buffer too small for description
 	h_snprintb_len(
 	    7, "\020\001lsb", 0x07,
-	    8, "0x7<ls");
+	    8, "0x7<l#");
 
 	// old style, buffer too small for '>'
 	h_snprintb_len(
 	    8, "\020\001lsb", 0x07,
-	    8, "0x7<lsb");
+	    8, "0x7<ls#");
 
 	// old style, buffer large enough for '>'
 	h_snprintb_len(
@@ -371,17 +371,17 @@ ATF_TC_BODY(snprintb, tc)
 	// old style, buffer too small for second description
 	h_snprintb_len(
 	    9, "\020\001one\002two", 0x07,
-	    12, "0x7<one,");
+	    12, "0x7<one#");
 
 	// old style, buffer too small for second description
 	h_snprintb_len(
 	    10, "\020\001one\002two", 0x07,
-	    12, "0x7<one,t");
+	    12, "0x7<one,#");
 
 	// old style, buffer too small for '>' after second description
 	h_snprintb_len(
 	    12, "\020\001one\002two", 0x07,
-	    12, "0x7<one,two");
+	    12, "0x7<one,tw#");
 
 	// old style, buffer large enough for '>' after second description
 	h_snprintb_len(
@@ -1090,28 +1090,28 @@ ATF_TC_BODY(snprintb, tc)
 	    1, "0");
 	h_snprintb_len(
 	    3, "\177\020", 0x07,
-	    3, "0x");
+	    3, "0#");
 	h_snprintb_len(
 	    4, "\177\020", 0x07,
 	    3, "0x7");
 	h_snprintb_len(
 	    7, "\177\020b\000lsb\0", 0x07,
-	    8, "0x7<ls");
+	    8, "0x7<l#");
 	h_snprintb_len(
 	    8, "\177\020b\000lsb\0", 0x07,
-	    8, "0x7<lsb");
+	    8, "0x7<ls#");
 	h_snprintb_len(
 	    9, "\177\020b\000lsb\0", 0x07,
 	    8, "0x7<lsb>");
 	h_snprintb_len(
 	    9, "\177\020b\000one\0b\001two\0", 0x07,
-	    12, "0x7<one,");
+	    12, "0x7<one#");
 	h_snprintb_len(
 	    10, "\177\020b\000one\0b\001two\0", 0x07,
-	    12, "0x7<one,t");
+	    12, "0x7<one,#");
 	h_snprintb_len(
 	    12, "\177\020b\000one\0b\001two\0", 0x07,
-	    12, "0x7<one,two");
+	    12, "0x7<one,tw#");
 	h_snprintb_len(
 	    13, "\177\020b\000one\0b\001two\0", 0x07,
 	    12, "0x7<one,two>");
@@ -1511,7 +1511,7 @@ ATF_TC_BODY(snprintb_m, tc)
 	    11,
 	    20,
 	    "0xff<lsb>\0"
-	    "0xf\0");		// XXX: incomplete number may be misleading
+	    "0x#\0");
 
 	// new-style format, buffer too small for '<' in line 2
 	h_snprintb_m_len(
@@ -1523,17 +1523,19 @@ ATF_TC_BODY(snprintb_m, tc)
 	    11,
 	    20,
 	    "0xff<lsb>\0"
-	    "0xff\0");
+	    "0xf#\0");
 
-	// new-style format, buffer too small for fallback
-	h_snprintb_m(
+	// new-style format, buffer too small for textual fallback
+	h_snprintb_m_len(
+	    24,
 	    "\177\020"
 	    "f\000\004bits\0"
 		"*=fallback\0"
 	    "b\0024\0",
 	    0xff,
 	    64,
-	    "0xff<bits=0xf=fallback,4>\0");
+	    26,
+	    "0xff<bits=0xf=fallbac#\0");
 
 	// new-style format, buffer too small for numeric fallback
 	h_snprintb_m_len(
@@ -1544,7 +1546,7 @@ ATF_TC_BODY(snprintb_m, tc)
 	    0xff,
 	    64,
 	    57,
-	    "0xff<fallback(0000\0");
+	    "0xff<fallback(000#\0");
 
 	// new-style format, buffer too small for numeric fallback past buffer
 	h_snprintb_m_len(
@@ -1557,7 +1559,7 @@ ATF_TC_BODY(snprintb_m, tc)
 	    0xff,
 	    64,
 	    48,
-	    "0xff<fallback\0");
+	    "0xff<fallbac#\0");
 
 	// new style, bits and fields, line break between fields
 	h_snprintb_m(

Reply via email to