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

systemd / systemd / 14554080340

19 Apr 2025 11:46AM UTC coverage: 72.101% (-0.03%) from 72.13%
14554080340

push

github

web-flow
Add two new paragraphs to coding style about header files (#37188)

296880 of 411754 relevant lines covered (72.1%)

687547.52 hits per line

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

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

3
#include <errno.h>
4
#include <inttypes.h>
5
#include <linux/ipv6.h>
6
#include <linux/netfilter/nf_tables.h>
7
#include <net/if.h>
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <sys/socket.h>
11

12
#include "alloc-util.h"
13
#include "errno-list.h"
14
#include "extract-word.h"
15
#include "locale-util.h"
16
#include "log.h"
17
#include "macro.h"
18
#include "missing_network.h"
19
#include "parse-util.h"
20
#include "process-util.h"
21
#include "stat-util.h"
22
#include "string-util.h"
23
#include "strv.h"
24

25
int parse_boolean(const char *v) {
623,556✔
26
        if (!v)
623,556✔
27
                return -EINVAL;
28

29
        if (STRCASE_IN_SET(v,
623,556✔
30
                           "1",
31
                           "yes",
32
                           "y",
33
                           "true",
34
                           "t",
35
                           "on"))
36
                return 1;
195,139✔
37

38
        if (STRCASE_IN_SET(v,
428,417✔
39
                           "0",
40
                           "no",
41
                           "n",
42
                           "false",
43
                           "f",
44
                           "off"))
45
                return 0;
404,426✔
46

47
        return -EINVAL;
23,991✔
48
}
49

50
int parse_tristate_full(const char *v, const char *third, int *ret) {
6,857✔
51
        int r;
6,857✔
52

53
        if (isempty(v) || streq_ptr(v, third)) { /* Empty string is always taken as the third/invalid/auto state */
13,714✔
54
                if (ret)
×
55
                        *ret = -1;
×
56
        } else {
57
                r = parse_boolean(v);
6,857✔
58
                if (r < 0)
6,857✔
59
                        return r;
60

61
                if (ret)
6,857✔
62
                        *ret = r;
6,857✔
63
        }
64

65
        return 0;
66
}
67

68
int parse_pid(const char *s, pid_t* ret_pid) {
97,592✔
69
        unsigned long ul = 0;
97,592✔
70
        pid_t pid;
97,592✔
71
        int r;
97,592✔
72

73
        assert(s);
97,592✔
74

75
        r = safe_atolu(s, &ul);
97,592✔
76
        if (r < 0)
97,592✔
77
                return r;
97,592✔
78

79
        pid = (pid_t) ul;
97,450✔
80

81
        if ((unsigned long) pid != ul)
97,450✔
82
                return -ERANGE;
83

84
        if (!pid_is_valid(pid))
97,450✔
85
                return -ERANGE;
86

87
        if (ret_pid)
97,448✔
88
                *ret_pid = pid;
97,446✔
89
        return 0;
90
}
91

92
int parse_mode(const char *s, mode_t *ret) {
110,957✔
93
        unsigned m;
110,957✔
94
        int r;
110,957✔
95

96
        assert(s);
110,957✔
97

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

109
        if (ret)
110,941✔
110
                *ret = m;
110,941✔
111
        return 0;
112
}
113

114
int parse_ifindex(const char *s) {
159,816✔
115
        int ifi, r;
159,816✔
116

117
        assert(s);
159,816✔
118

119
        r = safe_atoi(s, &ifi);
159,816✔
120
        if (r < 0)
159,816✔
121
                return r;
159,816✔
122
        if (ifi <= 0)
37,840✔
123
                return -EINVAL;
2✔
124

125
        return ifi;
126
}
127

128
int parse_mtu(int family, const char *s, uint32_t *ret) {
206✔
129
        uint64_t u, m;
206✔
130
        int r;
206✔
131

132
        r = parse_size(s, 1024, &u);
206✔
133
        if (r < 0)
206✔
134
                return r;
206✔
135

136
        if (u > UINT32_MAX)
185✔
137
                return -ERANGE;
138

139
        switch (family) {
181✔
140
        case AF_INET:
141
                m = IPV4_MIN_MTU; /* This is 68 */
142
                break;
143
        case AF_INET6:
27✔
144
                m = IPV6_MIN_MTU; /* This is 1280 */
27✔
145
                break;
27✔
146
        default:
147
                m = 0;
148
        }
149

150
        if (u < m)
37✔
151
                return -ERANGE;
152

153
        *ret = (uint32_t) u;
177✔
154
        return 0;
177✔
155
}
156

157
int parse_size(const char *t, uint64_t base, uint64_t *size) {
12,962✔
158

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

173
        struct table {
12,962✔
174
                const char *suffix;
175
                unsigned long long factor;
176
        };
177

178
        static const struct table iec[] = {
12,962✔
179
                { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
180
                { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
181
                { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
182
                { "G", 1024ULL*1024ULL*1024ULL },
183
                { "M", 1024ULL*1024ULL },
184
                { "K", 1024ULL },
185
                { "B", 1ULL },
186
                { "",  1ULL },
187
        };
188

189
        static const struct table si[] = {
12,962✔
190
                { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
191
                { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
192
                { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
193
                { "G", 1000ULL*1000ULL*1000ULL },
194
                { "M", 1000ULL*1000ULL },
195
                { "K", 1000ULL },
196
                { "B", 1ULL },
197
                { "",  1ULL },
198
        };
199

200
        const struct table *table;
12,962✔
201
        const char *p;
12,962✔
202
        unsigned long long r = 0;
12,962✔
203
        unsigned n_entries, start_pos = 0;
12,962✔
204

205
        assert(t);
12,962✔
206
        assert(IN_SET(base, 1000, 1024));
12,962✔
207
        assert(size);
12,962✔
208

209
        if (base == 1000) {
12,962✔
210
                table = si;
211
                n_entries = ELEMENTSOF(si);
212
        } else {
213
                table = iec;
12,905✔
214
                n_entries = ELEMENTSOF(iec);
12,905✔
215
        }
216

217
        p = t;
12,962✔
218
        do {
13,017✔
219
                unsigned long long l, tmp;
13,017✔
220
                double frac = 0;
13,017✔
221
                char *e;
13,017✔
222
                unsigned i;
13,017✔
223

224
                p += strspn(p, WHITESPACE);
13,017✔
225

226
                errno = 0;
13,017✔
227
                l = strtoull(p, &e, 10);
13,017✔
228
                if (errno > 0)
13,017✔
229
                        return -errno;
55✔
230
                if (e == p)
13,015✔
231
                        return -EINVAL;
232
                if (*p == '-')
12,978✔
233
                        return -ERANGE;
234

235
                if (*e == '.') {
12,966✔
236
                        e++;
42✔
237

238
                        /* strtoull() itself would accept space/+/- */
239
                        if (ascii_isdigit(*e)) {
42✔
240
                                unsigned long long l2;
38✔
241
                                char *e2;
38✔
242

243
                                l2 = strtoull(e, &e2, 10);
38✔
244
                                if (errno > 0)
38✔
245
                                        return -errno;
×
246

247
                                /* Ignore failure. E.g. 10.M is valid */
248
                                frac = l2;
38✔
249
                                for (; e < e2; e++)
84✔
250
                                        frac /= 10;
46✔
251
                        }
252
                }
253

254
                e += strspn(e, WHITESPACE);
12,966✔
255

256
                for (i = start_pos; i < n_entries; i++)
99,982✔
257
                        if (startswith(e, table[i].suffix))
99,980✔
258
                                break;
259

260
                if (i >= n_entries)
12,964✔
261
                        return -EINVAL;
262

263
                if (l + (frac > 0) > ULLONG_MAX / table[i].factor)
12,964✔
264
                        return -ERANGE;
265

266
                tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor);
12,962✔
267
                if (tmp > ULLONG_MAX - r)
12,962✔
268
                        return -ERANGE;
269

270
                r += tmp;
12,962✔
271
                if ((unsigned long long) (uint64_t) r != r)
12,962✔
272
                        return -ERANGE;
273

274
                p = e + strlen(table[i].suffix);
12,962✔
275

276
                start_pos = i + 1;
12,962✔
277

278
        } while (*p);
12,962✔
279

280
        *size = r;
12,907✔
281

282
        return 0;
12,907✔
283
}
284

285
int parse_sector_size(const char *t, uint64_t *ret) {
4✔
286
        int r;
4✔
287

288
        assert(t);
4✔
289
        assert(ret);
4✔
290

291
        uint64_t ss;
4✔
292

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

301
        *ret = ss;
4✔
302
        return 0;
4✔
303
}
304

305
int parse_range(const char *t, unsigned *lower, unsigned *upper) {
331✔
306
        _cleanup_free_ char *word = NULL;
331✔
307
        unsigned l, u;
331✔
308
        int r;
331✔
309

310
        assert(lower);
331✔
311
        assert(upper);
331✔
312

313
        /* Extract the lower bound. */
314
        r = extract_first_word(&t, &word, "-", EXTRACT_DONT_COALESCE_SEPARATORS);
331✔
315
        if (r < 0)
331✔
316
                return r;
317
        if (r == 0)
331✔
318
                return -EINVAL;
319

320
        r = safe_atou(word, &l);
331✔
321
        if (r < 0)
331✔
322
                return r;
323

324
        /* Check for the upper bound and extract it if needed */
325
        if (!t)
302✔
326
                /* Single number with no dashes. */
327
                u = l;
105✔
328
        else if (!*t)
197✔
329
                /* Trailing dash is an error. */
330
                return -EINVAL;
331
        else {
332
                r = safe_atou(t, &u);
196✔
333
                if (r < 0)
196✔
334
                        return r;
335
        }
336

337
        *lower = l;
289✔
338
        *upper = u;
289✔
339
        return 0;
289✔
340
}
341

342
int parse_errno(const char *t) {
1,567✔
343
        int r, e;
1,567✔
344

345
        assert(t);
1,567✔
346

347
        r = errno_from_name(t);
1,567✔
348
        if (r > 0)
1,567✔
349
                return r;
1,567✔
350

351
        r = safe_atoi(t, &e);
27✔
352
        if (r < 0)
27✔
353
                return r;
354

355
        /* 0 is also allowed here */
356
        if (!errno_is_valid(e) && e != 0)
12✔
357
                return -ERANGE;
5✔
358

359
        return e;
360
}
361

362
int parse_fd(const char *t) {
171,277✔
363
        int r, fd;
171,277✔
364

365
        assert(t);
171,277✔
366

367
        r = safe_atoi(t, &fd);
171,277✔
368
        if (r < 0)
171,277✔
369
                return r;
171,277✔
370

371
        if (fd < 0)
171,273✔
372
                return -EBADF;
2✔
373

374
        return fd;
375
}
376

377
static const char *mangle_base(const char *s, unsigned *base) {
5,697,002✔
378
        const char *k;
5,697,002✔
379

380
        assert(s);
5,697,002✔
381
        assert(base);
5,697,002✔
382

383
        /* Base already explicitly specified, then don't do anything. */
384
        if (SAFE_ATO_MASK_FLAGS(*base) != 0)
5,697,002✔
385
                return s;
5,697,002✔
386

387
        /* Support Python 3 style "0b" and 0x" prefixes, because they truly make sense, much more than C's "0" prefix for octal. */
388
        k = STARTSWITH_SET(s, "0b", "0B");
5,324,373✔
389
        if (k) {
5,324,373✔
390
                *base = 2 | (*base & SAFE_ATO_ALL_FLAGS);
4✔
391
                return k;
4✔
392
        }
393

394
        k = STARTSWITH_SET(s, "0o", "0O");
5,324,369✔
395
        if (k) {
5,324,369✔
396
                *base = 8 | (*base & SAFE_ATO_ALL_FLAGS);
4✔
397
                return k;
4✔
398
        }
399

400
        return s;
401
}
402

403
int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
1,921,292✔
404
        char *x = NULL;
1,921,292✔
405
        unsigned long l;
1,921,292✔
406

407
        assert(s);
1,921,292✔
408
        assert(SAFE_ATO_MASK_FLAGS(base) <= 16);
1,921,292✔
409

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

415
        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_WHITESPACE) &&
1,921,292✔
416
            strchr(WHITESPACE, s[0]))
197,425✔
417
                return -EINVAL;
1,921,292✔
418

419
        s += strspn(s, WHITESPACE);
1,921,275✔
420

421
        if (FLAGS_SET(base, SAFE_ATO_REFUSE_PLUS_MINUS) &&
1,921,275✔
422
            IN_SET(s[0], '+', '-'))
308,078✔
423
                return -EINVAL; /* Note that we check the "-" prefix again a second time below, but return a
424
                                 * different error. I.e. if the SAFE_ATO_REFUSE_PLUS_MINUS flag is set we
425
                                 * blanket refuse +/- prefixed integers, while if it is missing we'll just
426
                                 * return ERANGE, because the string actually parses correctly, but doesn't
427
                                 * fit in the return type. */
428

429
        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_ZERO) &&
1,921,258✔
430
            s[0] == '0' && !streq(s, "0"))
196,486✔
431
                return -EINVAL; /* This is particularly useful to avoid ambiguities between C's octal
432
                                 * notation and assumed-to-be-decimal integers with a leading zero. */
433

434
        s = mangle_base(s, &base);
1,921,238✔
435

436
        errno = 0;
1,921,238✔
437
        l = strtoul(s, &x, SAFE_ATO_MASK_FLAGS(base) /* Let's mask off the flags bits so that only the actual
1,921,238✔
438
                                                      * base is left */);
439
        if (errno > 0)
1,921,238✔
440
                return -errno;
2✔
441
        if (!x || x == s || *x != 0)
1,921,236✔
442
                return -EINVAL;
443
        if (l != 0 && s[0] == '-')
1,723,317✔
444
                return -ERANGE;
445
        if ((unsigned long) (unsigned) l != l)
1,622,000✔
446
                return -ERANGE;
447

448
        if (ret_u)
1,723,289✔
449
                *ret_u = (unsigned) l;
1,723,212✔
450

451
        return 0;
452
}
453

454
int safe_atou_bounded(const char *s, unsigned min, unsigned max, unsigned *ret) {
94✔
455
        unsigned v;
94✔
456
        int r;
94✔
457

458
        r = safe_atou(s, &v);
94✔
459
        if (r < 0)
94✔
460
                return r;
94✔
461

462
        if (v < min || v > max)
93✔
463
                return -ERANGE;
464

465
        *ret = v;
91✔
466
        return 0;
91✔
467
}
468

469
int safe_atoi(const char *s, int *ret_i) {
2,242,351✔
470
        unsigned base = 0;
2,242,351✔
471
        char *x = NULL;
2,242,351✔
472
        long l;
2,242,351✔
473

474
        assert(s);
2,242,351✔
475

476
        s += strspn(s, WHITESPACE);
2,242,351✔
477
        s = mangle_base(s, &base);
2,242,351✔
478

479
        errno = 0;
2,242,351✔
480
        l = strtol(s, &x, base);
2,242,351✔
481
        if (errno > 0)
2,242,351✔
482
                return -errno;
8✔
483
        if (!x || x == s || *x != 0)
2,242,343✔
484
                return -EINVAL;
485
        if ((long) (int) l != l)
2,108,819✔
486
                return -ERANGE;
487

488
        if (ret_i)
2,108,813✔
489
                *ret_i = (int) l;
2,108,539✔
490

491
        return 0;
492
}
493

494
int safe_atollu_full(const char *s, unsigned base, unsigned long long *ret_llu) {
1,533,364✔
495
        char *x = NULL;
1,533,364✔
496
        unsigned long long l;
1,533,364✔
497

498
        assert(s);
1,533,364✔
499
        assert(SAFE_ATO_MASK_FLAGS(base) <= 16);
1,533,364✔
500

501
        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_WHITESPACE) &&
1,533,364✔
502
            strchr(WHITESPACE, s[0]))
