• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 23966986054

03 Apr 2026 09:22PM UTC coverage: 72.107% (-0.3%) from 72.362%
23966986054

push

github

daandemeyer
fd-util: Add missing assert()

1 of 1 new or added line in 1 file covered. (100.0%)

7891 existing lines in 120 files now uncovered.

318256 of 441368 relevant lines covered (72.11%)

1186882.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.86
/src/shared/parse-argument.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include "alloc-util.h"
4
#include "ansi-color.h"
5
#include "bus-util.h"
6
#include "format-table.h"
7
#include "hostname-util.h"
8
#include "log.h"
9
#include "parse-argument.h"
10
#include "parse-util.h"
11
#include "path-util.h"
12
#include "signal-util.h"
13
#include "string-table.h"
14
#include "string-util.h"
15

16
/* All functions in this file emit warnings. */
17

18
int parse_boolean_argument(const char *optname, const char *s, bool *ret) {
2,112✔
19
        int r;
2,112✔
20

21
        /* Returns the result through *ret and the return value. */
22

23
        assert(optname);
2,112✔
24

25
        if (s) {
2,112✔
26
                r = parse_boolean(s);
2,106✔
27
                if (r < 0)
2,106✔
28
                        return log_error_errno(r, "Failed to parse boolean argument to '%s': %s", optname, s);
32✔
29

30
                if (ret)
2,074✔
31
                        *ret = r;
1,585✔
32
                return r;
2,074✔
33
        } else {
34
                /* s may be NULL. This is controlled by getopt_long() parameters. */
35
                if (ret)
6✔
36
                        *ret = true;
6✔
37
                return true;
6✔
38
        }
39
}
40

41
int parse_tristate_argument_with_auto(const char *optname, const char *s, int *ret) {
130✔
42
        int r;
130✔
43

44
        assert(optname);
130✔
45
        assert(s); /* We refuse NULL optarg here, since that would be ambiguous on cmdline:
130✔
46
                      for --enable-a[=BOOL], --enable-a is intuitively interpreted as true rather than "auto"
47
                      (parse_boolean_argument() does exactly that). IOW, tristate options should require
48
                      arguments. */
49

50
        r = parse_tristate_full(s, "auto", ret);
130✔
51
        if (r < 0)
130✔
52
                return log_error_errno(r, "Failed to parse tristate argument to '%s': %s", optname, s);
1✔
53

54
        return 0;
55
}
56

57
int parse_json_argument(const char *s, sd_json_format_flags_t *ret) {
531✔
58
        assert(s);
531✔
59
        assert(ret);
531✔
60

61
        if (streq(s, "pretty"))
531✔
62
                *ret = SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO;
104✔
63
        else if (streq(s, "short"))
427✔
64
                *ret = SD_JSON_FORMAT_NEWLINE;
404✔
65
        else if (streq(s, "off"))
23✔
66
                *ret = SD_JSON_FORMAT_OFF;
11✔
67
        else if (streq(s, "help")) {
12✔
68
                puts("pretty\n"
4✔
69
                     "short\n"
70
                     "off");
71
                return 0; /* 0 means → we showed a brief help, exit now */
4✔
72
        } else
73
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown argument to --json= switch: %s", s);
8✔
74

75
        return 1; /* 1 means → properly parsed */
76
}
77

78
int parse_path_argument(const char *path, bool suppress_root, char **arg) {
2,475✔
79
        char *p;
2,475✔
80
        int r;
2,475✔
81

82
        assert(arg);
2,475✔
83

84
        /*
85
         * This function is intended to be used in command line parsers, to handle paths that are passed
86
         * in. It makes the path absolute, and reduces it to NULL if omitted or root (the latter optionally).
87
         *
88
         * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON SUCCESS!
89
         * Hence, do not pass in uninitialized pointers.
90
         */
91

92
        if (isempty(path)) {
2,475✔
93
                *arg = mfree(*arg);
8✔
94
                return 0;
8✔
95
        }
96

97
        r = path_make_absolute_cwd(path, &p);
2,467✔
98
        if (r < 0)
2,467✔
UNCOV
99
                return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path);
×
100

101
        path_simplify(p);
2,467✔
102
        if (suppress_root && empty_or_root(p))
2,467✔
103
                p = mfree(p);
3✔
104

105
        return free_and_replace(*arg, p);
2,467✔
106
}
107

108
int parse_signal_argument(const char *s, int *ret) {
28✔
109
        int r;
28✔
110

111
        assert(s);
28✔
112
        assert(ret);
28✔
113

114
        if (streq(s, "help"))
28✔
115
                return DUMP_STRING_TABLE(signal, int, _NSIG);
132✔
116

117
        if (streq(s, "list")) {
26✔
118
                _cleanup_(table_unrefp) Table *table = NULL;
1✔
119

120
                table = table_new("signal", "name");
1✔
121
                if (!table)
1✔
UNCOV
122
                        return log_oom();
×
123

124
                for (int i = 1; i < _NSIG; i++) {
65✔
125
                        r = table_add_many(
64✔
126
                                        table,
127
                                        TABLE_INT, i,
128
                                        TABLE_SIGNAL, i);
129
                        if (r < 0)
64✔
UNCOV
130
                                return table_log_add_error(r);
×
131
                }
132

133
                r = table_print(table, NULL);
1✔
134
                if (r < 0)
1✔
UNCOV
135
                        return table_log_print_error(r);
×
136

137
                return 0;
138
        }
139

140
        r = signal_from_string(s);
25✔
141
        if (r < 0)
25✔
UNCOV
142
                return log_error_errno(r, "Failed to parse signal string \"%s\".", s);
×
143

144
        *ret = r;
25✔
145
        return 1; /* work to do */
25✔
146
}
147

148
int parse_machine_argument(const char *s, const char **ret_host, BusTransport *ret_transport) {
116✔
149
        int r;
116✔
150

151
        assert(s);
116✔
152
        assert(ret_host);
116✔
153

154
        r = machine_spec_valid(s);
116✔
155
        if (r < 0)
116✔
156
                return log_error_errno(r, "Failed to validate --machine= argument '%s': %m", s);
×
157
        if (r == 0)
116✔
UNCOV
158
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --machine= specified: %s", s);
×
159

160
        *ret_host = s;
116✔
161

162
        if (ret_transport)
116✔
163
                *ret_transport = BUS_TRANSPORT_MACHINE;
116✔
164

165
        return 0;
166
}
167

168
int parse_background_argument(const char *s, char **arg) {
9✔
169
        if (!isempty(s) && !looks_like_ansi_color_code(s))
17✔
170
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --background= argument: %s", s);
4✔
171

172
        return free_and_strdup_warn(arg, s);
5✔
173
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc