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

systemd / systemd / 16307057356

15 Jul 2025 11:29PM UTC coverage: 72.142% (-0.02%) from 72.166%
16307057356

push

github

web-flow
fsck,quotacheck: add credentials support and drop support of /forcefsck and friends (#38197)

17 of 54 new or added lines in 2 files covered. (31.48%)

2680 existing lines in 65 files now uncovered.

301900 of 418480 relevant lines covered (72.14%)

731273.64 hits per line

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

97.04
/src/basic/parse-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <linux/ipv6.h>
4
#include <linux/netfilter/nf_tables.h>
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <sys/socket.h>
8

9
#include "alloc-util.h"
10
#include "errno-list.h"
11
#include "extract-word.h"
12
#include "locale-util.h"
13
#include "log.h"
14
#include "missing-network.h"
15
#include "parse-util.h"
16
#include "path-util.h"
17
#include "process-util.h"
18
#include "string-util.h"
19
#include "strv.h"
20

21
int parse_boolean(const char *v) {
641,220✔
22
        if (!v)
641,220✔
23
                return -EINVAL;
24

25
        if (STRCASE_IN_SET(v,
641,220✔
26
                           "1",
27
                           "yes",
28
                           "y",
29
                           "true",
30
                           "t",
31
                           "on"))
32
                return 1;
189,457✔
33

34
        if (STRCASE_IN_SET(v,
451,763✔
35
                           "0",
36
                           "no",
37
                           "n",
38
                           "false",
39
                           "f",
40
                           "off"))
41
                return 0;
415,275✔
42

43
        return -EINVAL;
36,488✔
44
}
45

46
int parse_tristate_full(const char *v, const char *third, int *ret) {
5,517✔
47
        int r;
5,517✔
48

49
        if (isempty(v) || streq_ptr(v, third)) { /* Empty string is always taken as the third/invalid/auto state */
11,034✔
50
                if (ret)
×
UNCOV
51
                        *ret = -1;
×
52
        } else {
53
                r = parse_boolean(v);
5,517✔
54
                if (r < 0)
5,517✔
55
                        return r;
56

57
                if (ret)
5,517✔
58
                        *ret = r;
5,517✔
59
        }
60

61
        return 0;
62
}
63

64
int parse_pid(const char *s, pid_t* ret_pid) {
105,911✔
65
        unsigned long ul = 0;
105,911✔
66
        pid_t pid;
105,911✔
67
        int r;
105,911✔
68

69
        assert(s);
105,911✔
70

71
        r = safe_atolu(s, &ul);
105,911✔
72
        if (r < 0)
105,911✔
73
                return r;
105,911✔
74

75
        pid = (pid_t) ul;
105,769✔
76

77
        if ((unsigned long) pid != ul)
105,769✔
78
                return -ERANGE;
79

80
        if (!pid_is_valid(pid))
105,769✔
81
                return -ERANGE;
82

83
        if (ret_pid)
105,767✔
84
                *ret_pid = pid;
105,765✔
85
        return 0;
86
}
87

88
int parse_mode(const char *s, mode_t *ret) {
113,673✔
89
        unsigned m;
113,673✔
90
        int r;
113,673✔
91

92
        assert(s);
113,673✔
93

94
        r = safe_atou_full(s, 8 |
113,673✔
95
                           SAFE_ATO_REFUSE_PLUS_MINUS, /* Leading '+' or even '-' char? that's just weird,
96
                                                        * refuse. User might have wanted to add mode flags or
97
                                                        * so, but this parser doesn't allow that, so let's
98
                                                        * better be safe. */
99
                           &m);
100
        if (r < 0)
113,673✔
101
                return r;
113,673✔
102
        if (m > 07777)
113,658✔
103
                return -ERANGE;
104

105
        if (ret)
113,657✔
106
                *ret = m;
113,657✔
107
        return 0;
108
}
109

110
int parse_ifindex(const char *s) {
168,278✔
111
        int ifi, r;
168,278✔
112

113
        assert(s);
168,278✔
114

115
        r = safe_atoi(s, &ifi);
168,278✔
116
        if (r < 0)
168,278✔
117
                return r;
168,278✔
118
        if (ifi <= 0)
37,882✔
119
                return -EINVAL;
2✔
120

121
        return ifi;
122
}
123

124
int parse_mtu(int family, const char *s, uint32_t *ret) {
238✔
125
        uint64_t u, m;
238✔
126
        int r;
238✔
127

128
        r = parse_size(s, 1024, &u);
238✔
129
        if (r < 0)
238✔
130
                return r;
238✔
131

132
        if (u > UINT32_MAX)
217✔
133
                return -ERANGE;
134

135
        switch (family) {
213✔
136
        case AF_INET:
137
                m = IPV4_MIN_MTU; /* This is 68 */
138
                break;
139
        case AF_INET6:
27✔
140
                m = IPV6_MIN_MTU; /* This is 1280 */
27✔
141
                break;
27✔
142
        default:
143
                m = 0;
144
        }
145

146
        if (u < m)
37✔
147
                return -ERANGE;
148

149
        *ret = (uint32_t) u;
209✔
150
        return 0;
209✔
151
}
152

153
int parse_size(const char *t, uint64_t base, uint64_t *size) {
13,240✔
154

155
        /* Soo, sometimes we want to parse IEC binary suffixes, and
156
         * sometimes SI decimal suffixes. This function can parse
157
         * both. Which one is the right way depends on the
158
         * context. Wikipedia suggests that SI is customary for
159
         * hardware metrics and network speeds, while IEC is
160
         * customary for most data sizes used by software and volatile
161
         * (RAM) memory. Hence be careful which one you pick!
162
         *
163
         * In either case we use just K, M, G as suffix, and not Ki,
164
         * Mi, Gi or so (as IEC would suggest). That's because that's
165
         * frickin' ugly. But this means you really need to make sure
166
         * to document which base you are parsing when you use this
167
         * call. */
168

169
        struct table {
13,240✔
170
                const char *suffix;
171
                unsigned long long factor;
172
        };
173

174
        static const struct table iec[] = {
13,240✔
175
                { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
176
                { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
177
                { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
178
                { "G", 1024ULL*1024ULL*1024ULL },
179
                { "M", 1024ULL*1024ULL },
180
                { "K", 1024ULL },
181
                { "B", 1ULL },
182
                { "",  1ULL },
183
        };
184

185
        static const struct table si[] = {
13,240✔
186
                { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
187
                { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
188
                { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
189
                { "G", 1000ULL*1000ULL*1000ULL },
190
                { "M", 1000ULL*1000ULL },
191
                { "K", 1000ULL },
192
                { "B", 1ULL },
193
                { "",  1ULL },
194
        };
195

196
        const struct table *table;
13,240✔
197
        const char *p;
13,240✔
198
        unsigned long long r = 0;
13,240✔
199
        unsigned n_entries, start_pos = 0;
13,240✔
200

201
        assert(t);
13,240✔
202
        assert(IN_SET(base, 1000, 1024));
13,240✔
203
        assert(size);
13,240✔
204

205
        if (base == 1000) {
13,240✔
206
                table = si;
207
                n_entries = ELEMENTSOF(si);
208
        } else {
209
                table = iec;
13,172✔
210
                n_entries = ELEMENTSOF(iec);
13,172✔
211
        }
212

213
        p = t;
13,240✔
214
        do {
13,295✔
215
                unsigned long long l, tmp;
13,295✔
216
                double frac = 0;
13,295✔
217
                char *e;
13,295✔
218
                unsigned i;
13,295✔
219

220
                p += strspn(p, WHITESPACE);
13,295✔
221

222
                errno = 0;
13,295✔
223
                l = strtoull(p, &e, 10);
13,295✔
224
                if (errno > 0)
13,295✔
225
                        return -errno;
55✔
226
                if (e == p)
13,293✔
227
                        return -EINVAL;
228
                if (*p == '-')
13,256✔
229
                        return -ERANGE;
230

231
                if (*e == '.') {
13,244✔
232
                        e++;
43✔
233

234
                        /* strtoull() itself would accept space/+/- */
235
                        if (ascii_isdigit(*e)) {
43✔
236
                                unsigned long long l2;
38✔
237
                                char *e2;
38✔
238

239
                                l2 = strtoull(e, &e2, 10);
38✔
240
                                if (errno > 0)
38✔
UNCOV
241
                                        return -errno;
×
242

243
                                /* Ignore failure. E.g. 10.M is valid */
244
                                frac = l2;
38✔
245
                                for (; e < e2; e++)
84✔
246
                                        frac /= 10;
46✔
247
                        }
248
                }
249

250
                e += strspn(e, WHITESPACE);
13,244✔
251

252
                for (i = start_pos; i < n_entries; i++)
102,390✔
253
                        if (startswith(e, table[i].suffix))
102,388✔
254
                                break;
255

256
                if (i >= n_entries)
13,242✔
257
                        return -EINVAL;
258

259
                if (l + (frac > 0) > ULLONG_MAX / table[i].factor)
13,242✔
260
                        return -ERANGE;
261

262
                tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor);
13,240✔
263
                if (tmp > ULLONG_MAX - r)
13,240✔
264
                        return -ERANGE;
265

266
                r += tmp;
13,240✔
267
                if ((unsigned long long) (uint64_t) r != r)
13,240✔
268
                        return -ERANGE;
269

270
                p = e + strlen(table[i].suffix);
13,240✔
271

272
                start_pos = i + 1;
13,240✔
273

274
        } while (*p);
13,240✔
275

276
        *size = r;
13,185✔
277

278
        return 0;
13,185✔
279
}
280

281
int parse_sector_size(const char *t, uint64_t *ret) {
4✔
282
        int r;
4✔
283

284
        assert(t);
4✔
285
        assert(ret);
4✔
286

287
        uint64_t ss;
4✔
288

289
        r = safe_atou64(t, &ss);
4✔
290
        if (r < 0)
4✔
UNCOV
291
                return log_error_errno(r, "Failed to parse sector size parameter %s", t);
×
292
        if (ss < 512 || ss > 4096) /* Allow up to 4K due to dm-crypt support and 4K alignment by the homed LUKS backend */
4✔
UNCOV
293
                return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Sector size not between 512 and 4096: %s", t);
×
294
        if (!ISPOWEROF2(ss))
4✔
UNCOV
295
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Sector size not power of 2: %s", t);
×
296

297
        *ret = ss;
4✔
298
        return 0;
4✔
299
}
300

301
int parse_range(const char *t, unsigned *lower, unsigned *upper) {
366✔
302
        _cleanup_free_ char *word = NULL;
366✔
303
        unsigned l, u;
366✔
304
        int r;
366✔
305

306
        assert(lower);
366✔
307
        assert(upper);
366✔
308

309
        /* Extract the lower bound. */
310
        r = extract_first_word(&t, &word, "-", EXTRACT_DONT_COALESCE_SEPARATORS);
366✔
311
        if (r < 0)
366✔
312
                return r;
313
        if (r == 0)
366✔
314
                return -EINVAL;
315

316
        r = safe_atou(word, &l);
366✔
317
        if (r < 0)
366✔
318
                return r;
319

320
        /* Check for the upper bound and extract it if needed */
321
        if (!t)
337✔
322
                /* Single number with no dashes. */
323
                u = l;
131✔
324
        else if (!*t)
206✔
325
                /* Trailing dash is an error. */
326
                return -EINVAL;
327
        else {
328
                r = safe_atou(t, &u);
205✔
329
                if (r < 0)
205✔
330
                        return r;
331
        }
332

333
        *lower = l;
324✔
334
        *upper = u;
324✔
335
        return 0;
324✔
336
}
337

338
int parse_errno(const char *t) {
1,306✔
339
        int r, e;
1,306✔
340

341
        assert(t);
1,306✔
342

343
        r = errno_from_name(t);
1,306✔
344
        if (r > 0)
1,306✔
345
                return r;
1,306✔
346

347
        r = safe_atoi(t, &e);
29✔
348
        if (r < 0)
29✔
349
                return r;
350

351
        /* 0 is also allowed here */
352
        if (!errno_is_valid(e) && e != 0)
14✔
353
                return -ERANGE;
5✔
354

355
        return e;
356
}
357

358
int parse_fd(const char *t) {
178,623✔
359
        int r, fd;
178,623✔
360

361
        assert(t);
178,623✔
362

363
        r = safe_atoi(t, &fd);
178,623✔
364
        if (r < 0)
178,623✔
365
                return r;
178,623✔
366

367
        if (fd < 0)
178,619✔
368
                return -EBADF;
2✔
369

370
        return fd;
371
}
372

373
int parse_user_shell(const char *s, char **ret_sh, bool *ret_copy) {
26✔
374
        char *sh;
26✔
375
        int r;
26✔
376

377
        if (path_is_absolute(s) && path_is_normalized(s)) {
26✔
378
                sh = strdup(s);
9✔
379
                if (!sh)
9✔
380
                        return -ENOMEM;
381

382
                *ret_sh = sh;
9✔
383
                *ret_copy = false;
9✔
384
        } else {
385
                r = parse_boolean(s);
17✔
386
                if (r < 0)
17✔
387
                        return r;
388

389
                *ret_sh = NULL;
16✔
390
                *ret_copy = r;
16✔
391
        }
392

393
        return 0;
394
}
395

396
static const char *mangle_base(const char *s, unsigned *base) {
5,882,056✔
397
        const char *k;
5,882,056✔
398

399
        assert(s);
5,882,056✔
400
        assert(base);
5,882,056✔
401

402
        /* Base already explicitly specified, then don't do anything. */
403
        if (SAFE_ATO_MASK_FLAGS(*base) != 0)
5,882,056✔
404
                return s;
5,882,056✔
405

406
        /* Support Python 3 style "0b" and 0x" prefixes, because they truly make sense, much more than C's "0" prefix for octal. */
407
        k = STARTSWITH_SET(s, "0b", "0B");
5,491,781✔
408
        if (k) {
5,491,781✔
409
                *base = 2 | (*base & SAFE_ATO_ALL_FLAGS);
4✔
410
                return k;
4✔
411
        }
412

413
        k = STARTSWITH_SET(s, "0o", "0O");
5,491,777✔
414
        if (k) {
5,491,777✔
415
                *base = 8 | (*base & SAFE_ATO_ALL_FLAGS);
4✔
416
                return k;
4✔
417
        }
418

419
        return s;
420
}
421

422
int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
2,103,700✔
423
        char *x = NULL;
2,103,700✔
424
        unsigned long l;
2,103,700✔
425

426
        assert(s);
2,103,700✔
427
        assert(SAFE_ATO_MASK_FLAGS(base) <= 16);
2,103,700✔
428

429
        /* strtoul() is happy to parse negative values, and silently converts them to unsigned values without
430
         * generating an error. We want a clean error, hence let's look for the "-" prefix on our own, and
431
         * generate an error. But let's do so only after strtoul() validated that the string is clean
432
         * otherwise, so that we return EINVAL preferably over ERANGE. */
433

434
        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_WHITESPACE) &&
2,103,700✔
435
            strchr(WHITESPACE, s[0]))
206,962✔
436
                return -EINVAL;
2,103,700✔
437

438
        s += strspn(s, WHITESPACE);
2,103,683✔
439

440
        if (FLAGS_SET(base, SAFE_ATO_REFUSE_PLUS_MINUS) &&
2,103,683✔
441
            IN_SET(s[0], '+', '-'))
320,325✔
442
                return -EINVAL; /* Note that we check the "-" prefix again a second time below, but return a
443
                                 * different error. I.e. if the SAFE_ATO_REFUSE_PLUS_MINUS flag is set we
444
                                 * blanket refuse +/- prefixed integers, while if it is missing we'll just
445
                                 * return ERANGE, because the string actually parses correctly, but doesn't
446
                                 * fit in the return type. */
447

448
        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_ZERO) &&
2,103,666✔
449
            s[0] == '0' && !streq(s, "0"))
205,478✔
450
                return -EINVAL; /* This is particularly useful to avoid ambiguities between C's octal
451
                                 * notation and assumed-to-be-decimal integers with a leading zero. */
452

453
        s = mangle_base(s, &base);
2,103,646✔
454

455
        errno = 0;
2,103,646✔
456
        l = strtoul(s, &x, SAFE_ATO_MASK_FLAGS(base) /* Let's mask off the flags bits so that only the actual
2,103,646✔
457
                                                      * base is left */);
458
        if (errno > 0)
2,103,646✔
459
                return -errno;
2✔
460
        if (!x || x == s || *x != 0)
2,103,644✔
461
                return -EINVAL;
462
        if (l != 0 && s[0] == '-')
1,901,302✔
463
                return -ERANGE;
464
        if ((unsigned long) (unsigned) l != l)
1,799,960✔
465
                return -ERANGE;
466

467
        if (ret_u)
1,901,274✔
468
                *ret_u = (unsigned) l;
1,901,192✔
469

470
        return 0;
471
}
472

473
int safe_atou_bounded(const char *s, unsigned min, unsigned max, unsigned *ret) {
99✔
474
        unsigned v;
99✔
475
        int r;
99✔
476

477
        r = safe_atou(s, &v);
99✔
478
        if (r < 0)
99✔
479
                return r;
99✔
480

481
        if (v < min || v > max)
98✔
482
                return -ERANGE;
483

484
        *ret = v;
96✔
485
        return 0;
96✔
486
}
487

488
int safe_atoi(const char *s, int *ret_i) {
2,168,864✔
489
        unsigned base = 0;
2,168,864✔
490
        char *x = NULL;
2,168,864✔
491
        long l;
2,168,864✔
492

493
        assert(s);
2,168,864✔
494

495
        s += strspn(s, WHITESPACE);
2,168,864✔
496
        s = mangle_base(s, &base);
2,168,864✔
497

498
        errno = 0;
2,168,864✔
499
        l = strtol(s, &x, base);
2,168,864✔
500
        if (errno > 0)
2,168,864✔
501
                return -errno;
8✔
502
        if (!x || x == s || *x != 0)
2,168,856✔
503
                return -EINVAL;
504
        if ((long) (int) l != l)
2,027,978✔
505
                return -ERANGE;
506

507
        if (ret_i)
2,027,971✔
508
                *ret_i = (int) l;
2,027,624✔
509

510
        return 0;
511
}
512

513
int safe_atollu_full(const char *s, unsigned base, unsigned long long *ret_llu) {
1,609,492✔
514
        char *x = NULL;
1,609,492✔
515
        unsigned long long l;
1,609,492✔
516

517
        assert(s);
1,609,492✔
518
        assert(SAFE_ATO_MASK_FLAGS(base) <= 16);
1,609,492✔
519

520
        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_WHITESPACE) &&
1,609,492✔
UNCOV
521
            strchr(WHITESPACE, s[0]))