×
503
                return -EINVAL;
1,533,364✔
504

505
        s += strspn(s, WHITESPACE);
1,533,364✔
506

507
        if (FLAGS_SET(base, SAFE_ATO_REFUSE_PLUS_MINUS) &&
1,533,364✔
508
            IN_SET(s[0], '+', '-'))
×
509
                return -EINVAL;
510

511
        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_ZERO) &&
1,533,364✔
512
            s[0] == '0' && s[1] != 0)
×
513
                return -EINVAL;
514

515
        s = mangle_base(s, &base);
1,533,364✔
516

517
        errno = 0;
1,533,364✔
518
        l = strtoull(s, &x, SAFE_ATO_MASK_FLAGS(base));
1,533,364✔
519
        if (errno > 0)
1,533,364✔
520
                return -errno;
3✔
521
        if (!x || x == s || *x != 0)
1,533,361✔
522
                return -EINVAL;
523
        if (l != 0 && s[0] == '-')
1,533,203✔
524
                return -ERANGE;
525

526
        if (ret_llu)
1,533,196✔
527
                *ret_llu = l;
1,533,196✔
528

529
        return 0;
530
}
531

532
int safe_atolli(const char *s, long long *ret_lli) {
35✔
533
        unsigned base = 0;
35✔
534
        char *x = NULL;
35✔
535
        long long l;
35✔
536

537
        assert(s);
35✔
538

539
        s += strspn(s, WHITESPACE);
35✔
540
        s = mangle_base(s, &base);
35✔
541

542
        errno = 0;
35✔
543
        l = strtoll(s, &x, base);
35✔
544
        if (errno > 0)
35✔
545
                return -errno;
4✔
546
        if (!x || x == s || *x != 0)
31✔
547
                return -EINVAL;
548

549
        if (ret_lli)
23✔
550
                *ret_lli = l;
23✔
551

552
        return 0;
553
}
554

