dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 98.0% 50 / 0 / 51
Functions: 100.0% 6 / 0 / 6
Branches: 86.4% 19 / 0 / 22

src/command/error.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "error.h"
6 #include "util/log.h"
7 #include "util/xstdio.h"
8
9 VPRINTF(3) NONNULL_ARG(1, 3)
10 173 static void error_msgv(ErrorBuffer *eb, const char *cmd, const char *format, va_list ap)
11 {
12 173 const size_t size = sizeof(eb->buf);
13 173 const char *file = eb->sourcepos.filename;
14 173 unsigned int line = eb->sourcepos.line;
15 173 int pos = 0;
16
17
2/2
✓ Branch 2 → 3 taken 3 times.
✓ Branch 2 → 4 taken 170 times.
173 if (file && cmd) {
18 3 pos = snprintf(eb->buf, size, "%s:%u: %s: ", file, line, cmd);
19
2/2
✓ Branch 4 → 5 taken 11 times.
✓ Branch 4 → 6 taken 159 times.
170 } else if (file) {
20 11 pos = snprintf(eb->buf, size, "%s:%u: ", file, line);
21
2/2
✓ Branch 6 → 7 taken 145 times.
✓ Branch 6 → 10 taken 14 times.
159 } else if (cmd) {
22 145 pos = snprintf(eb->buf, size, "%s: ", cmd);
23 }
24
25
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 159 times.
159 LOG_ERRNO_ON(pos < 0, "snprintf");
26 173 pos = MAX(0, pos);
27
28
1/2
✓ Branch 10 → 11 taken 173 times.
✗ Branch 10 → 12 not taken.
173 if (likely(pos < (size - 3))) {
29 173 vsnprintf(eb->buf + pos, size - pos, format, ap);
30 } else {
31 LOG_WARNING("no buffer space left for error message");
32 }
33
34
2/2
✓ Branch 13 → 14 taken 5 times.
✓ Branch 13 → 17 taken 168 times.
173 if (eb->print_to_stderr) {
35 5 xfputs(eb->buf, stderr);
36 5 xfputc('\n', stderr);
37 5 eb->stderr_errors_printed = true;
38 }
39
40 173 eb->is_error = true;
41 173 eb->nr_errors++;
42 173 LOG_INFO("%s", eb->buf);
43 173 }
44
45 167 bool error_msg(ErrorBuffer *eb, const char *format, ...)
46 {
47
2/2
✓ Branch 2 → 3 taken 151 times.
✓ Branch 2 → 5 taken 16 times.
167 if (eb) {
48 151 va_list ap;
49 151 va_start(ap, format);
50 151 error_msgv(eb, eb->command_name, format, ap);
51 151 va_end(ap);
52 }
53 167 return false; // To allow tail-calling from command handlers
54 }
55
56 23 bool error_msg_for_cmd(ErrorBuffer *eb, const char *cmd, const char *format, ...)
57 {
58
2/2
✓ Branch 2 → 3 taken 22 times.
✓ Branch 2 → 5 taken 1 time.
23 if (eb) {
59 22 va_list ap;
60 22 va_start(ap, format);
61 22 error_msgv(eb, cmd, format, ap);
62 22 va_end(ap);
63 }
64 23 return false;
65 }
66
67 2 bool error_msg_errno(ErrorBuffer *eb, const char *prefix)
68 {
69
3/4
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 7 taken 1 time.
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 7 not taken.
2 return eb ? error_msg(eb, "%s: %s", prefix, strerror(errno)) : false;
70 }
71
72 16 bool info_msg(ErrorBuffer *eb, const char *format, ...)
73 {
74
2/2
✓ Branch 2 → 3 taken 15 times.
✓ Branch 2 → 4 taken 1 time.
16 if (eb) {
75 15 va_list ap;
76 15 va_start(ap, format);
77 15 vsnprintf(eb->buf, sizeof(eb->buf), format, ap);
78 15 va_end(ap);
79 15 eb->is_error = false;
80 }
81 16 return true; // To allow tail-calling from command handlers
82 }
83
84 175 void clear_error(ErrorBuffer *eb)
85 {
86 175 eb->buf[0] = '\0';
87 175 }
88