×
522
                return -EINVAL;
1,609,492✔
523

524
        s += strspn(s, WHITESPACE);
1,609,492✔
525

526
        if (FLAGS_SET(base, SAFE_ATO_REFUSE_PLUS_MINUS) &&
1,609,492✔
UNCOV
527
            IN_SET(s[0], '+', '-'))
×
528
                return -EINVAL;
529

530
        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_ZERO) &&
1,609,492✔
UNCOV
531
            s[0] == '0' && s[1] != 0)
×
532
                return -EINVAL;
533

534
        s = mangle_base(s, &base);
1,609,492✔
535

536
        errno = 0;
1,609,492✔
537
        l = strtoull(s, &x, SAFE_ATO_MASK_FLAGS(base));
1,609,492✔
538
        if (errno > 0)
1,609,492✔
539
                return -errno;
3✔
540
        if (!x || x == s || *x != 0)
1,609,489✔
541
                return -EINVAL;
542
        if (l != 0 && s[0] == '-')
1,609,331✔
543
                return -ERANGE;
544

545
        if (ret_llu)
1,609,324✔
546
                *ret_llu = l;
1,609,324✔
547

548
        return 0;
549
}
550

551
int safe_atolli(const char *s, long long *ret_lli) {
40✔
552
        unsigned base = 0;
40✔
553
        char *x = NULL;
40✔
554
        long long l;
40✔
555

556
        assert(s);
40✔
557

558
        s += strspn(s, WHITESPACE);
40✔
559
        s = mangle_base(s, &base);
40✔
560

561
        errno = 0;
40✔
562
        l = strtoll(s, &x, base);
40✔
563
        if (errno > 0)
40✔
564
                return -errno;
4✔
565
        if (!x || x == s || *x != 0)
36✔
566
                return -EINVAL;
567

568
        if (ret_lli)
28✔
569
                *ret_lli = l;
28✔
570

571
        return 0;
572
}
573