555
int safe_atou8_full(const char *s, unsigned base, uint8_t *ret) {
143,298✔
556
        unsigned u;
143,298✔
557
        int r;
143,298✔
558

559
        r = safe_atou_full(s, base, &u);
143,298✔
560
        if (r < 0)
143,298✔
561
                return r;
143,298✔
562
        if (u > UINT8_MAX)
143,184✔
563
                return -ERANGE;
564

565
        *ret = (uint8_t) u;
143,183✔
566
        return 0;
143,183✔
567
}
568

569
int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) {
3,713✔
570
        unsigned u;
3,713✔
571
        int r;
3,713✔
572

573
        r = safe_atou_full(s, base, &u);
3,713✔
574
        if (r < 0)
3,713✔
575
                return r;
3,713✔
576
        if (u > UINT16_MAX)
3,630✔
577
                return -ERANGE;
578

579
        *ret = (uint16_t) u;
3,623✔
580
        return 0;
3,623✔
581
}
582

583
int safe_atoi16(const char *s, int16_t *ret) {
14✔
584
        unsigned base = 0;
14✔
585
        char *x = NULL;
14✔
586
        long l;
14✔
587

588
        assert(s);
14✔
589

590
        s += strspn(s, WHITESPACE);
14✔
591
        s = mangle_base(s, &base);
14✔
592

593
        errno = 0;
14✔
594
        l = strtol(s, &x, base);
14✔
595
        if (errno > 0)
14✔
596
                return -errno;
×
597
        if (!x || x == s || *x != 0)
14✔
598
                return -EINVAL;
599
        if ((long) (int16_t) l != l)
10✔
600
                return -ERANGE;
601

602
        if (ret)
8✔
603
                *ret = (int16_t) l;
8✔
604

605
        return 0;
606
}
607

