Module Name:    src
Committed By:   rillig
Date:           Fri Feb 16 18:09:16 UTC 2024

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

Log Message:
snprintb: fix '=' and ':' for 8-bit values on platforms with signed char

Previously, '=' and ':' worked only on platforms where plain 'char' is
unsigned. On platforms where plain 'char' is signed, only 7-bit values
worked.


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/common/lib/libutil/snprintb.c
cvs rdiff -u -r1.16 -r1.17 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.28 src/common/lib/libutil/snprintb.c:1.29
--- src/common/lib/libutil/snprintb.c:1.28	Fri Feb 16 18:03:16 2024
+++ src/common/lib/libutil/snprintb.c	Fri Feb 16 18:09:15 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: snprintb.c,v 1.28 2024/02/16 18:03:16 rillig Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.29 2024/02/16 18:09:15 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.28 2024/02/16 18:03:16 rillig Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.29 2024/02/16 18:09:15 rillig Exp $");
 #  endif
 
 #  include <sys/types.h>
@@ -51,7 +51,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.28 2024
 #  include <errno.h>
 # else /* ! _KERNEL */
 #  include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.28 2024/02/16 18:03:16 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.29 2024/02/16 18:09:15 rillig Exp $");
 #  include <sys/param.h>
 #  include <sys/inttypes.h>
 #  include <sys/systm.h>
@@ -206,7 +206,7 @@ snprintb_m(char *buf, size_t bufsize, co
 		int matched = 1;
 		while (*bitfmt != '\0') {
 			char kind = *bitfmt++;
-			int bit = *bitfmt++;
+			uint8_t bit = *bitfmt++;
 			switch (kind) {
 			case 'b':
 				if (((val >> bit) & 1) == 0)
@@ -254,7 +254,7 @@ snprintb_m(char *buf, size_t bufsize, co
 				 * This only works for values in [0..255],
 				 * of course.
 				 */
-				if ((int)field != bit)
+				if (field != bit)
 					goto skip;
 				matched = 1;
 				if (kind == '=')

Index: src/tests/lib/libutil/t_snprintb.c
diff -u src/tests/lib/libutil/t_snprintb.c:1.16 src/tests/lib/libutil/t_snprintb.c:1.17
--- src/tests/lib/libutil/t_snprintb.c:1.16	Fri Feb 16 01:19:53 2024
+++ src/tests/lib/libutil/t_snprintb.c	Fri Feb 16 18:09:16 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: t_snprintb.c,v 1.16 2024/02/16 01:19:53 rillig Exp $ */
+/* $NetBSD: t_snprintb.c,v 1.17 2024/02/16 18:09:16 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\
  The NetBSD Foundation, inc. All rights reserved.");
-__RCSID("$NetBSD: t_snprintb.c,v 1.16 2024/02/16 01:19:53 rillig Exp $");
+__RCSID("$NetBSD: t_snprintb.c,v 1.17 2024/02/16 18:09:16 rillig Exp $");
 
 #include <stdio.h>
 #include <string.h>
@@ -492,6 +492,15 @@ ATF_TC_BODY(snprintb, tc)
 	    0x77ff55,
 	    "0x77ff55<Field=0x77ff=other(77ff),other(77ff)>");
 
+	// new-style format, bit-field with 8 bits
+	h_snprintb(
+	    "\177\020"
+	    "F\010\010\0"
+		":\377all\0"
+		"*other\0",
+	    0xff00,
+	    "0xff00<other>");
+
 	// new-style format, bit-fields with no match
 	h_snprintb(
 	    "\177\020"

Reply via email to