574
int safe_atou8_full(const char *s, unsigned base, uint8_t *ret) {
144,288✔
575
        unsigned u;
144,288✔
576
        int r;
144,288✔
577

578
        r = safe_atou_full(s, base, &u);
144,288✔
579
        if (r < 0)
144,288✔
580
                return r;
144,288✔
581
        if (u > UINT8_MAX)
144,176✔
582
                return -ERANGE;
583

584
        *ret = (uint8_t) u;
144,175✔
585
        return 0;
144,175✔
586
}
587

588
int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) {
3,930✔
589
        unsigned u;
3,930✔
590
        int r;
3,930✔
591

592
        r = safe_atou_full(s, base, &u);
3,930✔
593
        if (r < 0)
3,930✔
594
                return r;
3,930✔
595
        if (u > UINT16_MAX)
3,847✔
596
                return -ERANGE;
597

598
        *ret = (uint16_t) u;
3,840✔
599
        return 0;
3,840✔
600
}
601

602
int safe_atoi16(const char *s, int16_t *ret) {
14✔
603
        unsigned base = 0;
14✔
604
        char *x = NULL;
14✔
605
        long l;
14✔
606

607
        assert(s);
14✔
608

609
        s += strspn(s, WHITESPACE);
14✔
610
        s = mangle_base(s, &base);
14✔
611

612
        errno = 0;
14✔
613
        l = strtol(s, &x, base);
14✔
614
        if (errno > 0)
14✔
UNCOV
615
                return -errno;
×
616
        if (!x || x == s || *x != 0)
14✔
617
                return -EINVAL;
618
        if ((long) (int16_t) l != l)
10✔
619
                return -ERANGE;
620

621
        if (ret)
8✔
622
                *ret = (int16_t) l;
8✔
623

624
        return 0;
625
}
626

