Home | History | Annotate | Line # | Download | only in libutil
t_snprintb.c revision 1.27
      1 /* $NetBSD: t_snprintb.c,v 1.27 2024/02/22 21:04:24 rillig Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002, 2004, 2008, 2010, 2024 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code was contributed to The NetBSD Foundation by Christos Zoulas and
      8  * Roland Illig.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __COPYRIGHT("@(#) Copyright (c) 2008, 2010, 2024\
     34  The NetBSD Foundation, inc. All rights reserved.");
     35 __RCSID("$NetBSD: t_snprintb.c,v 1.27 2024/02/22 21:04:24 rillig Exp $");
     36 
     37 #include <stdio.h>
     38 #include <string.h>
     39 #include <util.h>
     40 #include <vis.h>
     41 
     42 #include <atf-c.h>
     43 
     44 static const char *
     45 vis_arr(const char *arr, size_t arrsize)
     46 {
     47 	static char buf[6][1024];
     48 	static size_t i;
     49 
     50 	i = (i + 1) % (sizeof(buf) / sizeof(buf[0]));
     51 	buf[i][0] = '"';
     52 	int rv = strnvisx(buf[i] + 1, sizeof(buf[i]) - 2, arr, arrsize,
     53 	    VIS_WHITE | VIS_OCTAL);
     54 	ATF_REQUIRE_MSG(rv >= 0, "strnvisx failed for size %zu", arrsize);
     55 	strcpy(buf[i] + 1 + rv, "\"");
     56 	return buf[i];
     57 }
     58 
     59 static void
     60 check_snprintb_m(const char *file, size_t line,
     61     size_t bufsize, const char *bitfmt, size_t bitfmtlen, uint64_t val,
     62     size_t line_max,
     63     int want_rv, const char *want_buf, size_t want_bufsize)
     64 {
     65 	char buf[1024];
     66 
     67 	ATF_REQUIRE(bufsize <= sizeof(buf));
     68 	ATF_REQUIRE(want_bufsize <= sizeof(buf));
     69 	if (bitfmtlen > 2 && bitfmt[0] == '\177')
     70 		ATF_REQUIRE_MSG(
     71 		    bitfmt[bitfmtlen - 1] == '\0',
     72 		    "%s:%zu: missing trailing '\\0' in new-style bitfmt",
     73 		    file, line);
     74 	if (bufsize == 0)
     75 		want_bufsize = 0;
     76 
     77 	memset(buf, 'Z', sizeof(buf));
     78 	int rv = snprintb_m(buf, bufsize, bitfmt, val, line_max);
     79 
     80 	size_t have_bufsize = sizeof(buf);
     81 	while (have_bufsize > 0 && buf[have_bufsize - 1] == 'Z')
     82 		have_bufsize--;
     83 	if (rv > 0 && (unsigned)rv < have_bufsize
     84 	    && buf[rv - 1] == '\0' && buf[rv] == '\0')
     85 		have_bufsize = rv + 1;
     86 	if (rv < 0)
     87 		for (size_t i = have_bufsize; i >= 2; i--)
     88 			if (buf[i - 2] == '\0' && buf[i - 1] == '\0')
     89 				have_bufsize = i;
     90 
     91 	ATF_CHECK_MSG(
     92 	    rv == want_rv
     93 	    && memcmp(buf, want_buf, want_bufsize) == 0
     94 	    && (line_max == 0 || have_bufsize < 2
     95 		|| buf[have_bufsize - 2] == '\0')
     96 	    && (have_bufsize < 1 || buf[have_bufsize - 1] == '\0'),
     97 	    "failed:\n"
     98 	    "\ttest case: %s:%zu\n"
     99 	    "\tformat: %s\n"
    100 	    "\tvalue: %#jx\n"
    101 	    "\tline_max: %zu\n"
    102 	    "\twant: %d bytes %s\n"
    103 	    "\thave: %d bytes %s\n",
    104 	    file, line,
    105 	    vis_arr(bitfmt, bitfmtlen),
    106 	    (uintmax_t)val,
    107 	    line_max,
    108 	    want_rv, vis_arr(want_buf, want_bufsize),
    109 	    rv, vis_arr(buf, have_bufsize));
    110 }
    111 
    112 #define	h_snprintb_m_len(bufsize, bitfmt, val, line_max,		\
    113 	    want_rv, want_buf)						\
    114 	check_snprintb_m(__FILE__, __LINE__,				\
    115 	    bufsize, bitfmt, sizeof(bitfmt) - 1, val, line_max,		\
    116 	    want_rv, want_buf, sizeof(want_buf))
    117 
    118 #define	h_snprintb(bitfmt, val, want_buf)				\
    119 	h_snprintb_m_len(1024, bitfmt, val, 0, sizeof(want_buf) - 1, want_buf)
    120 
    121 #define	h_snprintb_len(bufsize, bitfmt, val, want_rv, want_buf)		\
    122 	h_snprintb_m_len(bufsize, bitfmt, val, 0, want_rv, want_buf)
    123 
    124 #define	h_snprintb_error(bitfmt, want_buf)				\
    125 	h_snprintb_m_len(1024, bitfmt, 0, 0, -1, want_buf)
    126 
    127 #define	h_snprintb_m(bitfmt, val, line_max, want_buf)			\
    128 	h_snprintb_m_len(1024, bitfmt, val, line_max,			\
    129 	    sizeof(want_buf) - 1, want_buf)
    130 
    131 ATF_TC(snprintb);
    132 ATF_TC_HEAD(snprintb, tc)
    133 {
    134 	atf_tc_set_md_var(tc, "descr", "Checks snprintb(3)");
    135 }
    136 ATF_TC_BODY(snprintb, tc)
    137 {
    138 
    139 	// style and number base, old style, octal, zero value
    140 	//
    141 	// The value 0 does not get a leading '0'.
    142 	h_snprintb(
    143 	    "\010",
    144 	    0,
    145 	    "0");
    146 
    147 	// style and number base, old style, octal, nonzero value
    148 	//
    149 	// Nonzero octal values get a leading '0'.
    150 	h_snprintb(
    151 	    "\010",
    152 	    0xff,
    153 	    "0377");
    154 
    155 	// style and number base, old style, decimal, zero value
    156 	h_snprintb(
    157 	    "\012",
    158 	    0,
    159 	    "0");
    160 
    161 	// style and number base, old style, decimal, nonzero value
    162 	h_snprintb(
    163 	    "\012",
    164 	    0xff,
    165 	    "255");
    166 
    167 	// style and number base, old style, hexadecimal, zero value
    168 	//
    169 	// The value 0 does not get a leading '0x'.
    170 	h_snprintb(
    171 	    "\020",
    172 	    0,
    173 	    "0");
    174 
    175 	// style and number base, old style, hexadecimal, nonzero value
    176 	//
    177 	// Nonzero hexadecimal values get a leading '0x'.
    178 	h_snprintb(
    179 	    "\177\020",
    180 	    0xff,
    181 	    "0xff");
    182 
    183 	// style and number base, old style, invalid base 0
    184 	h_snprintb_error(
    185 	    "",
    186 	    "#");
    187 
    188 	// style and number base, old style, invalid base 2
    189 	h_snprintb_error(
    190 	    "\002",
    191 	    "#");
    192 
    193 	// style and number base, old style, invalid base 255 or -1
    194 	h_snprintb_error(
    195 	    "\377",
    196 	    "#");
    197 
    198 	// style and number base, new style, octal, zero value
    199 	//
    200 	// The value 0 does not get a leading '0'.
    201 	h_snprintb(
    202 	    "\177\010",
    203 	    0,
    204 	    "0");
    205 
    206 	// style and number base, new style, octal, nonzero value
    207 	//
    208 	// Nonzero octal values get a leading '0'.
    209 	h_snprintb(
    210 	    "\177\010",
    211 	    0xff,
    212 	    "0377");
    213 
    214 	// style and number base, new style, decimal, zero value
    215 	h_snprintb(
    216 	    "\177\012",
    217 	    0,
    218 	    "0");
    219 
    220 	// style and number base, new style, decimal, nonzero value
    221 	h_snprintb(
    222 	    "\177\012",
    223 	    0xff,
    224 	    "255");
    225 
    226 	// style and number base, new style, hexadecimal, zero value
    227 	//
    228 	// The value 0 does not get a leading '0x'.
    229 	h_snprintb(
    230 	    "\177\020",
    231 	    0,
    232 	    "0");
    233 
    234 	// style and number base, new style, hexadecimal, nonzero value
    235 	//
    236 	// Nonzero hexadecimal values get a leading '0x'.
    237 	h_snprintb(
    238 	    "\177\020",
    239 	    0xff,
    240 	    "0xff");
    241 
    242 	// style and number base, new style, invalid number base 0
    243 	h_snprintb_error(
    244 	    "\177",
    245 	    "#");
    246 
    247 	// style and number base, new style, invalid number base 2
    248 	h_snprintb_error(
    249 	    "\177\002",
    250 	    "#");
    251 
    252 	// style and number base, new style, invalid number base 255 or -1
    253 	h_snprintb_error(
    254 	    "\177\377",
    255 	    "#");
    256 
    257 	// old style, from lsb to msb
    258 	h_snprintb(
    259 	    "\020"
    260 	    "\001bit1"
    261 	    "\002bit2"
    262 	    "\037bit31"
    263 	    "\040bit32",
    264 	    0xffffffff80000001,
    265 	    "0xffffffff80000001<bit1,bit32>");
    266 
    267 	// old style, invalid bit number, at the beginning
    268 	h_snprintb_error(
    269 	    "\020"
    270 	    "\041invalid",
    271 	    "0#");
    272 
    273 	// old style, invalid bit number, in the middle
    274 	//
    275 	// The old-style format supports only 32 bits, interpreting the
    276 	// \041 as part of the text belonging to bit 1.
    277 	h_snprintb(
    278 	    "\020"
    279 	    "\001bit1"
    280 	    "\041bit33",
    281 	    0x1,
    282 	    "0x1<bit1!bit33>");
    283 
    284 	// old style, repeated bit numbers
    285 	//
    286 	// When a bit number is mentioned more than once,
    287 	// this is most likely a typo.
    288 	h_snprintb(
    289 	    "\020"
    290 	    "\001once"
    291 	    "\001again",
    292 	    0x1,
    293 	    "0x1<once,again>");
    294 
    295 	// old style, non-printable description
    296 	//
    297 	// The characters ' ' and '\t' are interpreted as bit numbers,
    298 	// not as part of the description; the visual arrangement is
    299 	// misleading.
    300 	h_snprintb(
    301 	    "\020"
    302 	    "\001least significant"
    303 	    "\002horizontal\ttab",
    304 	    0xff,
    305 	    "0xff<least,horizontal>");
    306 
    307 	// old style, empty description
    308 	//
    309 	// Empty descriptions result in multiple commas in a row, which is a
    310 	// mistake.
    311 	h_snprintb(
    312 	    "\020"
    313 	    "\001lsb"
    314 	    "\004"
    315 	    "\005"
    316 	    "\010msb",
    317 	    0xff,
    318 	    "0xff<lsb,,,msb>");
    319 
    320 	// old style, buffer size 0
    321 	//
    322 	// With the buffer size being 0, the buffer is not modified at all.
    323 	h_snprintb_len(
    324 	    0, "\020", 0,
    325 	    1, "");
    326 
    327 	// old style, buffer size 0, null buffer
    328 	//
    329 	// If the buffer size is 0, the buffer may be NULL.
    330 	int null_rv_old = snprintb(NULL, 0, "\020\001lsb", 0x01);
    331 	ATF_CHECK_MSG(
    332 	    null_rv_old == 8,
    333 	    "want length 8, have %d", null_rv_old);
    334 
    335 	// old style, buffer too small for value
    336 	h_snprintb_len(
    337 	    1, "\020", 0,
    338 	    1, "");
    339 
    340 	// old style, buffer large enough for zero value
    341 	h_snprintb_len(
    342 	    2, "\020", 0,
    343 	    1, "0");
    344 
    345 	// old style, buffer larger than necessary for zero value
    346 	h_snprintb_len(
    347 	    3, "\020", 0,
    348 	    1, "0");
    349 
    350 	// old style, buffer too small for nonzero value
    351 	h_snprintb_len(
    352 	    3, "\020", 7,
    353 	    3, "0x");
    354 
    355 	// old style, buffer large enough for nonzero value
    356 	h_snprintb_len(
    357 	    4, "\020", 7,
    358 	    3, "0x7");
    359 
    360 	// old style, buffer too small for '<'
    361 	h_snprintb_len(
    362 	    4, "\020\001lsb", 7,
    363 	    8, "0x7");
    364 
    365 	// old style, buffer too small for description
    366 	h_snprintb_len(
    367 	    7, "\020\001lsb", 7,
    368 	    8, "0x7<ls");
    369 
    370 	// old style, buffer too small for '>'
    371 	h_snprintb_len(
    372 	    8, "\020\001lsb", 7,
    373 	    8, "0x7<lsb");
    374 
    375 	// old style, buffer large enough for '<'
    376 	h_snprintb_len(
    377 	    9, "\020\001lsb", 7,
    378 	    8, "0x7<lsb>");
    379 
    380 	// old style, buffer too small for second description
    381 	h_snprintb_len(
    382 	    9, "\020\001one\002two", 7,
    383 	    12, "0x7<one,");
    384 
    385 	// old style, buffer too small for second description
    386 	h_snprintb_len(
    387 	    10, "\020\001one\002two", 7,
    388 	    12, "0x7<one,t");
    389 
    390 	// old style, buffer too small for '>' after second description
    391 	h_snprintb_len(
    392 	    12, "\020\001one\002two", 7,
    393 	    12, "0x7<one,two");
    394 
    395 	// old style, buffer large enough for '>' after second description
    396 	h_snprintb_len(
    397 	    13, "\020\001one\002two", 7,
    398 	    12, "0x7<one,two>");
    399 
    400 	// new style, buffer size 0, null buffer
    401 	//
    402 	// If the buffer size is 0, the buffer may be NULL.
    403 	int null_rv_new = snprintb(NULL, 0, "\177\020b\000lsb\0", 0x01);
    404 	ATF_CHECK_MSG(
    405 	    null_rv_new == 8,
    406 	    "want length 8, have %d", null_rv_new);
    407 
    408 	// new style single bits
    409 	h_snprintb(
    410 	    "\177\020"
    411 	    "b\000lsb\0"
    412 	    "b\001above-lsb\0"
    413 	    "b\037bit31\0"
    414 	    "b\040bit32\0"
    415 	    "b\076below-msb\0"
    416 	    "b\077msb\0",
    417 	    0x8000000180000001,
    418 	    "0x8000000180000001<lsb,bit31,bit32,msb>");
    419 
    420 	// new style single bits, duplicate bits
    421 	h_snprintb(
    422 	    "\177\020"
    423 	    "b\000lsb\0"
    424 	    "b\000lsb\0"
    425 	    "b\000lsb\0",
    426 	    0xff,
    427 	    "0xff<lsb,lsb,lsb>");
    428 
    429 	// new style single bits, empty description
    430 	h_snprintb(
    431 	    "\177\020"
    432 	    "b\000lsb\0"
    433 	    "b\001\0"
    434 	    "b\002\0"
    435 	    "b\007msb\0"
    436 	    ,
    437 	    0xff,
    438 	    "0xff<lsb,,,msb>");
    439 
    440 	// new style single bits, bit number too large
    441 	h_snprintb_error(
    442 	    "\177\020"
    443 	    "b\100too-high\0",
    444 	    "0#");
    445 	h_snprintb_error(
    446 	    "\177\020"
    447 	    "b\377too-high\0",
    448 	    "0#");
    449 
    450 	// new style single bits, non-printable description
    451 	//
    452 	// Contrary to the old-style format, the new-style format allows
    453 	// arbitrary characters in the description, even control characters
    454 	// and non-ASCII characters.
    455 	h_snprintb(
    456 	    "\177\020"
    457 	    "b\000space \t \xC3\xA4\0",
    458 	    0x1,
    459 	    "0x1<space \t \xC3\xA4>");
    460 
    461 	// new style named bit-field, octal
    462 	//
    463 	// The bit-field value gets a leading '0' iff it is nonzero.
    464 	h_snprintb(
    465 	    "\177\010"
    466 	    "f\000\010byte0\0"
    467 	    "f\010\010byte1\0",
    468 	    0x0100,
    469 	    "0400<byte0=0,byte1=01>");
    470 
    471 	// new style named bit-field, decimal
    472 	h_snprintb(
    473 	    "\177\012"
    474 	    "f\000\010byte0\0"
    475 	    "f\010\010byte1\0",
    476 	    0x0100,
    477 	    "256<byte0=0,byte1=1>");
    478 
    479 	// new style named bit-field, hexadecimal
    480 	h_snprintb(
    481 	    "\177\020"
    482 	    "f\000\010byte0\0"
    483 	    "f\010\010byte1\0",
    484 	    0x0100,
    485 	    "0x100<byte0=0,byte1=0x1>");
    486 
    487 	// new style bit-field, from 0 width 0
    488 	h_snprintb(
    489 	    "\177\020"
    490 	    "f\000\000zero-width\0"
    491 		"=\000zero\0",
    492 	    0xffff,
    493 	    "0xffff<zero-width=0=zero>");
    494 
    495 	// new style bit-field, from 0 width 1
    496 	h_snprintb(
    497 	    "\177\020"
    498 	    "f\000\001lsb\0"
    499 		"=\000zero\0"
    500 		"=\001one\0",
    501 	    0x0,
    502 	    "0<lsb=0=zero>");
    503 	h_snprintb(
    504 	    "\177\020"
    505 	    "f\000\001lsb\0"
    506 		"=\000zero\0"
    507 		"=\001one\0",
    508 	    0x1,
    509 	    "0x1<lsb=0x1=one>");
    510 
    511 	// new style bit-field, from 0 width 63
    512 	h_snprintb(
    513 	    "\177\020"
    514 	    "f\000\077uint63\0"
    515 		"=\125match\0",
    516 	    0xaaaa5555aaaa5555,
    517 	    "0xaaaa5555aaaa5555<uint63=0x2aaa5555aaaa5555>");
    518 
    519 	// new style bit-field, from 0 width 64
    520 	h_snprintb(
    521 	    "\177\020"
    522 	    "f\000\100uint64\0"
    523 		"=\125match\0",
    524 	    0xaaaa5555aaaa5555,
    525 	    "0xaaaa5555aaaa5555<uint64=0xaaaa5555aaaa5555>");
    526 
    527 	// new style bit-field, from 0 width 65
    528 	h_snprintb_error(
    529 	    "\177\020"
    530 	    "f\000\101uint65\0",
    531 	    "0#");
    532 
    533 	// new style bit-field, from 1 width 8
    534 	h_snprintb(
    535 	    "\177\020"
    536 	    "f\001\010uint8\0"
    537 		"=\203match\0",
    538 	    0x0106,
    539 	    "0x106<uint8=0x83=match>");
    540 
    541 	// new style bit-field, from 1 width 9
    542 	//
    543 	// The '=' and ':' directives can only match a bit-field value between
    544 	// 0 and 255, independent of the bit-field's width.
    545 	h_snprintb(
    546 	    "\177\020"
    547 	    "f\001\011uint9\0"
    548 		"=\203match\0"
    549 		"*=other-f\0"
    550 	    "F\001\011\0"
    551 		":\203match\0"
    552 		"*other-F\0",
    553 	    0x0306,
    554 	    "0x306<uint9=0x183=other-f,other-F>");
    555 
    556 	// new style bit-field, from 32 width 32
    557 	h_snprintb(
    558 	    "\177\020"
    559 	    "f\040\040uint32\0",
    560 	    0xaaaa555500000000,
    561 	    "0xaaaa555500000000<uint32=0xaaaa5555>");
    562 
    563 	// new style bit-field, from 60 width 4
    564 	h_snprintb(
    565 	    "\177\020"
    566 	    "f\074\004uint4\0",
    567 	    0xf555555555555555,
    568 	    "0xf555555555555555<uint4=0xf>");
    569 
    570 	// new style bit-field, from 60 width 5
    571 	//
    572 	// The end of the bit-field is out of bounds.
    573 	h_snprintb(
    574 	    "\177\020"
    575 	    "f\074\005uint5\0",
    576 	    0xf555555555555555,
    577 	    "0xf555555555555555<uint5=0xf>");
    578 
    579 	// new style bit-field, from 64 width 0
    580 	//
    581 	// The beginning of the bit-field is out of bounds, the end is fine.
    582 	h_snprintb_error(
    583 	    "\177\020"
    584 	    "f\100\000uint0\0",
    585 	    "0#");
    586 
    587 	// new style bit-field, from 65 width 0
    588 	//
    589 	// The beginning and end of the bit-field are out of bounds.
    590 	h_snprintb_error(
    591 	    "\177\020"
    592 	    "f\101\000uint0\0",
    593 	    "0#");
    594 
    595 	// new style bit-field, empty field description
    596 	//
    597 	// The description of a field may be empty, though this is probably a
    598 	// mistake, as it outputs an isolated '='.
    599 	h_snprintb(
    600 	    "\177\020"
    601 	    "f\000\004\0"
    602 		"=\001one\0",
    603 	    0x1,
    604 	    "0x1<=0x1=one>");
    605 
    606 	// new style bit-field, non-printable description
    607 	//
    608 	// Contrary to the old-style format, the new-style format allows
    609 	// arbitrary characters in the description, even control characters
    610 	// and non-ASCII characters.
    611 	h_snprintb(
    612 	    "\177\020"
    613 	    "f\000\010\t \xC3\xA4\0"
    614 		"=\001\t \xC3\xA4\0"
    615 	    "F\000\010\0"
    616 		":\001\t \xC3\xA4\0"
    617 	    "F\000\010\0"
    618 		"*\t \xC3\xA4\0",
    619 	    0x1,
    620 	    "0x1<\t \xC3\xA4=0x1=\t \xC3\xA4,\t \xC3\xA4,\t \xC3\xA4>");
    621 
    622 	// new style bit-field, '=' with empty description
    623 	//
    624 	// The description of a '=' directive may be empty, though this is
    625 	// probably a mistake, as it outputs several '=' in a row.
    626 	h_snprintb(
    627 	    "\177\020"
    628 	    "f\000\004f\0"
    629 		"=\001one\0"
    630 		"=\001\0"
    631 		"=\001\0",
    632 	    0x1,
    633 	    "0x1<f=0x1=one==>");
    634 
    635 	// new style bit-field, 'F' followed by ':' with empty description
    636 	//
    637 	// The description of a ':' directive may be empty, though this is
    638 	// probably a mistake, as it leads to empty angle brackets.
    639 	h_snprintb(
    640 	    "\177\020"
    641 	    "F\000\004\0"
    642 		":\001\0"
    643 		"*default\0",
    644 	    0x1,
    645 	    "0x1<>");
    646 
    647 	// new style bit-field, 'F', ':' with empty description, '*'
    648 	//
    649 	// The ':' directive could be used to suppress a following '*'
    650 	// directive, but this combination is probably a mistake, as a
    651 	// matching ':' leads to empty angle brackets.
    652 	h_snprintb(
    653 	    "\177\020"
    654 	    "F\000\004\0"
    655 		":\001\0"
    656 		"*default\0",
    657 	    0x2,
    658 	    "0x2<default>");
    659 
    660 	// new style bit-field, 'f' with non-exhaustive '='
    661 	h_snprintb(
    662 	    "\177\020"
    663 	    "f\000\004Field\0"
    664 		"=\1one\0"
    665 		"=\2two\0",
    666 	    0x3,
    667 	    "0x3<Field=0x3>");
    668 
    669 	// new style bit-field, 'F' with non-exhaustive ':'
    670 	//
    671 	// A bit-field that does not match any values still generates empty
    672 	// angle brackets.
    673 	h_snprintb(
    674 	    "\177\020"
    675 	    "F\000\004\0"
    676 		":\1one\0"
    677 		":\2two\0",
    678 	    0x3,
    679 	    "0x3<>");
    680 
    681 	// new style bit-field, 'F' with non-exhaustive ':'
    682 	//
    683 	// A bit-field that does not match any values still generates empty
    684 	// angle brackets or adjacent commas.
    685 	h_snprintb(
    686 	    "\177\020"
    687 	    "b\000bit0\0"
    688 	    "F\000\004\0"
    689 		":\1one\0"
    690 		":\2two\0"
    691 	    "b\001bit1\0",
    692 	    0x3,
    693 	    "0x3<bit0,,bit1>");
    694 
    695 	// new style bit-field, '=', can never match
    696 	h_snprintb(
    697 	    "\177\020"
    698 	    "f\000\007f\0"
    699 		"=\200never\0"
    700 		"=\377never\0",
    701 	    0xff,
    702 	    "0xff<f=0x7f>");
    703 
    704 	// new style, two separate bit-fields
    705 	h_snprintb(
    706 	    "\177\020"
    707 	    "f\000\004f1\0"
    708 		"=\001one\0"
    709 		"=\002two\0"
    710 	    "f\004\004f2\0"
    711 		"=\001one\0"
    712 		"=\002two\0",
    713 	    0x12,
    714 	    "0x12<f1=0x2=two,f2=0x1=one>");
    715 
    716 	// new style, mixed named and unnamed bit-fields
    717 	h_snprintb(
    718 	    "\177\020"
    719 	    "f\000\004f1\0"
    720 		"=\001one\0"
    721 		"=\002two\0"
    722 	    "F\010\004\0"
    723 		":\015thirteen\0"
    724 	    "f\004\004f2\0"
    725 		"=\001one\0"
    726 		"=\002two\0",
    727 	    0x0d12,
    728 	    "0xd12<f1=0x2=two,thirteen,f2=0x1=one>");
    729 
    730 	// new style bit-field, overlapping
    731 	h_snprintb(
    732 	    "\177\020"
    733 	    "f\000\004lo\0"
    734 	    "f\002\004mid\0"
    735 	    "f\004\004hi\0"
    736 	    "f\000\010all\0",
    737 	    0x18,
    738 	    "0x18<lo=0x8,mid=0x6,hi=0x1,all=0x18>");
    739 
    740 	// new style bit-field, difference between '=' and ':'
    741 	//
    742 	// The ':' directive can almost emulate the '=' directive, without the
    743 	// numeric output and with a different separator. It's best to use
    744 	// either 'f' with '=' or 'F' with ':', but not mix them.
    745 	h_snprintb(
    746 	    "\177\020"
    747 	    "f\000\004field\0"
    748 		"=\010value\0"
    749 	    "F\000\000\0"
    750 		":\000field\0"	// Since the description of 'F' is ignored.
    751 	    "F\000\004\0"
    752 		":\010value\0",
    753 	    0x18,
    754 	    "0x18<field=0x8=value,field,value>");
    755 
    756 	// new style bit-field default, fixed string
    757 	//
    758 	// The 'f' directive pairs up with the '=' directive,
    759 	// the 'F' directive pairs up with the ':' directive,
    760 	// but there's only one 'default' directive for both variants,
    761 	// so its description should include the '=' when used with 'f' but
    762 	// not with 'F'.
    763 	h_snprintb(
    764 	    "\177\020"
    765 	    "f\030\010f1\0"
    766 		"*default\0"
    767 	    "f\020\010f2\0"
    768 		"*=default\0"
    769 	    "F\010\010\0"
    770 		"*default\0"
    771 	    "F\010\010\0"
    772 		"*=default\0",
    773 	    0x11223344,
    774 	    "0x11223344<f1=0x11default,f2=0x22=default,default,=default>");
    775 
    776 	// new style bit-field default, numeric conversion specifier
    777 	h_snprintb(
    778 	    "\177\020"
    779 	    "f\010\010f\0"
    780 		"*=f(%ju)\0"
    781 	    "F\000\010F\0"
    782 		"*F(%ju)\0",
    783 	    0x1122,
    784 	    "0x1122<f=0x11=f(17),F(34)>");
    785 
    786 	// new style bit-field default, can never match
    787 	h_snprintb(
    788 	    "\177\020"
    789 	    "f\010\002f\0"
    790 		"=\000zero\0"
    791 		"=\001one\0"
    792 		"=\002two\0"
    793 		"=\003three\0"
    794 		"*other\0",
    795 	    0xff00,
    796 	    "0xff00<f=0x3=three>");
    797 
    798 	// new style bit-field default, invalid conversion specifier
    799 	//
    800 	// There is no reliable way to make snprintf return an error, as such
    801 	// errors are defined as undefined behavior in the C standard.
    802 	// Instead, here's a conversion specifier that produces a literal '%'.
    803 	h_snprintb(
    804 	    "\177\020"
    805 	    "f\000\010f\0"
    806 		"*=%030ju%%\0",
    807 	    0xff,
    808 	    "0xff<f=0xff=000000000000000000000000000255%>");
    809 
    810 	// new style unknown directive
    811 	//
    812 	// Unknown directives are assumed to have a single byte argument
    813 	// followed by a description; they are skipped up to the next '\0'.
    814 	h_snprintb(
    815 	    "\177\020"
    816 	    "c\010ignored\0"
    817 	    "c\000b\0"
    818 	    "lsb\0"
    819 	    "b\007msb\0",
    820 	    0xff,
    821 	    "0xff<msb>");
    822 
    823 	// new style combinations, 'b' '='
    824 	//
    825 	// A '=' directive without a preceding 'f' or 'F' directive applies to
    826 	// the whole value; its description may appear inside or outside the
    827 	// angle brackets. Having such a format is likely an error.
    828 	h_snprintb(
    829 	    "\177\020"
    830 	    "b\004bit4\0"
    831 		"=\000clear\0"
    832 		"=\001set\0"
    833 		"=\245complete\0"
    834 	    "b\000bit0\0"
    835 		"=\000clear\0"
    836 		"=\001set\0"
    837 		"=\245complete\0",
    838 	    0xa5,
    839 	    "0xa5=complete<bit0=complete>");
    840 
    841 	// new style combinations, 'b' ':'
    842 	//
    843 	// A ':' directive without a preceding 'f' or 'F' directive applies to
    844 	// the whole value; its description may appear inside or outside the
    845 	// angle brackets. Having such a format is likely an error.
    846 	h_snprintb(
    847 	    "\177\020"
    848 	    "b\004bit4\0"
    849 		":\000clear\0"
    850 		":\001set\0"
    851 		":\245complete\0"
    852 	    "b\000bit0\0"
    853 		":\000clear\0"
    854 		":\001set\0"
    855 		":\245complete\0",
    856 	    0xa5,
    857 	    "0xa5complete<bit0complete>");
    858 
    859 	// new style combinations, 'b' '*'
    860 	//
    861 	// A '*' directive without a preceding 'f' or 'F' directive is ignored.
    862 	h_snprintb(
    863 	    "\177\020"
    864 	    "b\004bit4\0"
    865 		"*default(%ju)\0"
    866 	    "b\000bit0\0"
    867 		"*default(%ju)\0",
    868 	    0xa5,
    869 	    "0xa5<bit0>");
    870 
    871 	// new style combinations, 'f' 'b' '='
    872 	//
    873 	// Between an 'f' and an '=' directive, there may be unrelated 'b'
    874 	// directives, they do not affect the value of the "previous field".
    875 	// Formats like these are probably mistakes.
    876 	h_snprintb(
    877 	    "\177\020"
    878 	    "f\000\010f\0"
    879 	    "b\005bit5\0"
    880 		"=\xa5match\0",
    881 	    0xa5,
    882 	    "0xa5<f=0xa5,bit5=match>");
    883 
    884 	// new style combinations, 'f' 'b' ':'
    885 	//
    886 	// Between an 'f' and a ':' directive, there may be unrelated 'b'
    887 	// directives, they do not affect the value of the "previous field".
    888 	// Formats like these are mistakes, as the output is garbled.
    889 	h_snprintb(
    890 	    "\177\020"
    891 	    "f\000\010f\0"
    892 	    "b\005bit5\0"
    893 		":\xa5match\0",
    894 	    0xa5,
    895 	    "0xa5<f=0xa5,bit5match>");
    896 
    897 	// new style combinations, 'f' ':'
    898 	h_snprintb(
    899 	    "\177\20"
    900 	    "f\000\004nibble\0"
    901 		":\001one\0",
    902 	    0x01,
    903 	    "0x1<nibble=0x1one>");
    904 
    905 	// new style combinations, 'F' '='
    906 	//
    907 	// Combining the 'F' and '=' directives outputs an isolated '=', which
    908 	// doesn't look well-formed.
    909 	h_snprintb(
    910 	    "\177\20"
    911 	    "F\000\004\0"
    912 		"=\001one\0",
    913 	    0x01,
    914 	    "0x1<=one>");
    915 
    916 	// new style combinations, '='
    917 	//
    918 	// A '=' directive without a preceding 'f' or 'F' directive matches on
    919 	// the complete value. This is not documented in the manual page, and
    920 	// formats like these are probably mistakes.
    921 	h_snprintb(
    922 	    "\177\020"
    923 		"=\xa5match\0",
    924 	    0xa5,
    925 	    "0xa5=match");
    926 
    927 	// new style combinations, ':'
    928 	//
    929 	// A ':' directive without a preceding 'f' or 'F' directive matches on
    930 	// the complete value. This is not documented in the manual page, and
    931 	// formats like these are probably mistakes.
    932 	h_snprintb(
    933 	    "\177\020"
    934 		":\xa5match\0",
    935 	    0xa5,
    936 	    "0xa5match");
    937 
    938 	// new style combinations, '*'
    939 	//
    940 	// A '*' directive without a preceding 'f' or 'F' is skipped. Formats
    941 	// like these are mistakes.
    942 	h_snprintb(
    943 	    "\177\020"
    944 		"*match\0",
    945 	    0xa5,
    946 	    "0xa5");
    947 
    948 	// new style combinations, 'f' '*' '='
    949 	//
    950 	// A '*' directive may be followed by a '=' directive. Formats like
    951 	// this are probably a mistake.
    952 	h_snprintb(
    953 	    "\177\020"
    954 	    "f\000\010f\0"
    955 		"*=default\0"
    956 		"=\xa5match\0",
    957 	    0xa5,
    958 	    "0xa5<f=0xa5=default=match>");
    959 
    960 	// new style combinations, 'F' '*' ':'
    961 	//
    962 	// A '*' directive may be followed by a ':' directive. Formats like
    963 	// this are probably a mistake.
    964 	h_snprintb(
    965 	    "\177\020"
    966 	    "F\000\010F\0"
    967 		"*default\0"
    968 		":\xa5-match\0",
    969 	    0xa5,
    970 	    "0xa5<default-match>");
    971 
    972 	// new style combinations, '*' '*'
    973 	//
    974 	// The first '*' directive matches everything, so the second '*'
    975 	// directive cannot match anything and is thus redundant. Formats like
    976 	// this are a mistake.
    977 	h_snprintb(
    978 	    "\177\020"
    979 	    "f\000\010f\0"
    980 		"*=default-f\0"
    981 		"*ignored\0"
    982 	    "F\000\010\0"
    983 		"*default-F\0"
    984 		"*ignored\0",
    985 	    0xa5,
    986 	    "0xa5<f=0xa5=default-f,default-F>");
    987 
    988 	// manual page, old style octal
    989 	h_snprintb(
    990 	    "\10\2BITTWO\1BITONE",
    991 	    3,
    992 	    "03<BITTWO,BITONE>");
    993 
    994 	// manual page, old style hexadecimal
    995 	h_snprintb(
    996 	    "\20"
    997 	    "\x10NOTBOOT" "\x0f""FPP" "\x0eSDVMA"
    998 	    "\x0cVIDEO" "\x0bLORES" "\x0a""FPA" "\x09""DIAG"
    999 	    "\x07""CACHE" "\x06IOCACHE" "\x05LOOPBACK"
   1000 	    "\x04""DBGCACHE",
   1001 	    0xe860,
   1002 	    "0xe860<NOTBOOT,FPP,SDVMA,VIDEO,CACHE,IOCACHE>");
   1003 
   1004 	// manual page, new style bits and fields
   1005 	h_snprintb(
   1006 	    "\177\020"
   1007 	    "b\0LSB\0" "b\1BITONE\0"
   1008 	    "f\4\4NIBBLE2\0"
   1009 	    "f\x10\4BURST\0" "=\4FOUR\0" "=\xf""FIFTEEN\0"
   1010 	    "b\x1fMSB\0",
   1011 	    0x800f0701,
   1012 	    "0x800f0701<LSB,NIBBLE2=0,BURST=0xf=FIFTEEN,MSB>");
   1013 
   1014 	// manual page, new style mmap
   1015 #define	MAP_FMT				\
   1016 	"\177\020"			\
   1017 	"b\0"  "SHARED\0"		\
   1018 	"b\1"  "PRIVATE\0"		\
   1019 	"b\2"  "COPY\0"			\
   1020 	"b\4"  "FIXED\0"		\
   1021 	"b\5"  "RENAME\0"		\
   1022 	"b\6"  "NORESERVE\0"		\
   1023 	"b\7"  "INHERIT\0"		\
   1024 	"b\11" "HASSEMAPHORE\0"		\
   1025 	"b\12" "TRYFIXED\0"		\
   1026 	"b\13" "WIRED\0"		\
   1027 	"F\14\1\0"			\
   1028 		":\0" "FILE\0"		\
   1029 		":\1" "ANONYMOUS\0"	\
   1030 	"b\15" "STACK\0"		\
   1031 	"F\30\010\0"			\
   1032 		":\000" "ALIGN=NONE\0"	\
   1033 		":\015" "ALIGN=8KB\0"	\
   1034 		"*"     "ALIGN=2^%ju\0"
   1035 	h_snprintb(
   1036 	    MAP_FMT,
   1037 	    0x0d001234,
   1038 	    "0xd001234<COPY,FIXED,RENAME,HASSEMAPHORE,ANONYMOUS,ALIGN=8KB>");
   1039 	h_snprintb(
   1040 	    MAP_FMT,
   1041 	    0x2e000000,
   1042 	    "0x2e000000<FILE,ALIGN=2^46>");
   1043 
   1044 	// It is possible but cumbersome to implement a reduced variant of
   1045 	// rot13 using snprintb, shown here for lowercase letters only.
   1046 	for (char ch = 'A'; ch <= '~'; ch++) {
   1047 		char rot13 = ch >= 'a' && ch <= 'm' ? ch + 13
   1048 		    : ch >= 'n' && ch <= 'z' ? ch - 13
   1049 		    : '?';
   1050 		char expected[8];
   1051 		ATF_REQUIRE_EQ(7,
   1052 		    snprintf(expected, sizeof(expected), "%#x<%c>", ch, rot13));
   1053 		h_snprintb(
   1054 		    "\177\020"
   1055 		    "F\000\010\0"
   1056 		    ":an\0:bo\0:cp\0:dq\0:er\0:fs\0:gt\0:hu\0"
   1057 		    ":iv\0:jw\0:kx\0:ly\0:mz\0"
   1058 		    ":na\0:ob\0:pc\0:qd\0:re\0:sf\0:tg\0:uh\0"
   1059 		    ":vi\0:wj\0:xk\0:yl\0:zm\0"
   1060 		    // If snprintf accepted "%jc", it would be possible to
   1061 		    // echo the non-alphabetic characters instead of a
   1062 		    // catchall question mark.
   1063 		    "*?\0",
   1064 		    ch,
   1065 		    expected);
   1066 	}
   1067 
   1068 	// new style, small buffers
   1069 	h_snprintb_len(
   1070 	    0, "\177\020", 0,
   1071 	    1, "");
   1072 	h_snprintb_len(
   1073 	    1, "\177\020", 0,
   1074 	    1, "");
   1075 	h_snprintb_len(
   1076 	    2, "\177\020", 0,
   1077 	    1, "0");
   1078 	h_snprintb_len(
   1079 	    3, "\177\020", 0,
   1080 	    1, "0");
   1081 	h_snprintb_len(
   1082 	    3, "\177\020", 7,
   1083 	    3, "0x");
   1084 	h_snprintb_len(
   1085 	    4, "\177\020", 7,
   1086 	    3, "0x7");
   1087 	h_snprintb_len(
   1088 	    7, "\177\020b\000lsb\0", 7,
   1089 	    8, "0x7<ls");
   1090 	h_snprintb_len(
   1091 	    8, "\177\020b\000lsb\0", 7,
   1092 	    8, "0x7<lsb");
   1093 	h_snprintb_len(
   1094 	    9, "\177\020b\000lsb\0", 7,
   1095 	    8, "0x7<lsb>");
   1096 	h_snprintb_len(
   1097 	    9, "\177\020b\000one\0b\001two\0", 7,
   1098 	    12, "0x7<one,");
   1099 	h_snprintb_len(
   1100 	    10, "\177\020b\000one\0b\001two\0", 7,
   1101 	    12, "0x7<one,t");
   1102 	h_snprintb_len(
   1103 	    12, "\177\020b\000one\0b\001two\0", 7,
   1104 	    12, "0x7<one,two");
   1105 	h_snprintb_len(
   1106 	    13, "\177\020b\000one\0b\001two\0", 7,
   1107 	    12, "0x7<one,two>");
   1108 }
   1109 
   1110 ATF_TC(snprintb_m);
   1111 ATF_TC_HEAD(snprintb_m, tc)
   1112 {
   1113 	atf_tc_set_md_var(tc, "descr", "Checks snprintb_m(3)");
   1114 }
   1115 ATF_TC_BODY(snprintb_m, tc)
   1116 {
   1117 
   1118 	// old style, line_max exceeded by number in line 1
   1119 	h_snprintb_m(
   1120 	    "\020",
   1121 	    0xff,
   1122 	    1,
   1123 	    "#\0");
   1124 
   1125 	// old style, line_max exceeded by '<' in line 1
   1126 	h_snprintb_m(
   1127 	    "\020"
   1128 	    "\001lsb",
   1129 	    0xff,
   1130 	    4,
   1131 	    "0xf#\0");
   1132 
   1133 	// old style, line_max exceeded by description
   1134 	h_snprintb_m(
   1135 	    "\020"
   1136 	    "\001bit1"
   1137 	    "\002bit2",
   1138 	    0xff,
   1139 	    7,
   1140 	    "0xff<b#\0"
   1141 	    "0xff<b#\0");
   1142 
   1143 	// old style, line_max exceeded by '>' in line 1
   1144 	h_snprintb_m(
   1145 	    "\020"
   1146 	    "\001bit1"
   1147 	    "\0022",
   1148 	    0xff,
   1149 	    9,
   1150 	    "0xff<bit#\0"
   1151 	    "0xff<2>\0");
   1152 
   1153 	// old style, line_max exceeded by description in line 2
   1154 	h_snprintb_m(
   1155 	    "\020"
   1156 	    "\0011"
   1157 	    "\002bit2",
   1158 	    0xff,
   1159 	    8,
   1160 	    "0xff<1>\0"
   1161 	    "0xff<bi#\0");
   1162 
   1163 	// old style, line_max exceeded by '>' in line 2
   1164 	h_snprintb_m(
   1165 	    "\020"
   1166 	    "\0011"
   1167 	    "\002bit2",
   1168 	    0xff,
   1169 	    9,
   1170 	    "0xff<1>\0"
   1171 	    "0xff<bit#\0");
   1172 
   1173 	// old style complete
   1174 	h_snprintb_m(
   1175 	    "\020"
   1176 	    "\0011"
   1177 	    "\002bit2",
   1178 	    0xff,
   1179 	    10,
   1180 	    "0xff<1>\0"
   1181 	    "0xff<bit2>\0");
   1182 
   1183 	// new style, line_max exceeded by value in line 1
   1184 	h_snprintb_m(
   1185 	    "\177\020",
   1186 	    0xff,
   1187 	    1,
   1188 	    "#\0");
   1189 
   1190 	// new style, line_max exceeded by single-bit '<' in line 1
   1191 	h_snprintb_m(
   1192 	    "\177\020"
   1193 	    "b\000bit\0",
   1194 	    0xff,
   1195 	    4,
   1196 	    "0xf#\0");
   1197 
   1198 	// new style, line_max exceeded by single-bit description in line 1
   1199 	h_snprintb_m(
   1200 	    "\177\020"
   1201 	    "b\000bit0\0"
   1202 	    "b\001two\0",
   1203 	    0xff,
   1204 	    8,
   1205 	    "0xff<bi#\0"
   1206 	    "0xff<tw#\0");
   1207 
   1208 	// new style, line_max exceeded by single-bit '>' in line 1
   1209 	h_snprintb_m(
   1210 	    "\177\020"
   1211 	    "b\000bit0\0"
   1212 	    "b\001two\0",
   1213 	    0xff,
   1214 	    9,
   1215 	    "0xff<bit#\0"
   1216 	    "0xff<two>\0");
   1217 
   1218 	// new style, line_max exceeded by single-bit description in line 2
   1219 	h_snprintb_m(
   1220 	    "\177\020"
   1221 	    "b\000one\0"
   1222 	    "b\001three\0",
   1223 	    0xff,
   1224 	    9,
   1225 	    "0xff<one>\0"
   1226 	    "0xff<thr#\0");
   1227 
   1228 	// new style, line_max exceeded by single-bit '>' in line 2
   1229 	h_snprintb_m(
   1230 	    "\177\020"
   1231 	    "b\000one\0"
   1232 	    "b\001four\0",
   1233 	    0xff,
   1234 	    9,
   1235 	    "0xff<one>\0"
   1236 	    "0xff<fou#\0");
   1237 
   1238 	// new style, single-bit complete
   1239 	h_snprintb_m(
   1240 	    "\177\020"
   1241 	    "b\000one\0"
   1242 	    "b\001three\0",
   1243 	    0xff,
   1244 	    11,
   1245 	    "0xff<one>\0"
   1246 	    "0xff<three>\0");
   1247 
   1248 	// new style, line_max exceeded by named bit-field number in line 1
   1249 	h_snprintb_m(
   1250 	    "\177\020"
   1251 	    "f\000\004lo\0",
   1252 	    0xff,
   1253 	    3,
   1254 	    "0x#\0");
   1255 
   1256 	// new style, line_max exceeded by named bit-field '<' in line 1
   1257 	h_snprintb_m(
   1258 	    "\177\020"
   1259 	    "f\000\004lo\0",
   1260 	    0xff,
   1261 	    4,
   1262 	    "0xf#\0");
   1263 
   1264 	// new style, line_max exceeded by named bit-field field description
   1265 	// in line 1
   1266 	h_snprintb_m(
   1267 	    "\177\020"
   1268 	    "f\000\004lo\0",
   1269 	    0xff,
   1270 	    6,
   1271 	    "0xff<#\0");
   1272 
   1273 	// new style, line_max exceeded by named bit-field '=' in line 1
   1274 	h_snprintb_m(
   1275 	    "\177\020"
   1276 	    "f\000\004lo\0",
   1277 	    0xff,
   1278 	    7,
   1279 	    "0xff<l#\0");
   1280 
   1281 	// new style, line_max exceeded by named bit-field value in line 1
   1282 	h_snprintb_m(
   1283 	    "\177\020"
   1284 	    "f\000\004lo\0",
   1285 	    0xff,
   1286 	    10,
   1287 	    "0xff<lo=0#\0");
   1288 
   1289 	// new style, line_max exceeded by named bit-field '=' in line 1
   1290 	h_snprintb_m(
   1291 	    "\177\020"
   1292 	    "f\000\004lo\0"
   1293 		"=\017match\0",
   1294 	    0xff,
   1295 	    11,
   1296 	    "0xff<lo=0x#\0");
   1297 
   1298 	// new style, line_max exceeded by named bit-field value description in
   1299 	// line 1
   1300 	h_snprintb_m(
   1301 	    "\177\020"
   1302 	    "f\000\004lo\0"
   1303 		"=\017match\0",
   1304 	    0xff,
   1305 	    16,
   1306 	    "0xff<lo=0xf=mat#\0");
   1307 
   1308 	// new style, line_max exceeded by named bit-field '>' in line 1
   1309 	h_snprintb_m(
   1310 	    "\177\020"
   1311 	    "f\000\004lo\0"
   1312 		"=\017match\0",
   1313 	    0xff,
   1314 	    17,
   1315 	    "0xff<lo=0xf=matc#\0");
   1316 
   1317 	// new style, line_max exceeded by named bit-field description in line 2
   1318 	h_snprintb_m(
   1319 	    "\177\020"
   1320 	    "f\000\004lo\0"
   1321 	    "f\000\004low-bits\0"
   1322 		"=\017match\0",
   1323 	    0xff,
   1324 	    12,
   1325 	    "0xff<lo=0xf>\0"
   1326 	    "0xff<low-bi#\0");
   1327 
   1328 	// new style, line_max exceeded by named bit-field '=' in line 2
   1329 	h_snprintb_m(
   1330 	    "\177\020"
   1331 	    "f\000\004lo\0"
   1332 	    "f\000\004low-bits\0"
   1333 		"=\017match\0",
   1334 	    0xff,
   1335 	    13,
   1336 	    "0xff<lo=0xf>\0"
   1337 	    "0xff<low-bit#\0");
   1338 
   1339 	// new style, line_max exceeded by named bit-field value in line 2
   1340 	h_snprintb_m(
   1341 	    "\177\020"
   1342 	    "f\000\004lo\0"
   1343 	    "f\000\004low-bits\0"
   1344 		"=\017match\0",
   1345 	    0xff,
   1346 	    16,
   1347 	    "0xff<lo=0xf>\0"
   1348 	    "0xff<low-bits=0#\0");
   1349 
   1350 	// new style, line_max exceeded by named bit-field '=' in line 2
   1351 	h_snprintb_m(
   1352 	    "\177\020"
   1353 	    "f\000\004lo\0"
   1354 	    "f\000\004low-bits\0"
   1355 		"=\017match\0",
   1356 	    0xff,
   1357 	    17,
   1358 	    "0xff<lo=0xf>\0"
   1359 	    "0xff<low-bits=0x#\0");
   1360 
   1361 	// new style, line_max exceeded by named bit-field value description
   1362 	// in line 2
   1363 	h_snprintb_m(
   1364 	    "\177\020"
   1365 	    "f\000\004lo\0"
   1366 	    "f\000\004low-bits\0"
   1367 		"=\017match\0",
   1368 	    0xff,
   1369 	    22,
   1370 	    "0xff<lo=0xf>\0"
   1371 	    "0xff<low-bits=0xf=mat#\0");
   1372 
   1373 	// new style, line_max exceeded by named bit-field '>' in line 2
   1374 	h_snprintb_m(
   1375 	    "\177\020"
   1376 	    "f\000\004lo\0"
   1377 	    "f\000\004low-bits\0"
   1378 		"=\017match\0",
   1379 	    0xff,
   1380 	    23,
   1381 	    "0xff<lo=0xf>\0"
   1382 	    "0xff<low-bits=0xf=matc#\0");
   1383 
   1384 	// new style, named bit-field complete
   1385 	h_snprintb_m(
   1386 	    "\177\020"
   1387 	    "f\000\004lo\0"
   1388 	    "f\000\004low-bits\0"
   1389 		"=\017match\0",
   1390 	    0xff,
   1391 	    24,
   1392 	    "0xff<lo=0xf>\0"
   1393 	    "0xff<low-bits=0xf=match>\0");
   1394 
   1395 	// new style, line_max exceeded by unnamed bit-field number in line 1
   1396 	h_snprintb_m(
   1397 	    "\177\020"
   1398 	    "F\000\004\0",
   1399 	    0xff,
   1400 	    3,
   1401 	    "0x#\0");
   1402 
   1403 	// new style, line_max exceeded by unnamed bit-field '<' in line 1
   1404 	h_snprintb_m(
   1405 	    "\177\020"
   1406 	    "F\000\004\0",
   1407 	    0xff,
   1408 	    4,
   1409 	    "0xf#\0");
   1410 
   1411 	// new style, line_max exceeded by unnamed bit-field value description
   1412 	// in line 1
   1413 	h_snprintb_m(
   1414 	    "\177\020"
   1415 	    "F\000\004\0"
   1416 		":\017match\0",
   1417 	    0xff,
   1418 	    9,
   1419 	    "0xff<mat#\0");
   1420 
   1421 	// new style, line_max exceeded by unnamed bit-field '>' in line 1
   1422 	h_snprintb_m(
   1423 	    "\177\020"
   1424 	    "F\000\004\0"
   1425 		":\017match\0",
   1426 	    0xff,
   1427 	    10,
   1428 	    "0xff<matc#\0");
   1429 
   1430 	// new style, line_max exceeded by unnamed bit-field value description
   1431 	// in line 2
   1432 	h_snprintb_m(
   1433 	    "\177\020"
   1434 	    "F\000\004\0"
   1435 		":\017m1\0"
   1436 		":\017match\0",
   1437 	    0xff,
   1438 	    10,
   1439 	    "0xff<m1ma#\0");
   1440 
   1441 	// new style, line_max exceeded by unnamed bit-field '>' in line 2
   1442 	h_snprintb_m(
   1443 	    "\177\020"
   1444 	    "F\000\004\0"
   1445 		":\017m1\0"
   1446 		":\017match\0",
   1447 	    0xff,
   1448 	    10,
   1449 	    "0xff<m1ma#\0");
   1450 
   1451 	// new style unnamed bit-field complete
   1452 	h_snprintb_m(
   1453 	    "\177\020"
   1454 	    "F\000\004\0"
   1455 		":\017m1\0"
   1456 		":\017match\0",
   1457 	    0xff,
   1458 	    13,
   1459 	    "0xff<m1match>\0");
   1460 
   1461 	// new style, line_max exceeded by bit-field default
   1462 	h_snprintb_m(
   1463 	    "\177\020"
   1464 	    "f\000\004f\0"
   1465 		"*=default\0",
   1466 	    0xff,
   1467 	    17,
   1468 	    "0xff<f=0xf=defau#\0");
   1469 
   1470 	// new style, line_max exceeded by unmatched field value
   1471 	h_snprintb_m(
   1472 	    "\177\020"
   1473 	    "f\000\004bits\0"
   1474 		":\000other\0",
   1475 	    0xff,
   1476 	    11,
   1477 	    "0xff<bits=#\0");
   1478 
   1479 	// manual page, new style bits and fields
   1480 	h_snprintb_m(
   1481 	    "\177\020"
   1482 	    "b\0LSB\0"
   1483 	    "b\1BITONE\0"
   1484 	    "f\4\4NIBBLE2\0"
   1485 	    "f\x10\4BURST\0"
   1486 		"=\4FOUR\0"
   1487 		"=\xf""FIFTEEN\0"
   1488 	    "b\x1fMSB\0",
   1489 	    0x800f0701,
   1490 	    34,
   1491 	    "0x800f0701<LSB,NIBBLE2=0>\0"
   1492 	    "0x800f0701<BURST=0xf=FIFTEEN,MSB>\0");
   1493 
   1494 	// new style, buffer too small for complete number in line 2
   1495 	h_snprintb_m_len(
   1496 	    15,
   1497 	    "\177\020"
   1498 	    "b\000lsb\0"
   1499 	    "b\001two\0",
   1500 	    0xff,
   1501 	    11,
   1502 	    20,
   1503 	    "0xff<lsb>\0"
   1504 	    "0xf\0"		// XXX: incomplete number may be misleading
   1505 	);
   1506 
   1507 	// new-style format, buffer too small for '<' in line 2
   1508 	h_snprintb_m_len(
   1509 	    16,
   1510 	    "\177\020"
   1511 	    "b\000lsb\0"
   1512 	    "b\001two\0",
   1513 	    0xff,
   1514 	    11,
   1515 	    20,
   1516 	    "0xff<lsb>\0"
   1517 	    "0xff\0"
   1518 	);
   1519 
   1520 	// new-style format, buffer too small for fallback
   1521 	h_snprintb_m(
   1522 	    "\177\020"
   1523 	    "f\000\004bits\0"
   1524 		"*=fallback\0"
   1525 	    "b\0024\0",
   1526 	    0xff,
   1527 	    64,
   1528 	    "0xff<bits=0xf=fallback,4>\0"
   1529 	);
   1530 
   1531 	// new-style format, buffer too small for numeric fallback
   1532 	h_snprintb_m_len(
   1533 	    20,
   1534 	    "\177\020"
   1535 	    "F\000\004\0"
   1536 		"*fallback(%040jd)\0",
   1537 	    0xff,
   1538 	    64,
   1539 	    57,
   1540 	    "0xff<fallback(0000\0"
   1541 	);
   1542 
   1543 	// new-style format, buffer too small for numeric fallback past buffer
   1544 	h_snprintb_m_len(
   1545 	    15,
   1546 	    "\177\020"
   1547 	    "F\000\004\0"
   1548 		"*fallback(%010jd)\0"
   1549 	    "F\004\004\0"
   1550 		"*fallback(%010jd)\0",
   1551 	    0xff,
   1552 	    64,
   1553 	    48,
   1554 	    "0xff<fallback\0"
   1555 	);
   1556 
   1557 	// new style, bits and fields, line break between fields
   1558 	h_snprintb_m(
   1559 	    "\177\020"
   1560 	    "b\0LSB\0"
   1561 	    "b\1_BITONE\0"
   1562 	    "f\4\4NIBBLE2\0"
   1563 	    "f\x10\4BURST\0"
   1564 		"=\04FOUR\0"
   1565 		"=\17FIFTEEN\0"
   1566 	    "b\x1fMSB\0",
   1567 	    0x800f0701,
   1568 	    33,
   1569 	    "0x800f0701<LSB,NIBBLE2=0>\0"
   1570 	    "0x800f0701<BURST=0xf=FIFTEEN,MSB>\0"
   1571 	);
   1572 
   1573 	// new style, bits and fields, line break after field description
   1574 	h_snprintb_m(
   1575 	    "\177\020"
   1576 	    "b\0LSB\0"
   1577 	    "b\1_BITONE\0"
   1578 	    "f\4\4NIBBLE2\0"
   1579 	    "f\x10\4BURST\0"
   1580 		"=\04FOUR\0"
   1581 		"=\17FIFTEEN\0"
   1582 	    "b\x1fMSB\0",
   1583 	    0x800f0701,
   1584 	    32,
   1585 	    "0x800f0701<LSB,NIBBLE2=0>\0"
   1586 	    "0x800f0701<BURST=0xf=FIFTEEN>\0"
   1587 	    "0x800f0701<MSB>\0");
   1588 }
   1589 
   1590 ATF_TP_ADD_TCS(tp)
   1591 {
   1592 
   1593 	ATF_TP_ADD_TC(tp, snprintb);
   1594 	ATF_TP_ADD_TC(tp, snprintb_m);
   1595 
   1596 	return atf_no_error();
   1597 }
   1598