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