627
int safe_atod(const char *s, double *ret_d) {
12✔
628
        _cleanup_(freelocalep) locale_t loc = (locale_t) 0;
24✔
629
        char *x = NULL;
12✔
630
        double d = 0;
12✔
631

632
        assert(s);
12✔
633

634
        loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
12✔
635
        if (loc == (locale_t) 0)
12✔
UNCOV
636
                return -errno;
×
637

638
        errno = 0;
12✔
639
        d = strtod_l(s, &x, loc);
12✔
640
        if (errno > 0)
12✔
UNCOV
641
                return -errno;
×
642
        if (!x || x == s || *x != 0)
12✔
643
                return -EINVAL;
644

645
        if (ret_d)
7✔
646
                *ret_d = (double) d;
7✔
647

648
        return 0;
649
}
650

651
int parse_fractional_part_u(const char **p, size_t digits, unsigned *res) {
459✔
652
        unsigned val = 0;
459✔
653
        const char *s;
459✔
654

655
        s = *p;
459✔
656

657
        /* accept any number of digits, strtoull is limited to 19 */
658
        for (size_t i = 0; i < digits; i++, s++) {
2,711✔
659
                if (!ascii_isdigit(*s)) {
2,428✔
660
                        if (i == 0)
176✔
661
                                return -EINVAL;
662

663
                        /* too few digits, pad with 0 */
664
                        for (; i < digits; i++)
678✔
665
                                val *= 10;
502✔
666

667
                        break;
668
                }
669

670
                val *= 10;
2,252✔
671
                val += *s - '0';
2,252✔
672
        }
673

674
        /* maybe round up */
675
        if (*s >= '5' && *s <= '9')
459✔
676
                val++;
4✔
677

678
        s += strspn(s, DIGITS);
459✔
679

680
        *p = s;
459✔
681
        *res = val;
459✔
682

683
        return 0;
459✔
684
}
685

