dte test coverage


Directory: ./
File: src/util/xsnprintf.c
Date: 2025-09-07 23:01:39
Exec Total Coverage
Lines: 10 10 100.0%
Functions: 2 2 100.0%
Branches: 2 6 33.3%

Line Branch Exec Source
1 #include <errno.h>
2 #include <stdio.h>
3 #include "xsnprintf.h"
4 #include "debug.h"
5
6 /*
7 * ISO C doesn't require vsnprintf(3) to set errno on failure, but
8 * POSIX does:
9 *
10 * "If an output error was encountered, these functions shall return
11 * a negative value and set errno to indicate the error."
12 *
13 * The mandated errors of interest are:
14 *
15 * • EILSEQ: A wide-character code does not correspond to a valid character
16 * • EOVERFLOW: The value of n is greater than INT_MAX
17 * • EOVERFLOW: The value to be returned is greater than INT_MAX
18 *
19 * ISO C11 states:
20 *
21 * "The vsnprintf function returns the number of characters that would
22 * have been written had n been sufficiently large, not counting the
23 * terminating null character, or a negative value if an encoding error
24 * occurred. Thus, the null-terminated output has been completely
25 * written if and only if the returned value is nonnegative and less
26 * than n."
27 *
28 * See also:
29 *
30 * • ISO C11 §7.21.6.12p3
31 * • https://pubs.opengroup.org/onlinepubs/9699919799/functions/vsnprintf.html
32 * • https://pubs.opengroup.org/onlinepubs/9699919799/functions/snprintf.html
33 */
34 5530 size_t xvsnprintf(char *restrict buf, size_t n, const char *restrict fmt, va_list v)
35 {
36 5530 int r = vsnprintf(buf, n, fmt, v);
37
2/6
✓ Branch 0 (2→3) taken 5530 times.
✗ Branch 1 (2→5) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→7) taken 5530 times.
✗ Branch 4 (4→5) not taken.
✗ Branch 5 (4→6) not taken.
5530 FATAL_ERROR_ON(r < 0 || r >= (int)n, r < 0 ? errno : ENOBUFS);
38 5530 return (size_t)r;
39 }
40
41 5528 size_t xsnprintf(char *restrict buf, size_t n, const char *restrict fmt, ...)
42 {
43 5528 va_list v;
44 5528 va_start(v, fmt);
45 5528 size_t r = xvsnprintf(buf, n, fmt, v);
46 5528 va_end(v);
47 5528 return r;
48 }
49