diff --git a/common/lib/libutil/snprintb.c b/common/lib/libutil/snprintb.c index 09b08a73bfae..87c344b0531b 100644 --- a/common/lib/libutil/snprintb.c +++ b/common/lib/libutil/snprintb.c @@ -1,4 +1,4 @@ -/* $NetBSD: snprintb.c,v 1.47 2024/04/07 12:05:23 rillig Exp $ */ +/* $NetBSD: snprintb.c,v 1.48 2024/04/07 15:20:16 rillig Exp $ */ /*- * Copyright (c) 2002, 2024 The NetBSD Foundation, Inc. @@ -35,7 +35,7 @@ # include # if defined(LIBC_SCCS) -__RCSID("$NetBSD: snprintb.c,v 1.47 2024/04/07 12:05:23 rillig Exp $"); +__RCSID("$NetBSD: snprintb.c,v 1.48 2024/04/07 15:20:16 rillig Exp $"); # endif # include @@ -46,7 +46,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.47 2024/04/07 12:05:23 rillig Exp $"); # include # else /* ! _KERNEL */ # include -__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.47 2024/04/07 12:05:23 rillig Exp $"); +__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.48 2024/04/07 15:20:16 rillig Exp $"); # include # include # include @@ -133,15 +133,17 @@ old_style(state *s) while (*s->bitfmt != '\0') { const char *cur_bitfmt = s->bitfmt; uint8_t bit = *s->bitfmt; - if (bit > ' ') + if (bit > 32) + return -1; + if ((uint8_t)cur_bitfmt[1] <= 32) return -1; if (s->val & (1U << (bit - 1))) { store_delimiter(s); - while ((uint8_t)*++s->bitfmt > ' ') + while ((uint8_t)*++s->bitfmt > 32) store(s, *s->bitfmt); maybe_wrap_line(s, cur_bitfmt); } else - while ((uint8_t)*++s->bitfmt > ' ') + while ((uint8_t)*++s->bitfmt > 32) continue; } return 0; @@ -222,6 +224,7 @@ new_style(state *s) case '*': if (field_kind == 0) return -1; + field_kind = 0; if (cur_bitfmt[1] == '\0') return -1; s->bitfmt++; diff --git a/tests/lib/libutil/t_snprintb.c b/tests/lib/libutil/t_snprintb.c index cc6077ac1f83..e8cdcb01216e 100644 --- a/tests/lib/libutil/t_snprintb.c +++ b/tests/lib/libutil/t_snprintb.c @@ -1,4 +1,4 @@ -/* $NetBSD: t_snprintb.c,v 1.34 2024/04/07 12:05:23 rillig Exp $ */ +/* $NetBSD: t_snprintb.c,v 1.35 2024/04/07 15:20:17 rillig Exp $ */ /* * Copyright (c) 2002, 2004, 2008, 2010, 2024 The NetBSD Foundation, Inc. @@ -32,7 +32,7 @@ #include __COPYRIGHT("@(#) Copyright (c) 2008, 2010, 2024\ The NetBSD Foundation, inc. All rights reserved."); -__RCSID("$NetBSD: t_snprintb.c,v 1.34 2024/04/07 12:05:23 rillig Exp $"); +__RCSID("$NetBSD: t_snprintb.c,v 1.35 2024/04/07 15:20:17 rillig Exp $"); #include #include @@ -309,16 +309,16 @@ ATF_TC_BODY(snprintb, tc) // old style, empty description // - // Empty descriptions result in multiple commas in a row, which is a - // mistake. - h_snprintb( + // The description of a bit in the old format must not be empty, + // to prevent multiple commas in a row. + h_snprintb_val_error( "\020" "\001lsb" "\004" "\005" "\010msb", 0xff, - "0xff"); + "0xff"); - // new style unknown directive, at the beginning + // new style unknown conversion, at the beginning h_snprintb_val_error( "\177\020" "unknown\0", 0xff, "0xff#"); - // new style unknown directive, after a known directive + // new style unknown conversion, after a known conversion h_snprintb_val_error( "\177\020" "b\007msb\0" @@ -946,42 +946,51 @@ ATF_TC_BODY(snprintb, tc) // new style combinations, 'f' '*' '=' // - // After a catch-all '*' directive, any following '=' directive - // generates misleading output, which is a mistake. - h_snprintb( + // After a catch-all '*' conversions, there must not be further '=' + // conversions. + h_snprintb_val_error( "\177\020" "f\000\010f\0" "*=default\0" "=\245match\0", 0xa5, - "0xa5"); + "0xa5"); + "0xa5"); + "0xa5