686
int parse_nice(const char *p, int *ret) {
248✔
687
        int n, r;
248✔
688

689
        r = safe_atoi(p, &n);
248✔
690
        if (r < 0)
248✔
691
                return r;
248✔
692

693
        if (!nice_is_valid(n))
242✔
694
                return -ERANGE;
695

696
        *ret = n;
236✔
697
        return 0;
236✔
698
}
699

700
int parse_ip_port(const char *s, uint16_t *ret) {
297✔
701
        uint16_t l;
297✔
702
        int r;
297✔
703

704
        r = safe_atou16_full(s, SAFE_ATO_REFUSE_LEADING_WHITESPACE, &l);
297✔
705
        if (r < 0)
297✔
706
                return r;
297✔
707

708
        if (l == 0)
224✔
709
                return -EINVAL;
710

711
        *ret = (uint16_t) l;
219✔
712

713
        return 0;
219✔
714
}
715

716
int parse_ip_port_range(const char *s, uint16_t *low, uint16_t *high, bool allow_zero) {
36✔
717
        unsigned l, h;
36✔
718
        int r;
36✔
719

720
        r = parse_range(s, &l, &h);
36✔
721
        if (r < 0)
36✔
722
                return r;
36✔
723

724
        if (l > 65535 || h > 65535)
26✔
725
                return -EINVAL;
726

727
        if (!allow_zero && (l == 0 || h == 0))
24✔
728
                return -EINVAL;
729

730
        if (h < l)
24✔
731
                return -EINVAL;
732

733
        *low = l;
23✔
734
        *high = h;
23✔
735

736
        return 0;
23✔
737
}
738