608
int safe_atod(const char *s, double *ret_d) {
12✔
609
        _cleanup_(freelocalep) locale_t loc = (locale_t) 0;
24✔
610
        char *x = NULL;
12✔
611
        double d = 0;
12✔
612

613
        assert(s);
12✔
614

615
        loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
12✔
616
        if (loc == (locale_t) 0)
12✔
617
                return -errno;
×
618

619
        errno = 0;
12✔
620
        d = strtod_l(s, &x, loc);
12✔
621
        if (errno > 0)
12✔
622
                return -errno;
×
623
        if (!x || x == s || *x != 0)
12✔
624
                return -EINVAL;
625

626
        if (ret_d)
7✔
627
                *ret_d = (double) d;
7✔
628

629
        return 0;
630
}
631

632
int parse_fractional_part_u(const char **p, size_t digits, unsigned *res) {
457✔
633
        unsigned val = 0;
457✔
634
        const char *s;
457✔
635

636
        s = *p;
457✔
637

638
        /* accept any number of digits, strtoull is limited to 19 */
639
        for (size_t i = 0; i < digits; i++, s++) {
2,697✔
640
                if (!ascii_isdigit(*s)) {
2,416✔
641
                        if (i == 0)
176✔
642
                                return -EINVAL;
643

644
                        /* too few digits, pad with 0 */
645
                        for (; i < digits; i++)
678✔
646
                                val *= 10;
502✔
647

648
                        break;
649
                }
650

651
                val *= 10;
2,240✔
652
                val += *s - '0';
2,240✔
653
        }
654

655
        /* maybe round up */
656
        if (*s >= '5' && *s <= '9')
457✔
657
                val++;
4✔
658

659
        s += strspn(s, DIGITS);
457✔
660

661
        *p = s;
457✔
662
        *res = val;
457✔
663

664
        return 0;
457✔
665
}
666

667
int parse_nice(const char *p, int *ret) {
265✔
668
        int n, r;
265✔
669

670
        r = safe_atoi(p, &n);
265✔
671
        if (r < 0)
265✔
672
                return r;
265✔
673

674
        if (!nice_is_valid(n))
259✔
675
                return -ERANGE;
676

677
        *ret = n;
253✔
678
        return 0;
253✔
679
}
680

681
int parse_ip_port(const char *s, uint16_t *ret) {
291✔
682
        uint16_t l;
291✔
683
        int r;
291✔
684

685
        r = safe_atou16_full(s, SAFE_ATO_REFUSE_LEADING_WHITESPACE, &l);
291✔
686
        if (r < 0)
291✔
687
                return r;
291✔
688

689
        if (l == 0)
218✔
690
                return -EINVAL;
691

692
        *ret = (uint16_t) l;
213✔
693

694
        return 0;
213✔
695
}
696

697
int parse_ip_port_range(const char *s, uint16_t *low, uint16_t *high, bool allow_zero) {
32✔
698
        unsigned l, h;
32✔
699
        int r;
32✔
700

701
        r = parse_range(s, &l, &h);
32✔
702
        if (r < 0)
32✔
703
                return r;
32✔
704

705
        if (l > 65535 || h > 65535)
22✔
706
                return -EINVAL;
707

708
        if (!allow_zero && (l == 0 || h == 0))
20✔
709
                return -EINVAL;
710

711
        if (h < l)
20✔
712
                return -EINVAL;
713

714
        *low = l;
19✔
715
        *high = h;
19✔
716

717
        return 0;
19✔
718
}
719

720
int parse_oom_score_adjust(const char *s, int *ret) {
680✔
721
        int r, v;
680✔
722

723
        assert(s);
680✔
724
        assert(ret);
680✔
725

726
        r = safe_atoi(s, &v);
680✔
727
        if (r < 0)
680✔
728
                return r;
680✔
729

730
        if (!oom_score_adjust_is_valid(v))
678✔
731
                return -ERANGE;
732

733
        *ret = v;
678✔
734
        return 0;
678✔
735
}
736

737
int store_loadavg_fixed_point(unsigned long i, unsigned long f, loadavg_t *ret) {
517✔
738
        assert(ret);
517✔
739

740
        if (i >= (~0UL << LOADAVG_PRECISION_BITS))
517✔
741
                return -ERANGE;
742

743
        i = i << LOADAVG_PRECISION_BITS;
516✔
744
        f = DIV_ROUND_UP((f << LOADAVG_PRECISION_BITS), 100);
516✔
745

746
        if (f >= LOADAVG_FIXED_POINT_1_0)
516✔
747
                return -ERANGE;
748

749
        *ret = i | f;
514✔
750
        return 0;
514✔
751
}
752

753
int parse_loadavg_fixed_point(const char *s, loadavg_t *ret) {
494✔
754
        const char *d, *f_str, *i_str;
494✔
755
        unsigned long i, f;
494✔
756
        int r;
494✔
757

758
        assert(s);
494✔
759
        assert(ret);
494✔
760

761
        d = strchr(s, '.');
494✔
762
        if (!d)
494✔
763
                return -EINVAL;
494✔
764

765
        i_str = strndupa_safe(s, d - s);
491✔
766
        f_str = d + 1;
491✔
767

768
        r = safe_atolu_full(i_str, 10, &i);
491✔
769
        if (r < 0)
491✔
770
                return r;
771

772
        r = safe_atolu_full(f_str, 10, &f);
489✔
773
        if (r < 0)
489✔
774
                return r;
775

776
        return store_loadavg_fixed_point(i, f, ret);
487✔
777
}
778

779
/* Limitations are described in https://www.netfilter.org/projects/nftables/manpage.html and
780
 * https://bugzilla.netfilter.org/show_bug.cgi?id=1175 */
781
bool nft_identifier_valid(const char *id) {
102✔
782
        if (isempty(id))
102✔
783
                return false;
784

785
        if (strlen(id) >= NFT_NAME_MAXLEN)
100✔
786
                return false;
787

788
        if (!ascii_isalpha(id[0]))
99✔
789
                return false;
790

791
        return in_charset(id + 1, ALPHANUMERICAL "/\\_.");
97✔
792
}
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