739
int parse_oom_score_adjust(const char *s, int *ret) {
612✔
740
        int r, v;
612✔
741

742
        assert(s);
612✔
743
        assert(ret);
612✔
744

745
        r = safe_atoi(s, &v);
612✔
746
        if (r < 0)
612✔
747
                return r;
612✔
748

749
        if (!oom_score_adjust_is_valid(v))
610✔
750
                return -ERANGE;
751

752
        *ret = v;
610✔
753
        return 0;
610✔
754
}
755

756
int store_loadavg_fixed_point(unsigned long i, unsigned long f, loadavg_t *ret) {
568✔
757
        assert(ret);
568✔
758

759
        if (i >= (~0UL << LOADAVG_PRECISION_BITS))
568✔
760
                return -ERANGE;
761

762
        i = i << LOADAVG_PRECISION_BITS;
567✔
763
        f = DIV_ROUND_UP((f << LOADAVG_PRECISION_BITS), 100);
567✔
764

765
        if (f >= LOADAVG_FIXED_POINT_1_0)
567✔
766
                return -ERANGE;
767

768
        *ret = i | f;
565✔
769
        return 0;
565✔
770
}
771

772
int parse_loadavg_fixed_point(const char *s, loadavg_t *ret) {
545✔
773
        const char *d, *f_str, *i_str;
545✔
774
        unsigned long i, f;
545✔
775
        int r;
545✔
776

777
        assert(s);
545✔
778
        assert(ret);
545✔
779

780
        d = strchr(s, '.');
545✔
781
        if (!d)
545✔
782
                return -EINVAL;
545✔
783

784
        i_str = strndupa_safe(s, d - s);
542✔
785
        f_str = d + 1;
542✔
786

787
        r = safe_atolu_full(i_str, 10, &i);
542✔
788
        if (r < 0)
542✔
789
                return r;
790

791
        r = safe_atolu_full(f_str, 10, &f);
540✔
792
        if (r < 0)
540✔
793
                return r;
794

795
        return store_loadavg_fixed_point(i, f, ret);
538✔
796
}
797

798
/* Limitations are described in https://www.netfilter.org/projects/nftables/manpage.html and
799
 * https://bugzilla.netfilter.org/show_bug.cgi?id=1175 */
800
bool nft_identifier_valid(const char *id) {
106✔
801
        if (isempty(id))
106✔
802
                return false;
803

804
        if (strlen(id) >= NFT_NAME_MAXLEN)
104✔
805
                return false;
806

807
        if (!ascii_isalpha(id[0]))
103✔
808
                return false;
809

810
        return in_charset(id + 1, ALPHANUMERICAL "/\\_.");
101✔
811
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc