• 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

95.58
/src/basic/utf8.c
1
/* SPDX-License-Identifier: LGPL-2.0-or-later */
2

3
/* Parts of this file are based on the GLIB utf8 validation functions. The original copyright follows.
4
 *
5
 * gutf8.c - Operations on UTF-8 strings.
6
 * Copyright (C) 1999 Tom Tromey
7
 * Copyright (C) 2000 Red Hat, Inc.
8
 */
9

10
#include "alloc-util.h"
11
#include "gunicode.h"
12
#include "hexdecoct.h"
13
#include "string-util.h"
14
#include "utf8.h"
15

16
bool unichar_is_valid(char32_t ch) {
88,055✔
17

18
        if (ch >= 0x110000) /* End of unicode space */
88,055✔
19
                return false;
20
        if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
88,055✔
21
                return false;
22
        if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */
88,055✔
23
                return false;
24
        if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
88,055✔
25
                return false;
6✔
26

27
        return true;
28
}
29

30
static bool unichar_is_control(char32_t ch) {
120,845,390✔
31

32
        /*
33
          0 to ' '-1 is the C0 range.
34
          DEL=0x7F, and DEL+1 to 0x9F is C1 range.
35
          '\t' is in C0 range, but more or less harmless and commonly used.
36
        */
37

38
        return (ch < ' ' && !IN_SET(ch, '\t', '\n')) ||
120,845,390✔
39
                (0x7F <= ch && ch <= 0x9F);
120,845,212✔
40
}
41

42
/* count of characters used to encode one unicode char */
43
static size_t utf8_encoded_expected_len(uint8_t c) {
1,061,899,823✔
44
        if (c < 0x80)
1,061,899,823✔
45
                return 1;
46
        if ((c & 0xe0) == 0xc0)
283,558✔
47
                return 2;
48
        if ((c & 0xf0) == 0xe0)
256,443✔
49
                return 3;
50
        if ((c & 0xf8) == 0xf0)
854✔
51
                return 4;
52
        if ((c & 0xfc) == 0xf8)
64✔
53
                return 5;
54
        if ((c & 0xfe) == 0xfc)
64✔
55
                return 6;
×
56

57
        return 0;
58
}
59

60
/* decode one unicode char */
61
int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) {
131,729,027✔
62
        char32_t unichar;
131,729,027✔
63
        size_t len;
131,729,027✔
64

65
        assert(str);
131,729,027✔
66
        assert(ret_unichar);
131,729,027✔
67

68
        len = utf8_encoded_expected_len(str[0]);
131,729,027✔
69

70
        switch (len) {
131,729,027✔
71
        case 1:
131,534,783✔
72
                *ret_unichar = (char32_t)str[0];
131,534,783✔
73
                return 1;
131,534,783✔
74
        case 2:
22,364✔
75
                unichar = str[0] & 0x1f;
22,364✔
76
                break;
22,364✔
77
        case 3:
171,381✔
78
                unichar = (char32_t)str[0] & 0x0f;
171,381✔
79
                break;
171,381✔
80
        case 4:
499✔
81
                unichar = (char32_t)str[0] & 0x07;
499✔
82
                break;
499✔
83
        case 5:
×
84
                unichar = (char32_t)str[0] & 0x03;
×
85
                break;
×
86
        case 6:
×
87
                unichar = (char32_t)str[0] & 0x01;
×
UNCOV
88
                break;
×
89
        default:
90
                return -EINVAL;
91
        }
92

93
        for (size_t i = 1; i < len; i++) {
560,859✔
94
                if (((char32_t)str[i] & 0xc0) != 0x80)
366,621✔
95
                        return -EINVAL;
96

97
                unichar <<= 6;
366,615✔
98
                unichar |= (char32_t)str[i] & 0x3f;
366,615✔
99
        }
100

101
        *ret_unichar = unichar;
194,238✔
102
        return len;
194,238✔
103
}
104

105
bool utf8_is_printable_newline(const char* str, size_t length, bool allow_newline) {
3,975,395✔
106
        assert(str);
3,975,395✔
107

108
        for (const char *p = str; length > 0;) {
124,820,607✔
109
                int encoded_len;
120,845,391✔
110
                char32_t val;
120,845,391✔
111

112
                encoded_len = utf8_encoded_valid_unichar(p, length);
120,845,391✔
113
                if (encoded_len < 0)
120,845,391✔
114
                        return false;
179✔
115
                assert(encoded_len > 0 && (size_t) encoded_len <= length);
120,845,390✔
116

117
                if (utf8_encoded_to_unichar(p, &val) < 0 ||
120,845,390✔
118
                    unichar_is_control(val) ||
120,845,390✔
119
                    (!allow_newline && val == '\n'))
25,179,598✔
120
                        return false;
121

122
                length -= encoded_len;
120,845,212✔
123
                p += encoded_len;
120,845,212✔
124
        }
125

126
        return true;
127
}
128

129
char* utf8_is_valid_n(const char *str, size_t len_bytes) {
45,839,710✔
130
        /* Check if the string is composed of valid utf8 characters. If length len_bytes is given, stop after
131
         * len_bytes. Otherwise, stop at NUL. */
132

133
        assert(str);
45,839,710✔
134

135
        for (size_t i = 0; len_bytes != SIZE_MAX ? i < len_bytes : str[i] != '\0'; ) {
820,593,275✔
136
                int len;
774,754,694✔
137

138
                if (_unlikely_(str[i] == '\0'))
774,754,694✔
139
                        return NULL; /* embedded NUL */
140

141
                len = utf8_encoded_valid_unichar(str + i,
774,754,691✔
142
                                                 len_bytes != SIZE_MAX ? len_bytes - i : SIZE_MAX);
143
                if (_unlikely_(len < 0))
774,754,691✔
144
                        return NULL; /* invalid character */
145

146
                i += len;
774,753,565✔
147
        }
148

149
        return (char*) str;
150
}
151

152
char* utf8_escape_invalid(const char *str) {
12,337✔
153
        char *p, *s;
12,337✔
154

155
        assert(str);
12,337✔
156

157
        p = s = malloc(strlen(str) * 4 + 1);
12,337✔
158
        if (!p)
12,337✔
159
                return NULL;
160

161
        while (*str) {
287,214✔
162
                int len;
274,877✔
163

164
                len = utf8_encoded_valid_unichar(str, SIZE_MAX);
274,877✔
165
                if (len > 0) {
274,877✔
166
                        s = mempcpy(s, str, len);
274,861✔
167
                        str += len;
274,861✔
168
                } else {
169
                        s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER);
16✔
170
                        str += 1;
16✔
171
                }
172
        }
173

174
        *s = '\0';
12,337✔
175
        return str_realloc(p);
12,337✔
176
}
177

178
int utf8_char_console_width(const char *str) {
8,520,195✔
179
        char32_t c;
8,520,195✔
180
        int r;
8,520,195✔
181

182
        r = utf8_encoded_to_unichar(str, &c);
8,520,195✔
183
        if (r < 0)
8,520,195✔
184
                return r;
8,520,195✔
185

186
        if (c == '\t')
8,520,194✔
187
                return 8; /* Assume a tab width of 8 */
188

189
        /* TODO: we should detect combining characters */
190

191
        return unichar_iswide(c) ? 2 : 1;
8,478,681✔
192
}
193

194
char* utf8_escape_non_printable_full(const char *str, size_t console_width, bool force_ellipsis) {
1,327✔
195
        char *p, *s, *prev_s;
1,327✔
196
        size_t n = 0; /* estimated print width */
1,327✔
197

198
        assert(str);
1,327✔
199

200
        if (console_width == 0)
1,327✔
201
                return strdup("");
9✔
202

203
        p = s = prev_s = malloc(strlen(str) * 4 + 1);
1,318✔
204
        if (!p)
1,318✔
205
                return NULL;
206

207
        for (;;) {
52,259✔
208
                int len;
52,259✔
209
                char *saved_s = s;
52,259✔
210

211
                if (!*str) { /* done! */
52,259✔
212
                        if (force_ellipsis)
1,244✔
213
                                goto truncation;
73✔
214
                        else
215
                                goto finish;
1,171✔
216
                }
217

218
                len = utf8_encoded_valid_unichar(str, SIZE_MAX);
51,015✔
219
                if (len > 0) {
51,015✔
220
                        if (utf8_is_printable(str, len)) {
50,919✔
221
                                int w;
50,756✔
222

223
                                w = utf8_char_console_width(str);
50,756✔
224
                                assert(w >= 0);
50,756✔
225
                                if (n + w > console_width)
50,756✔
226
                                        goto truncation;
34✔
227

228
                                s = mempcpy(s, str, len);
50,722✔
229
                                str += len;
50,722✔
230
                                n += w;
50,722✔
231

232
                        } else {
233
                                for (; len > 0; len--) {
288✔
234
                                        if (n + 4 > console_width)
163✔
235
                                                goto truncation;
38✔
236

237
                                        *(s++) = '\\';
125✔
238
                                        *(s++) = 'x';
125✔
239
                                        *(s++) = hexchar((int) *str >> 4);
125✔
240
                                        *(s++) = hexchar((int) *str);
125✔
241

242
                                        str += 1;
125✔
243
                                        n += 4;
125✔
244
                                }
245
                        }
246
                } else {
247
                        if (n + 1 > console_width)
96✔
248
                                goto truncation;
2✔
249

250
                        s = mempcpy(s, UTF8_REPLACEMENT_CHARACTER, strlen(UTF8_REPLACEMENT_CHARACTER));
94✔
251
                        str += 1;
94✔
252
                        n += 1;
94✔
253
                }
254

255
                prev_s = saved_s;
256
        }
257

258
 truncation:
147✔
259
        /* Try to go back one if we don't have enough space for the ellipsis */
260
        if (n + 1 > console_width)
147✔
261
                s = prev_s;
88✔
262

263
        s = mempcpy(s, "…", strlen("…"));
147✔
264

265
 finish:
1,318✔
266
        *s = '\0';
1,318✔
267
        return str_realloc(p);
1,318✔
268
}
269

270
char* ascii_is_valid_n(const char *str, size_t len) {
65,784✔
271
        /* Check whether the string consists of valid ASCII bytes, i.e values between 1 and 127, inclusive.
272
         * Stops at len, or NUL byte if len is SIZE_MAX. */
273

274
        assert(str);
65,784✔
275

276
        for (size_t i = 0; len != SIZE_MAX ? i < len : str[i] != '\0'; i++)
1,681,148✔
277
                if ((unsigned char) str[i] >= 128 || str[i] == '\0')
1,618,765✔
278
                        return NULL;
279

280
        return (char*) str;
281
}
282

283
int utf8_to_ascii(const char *str, char replacement_char, char **ret) {
49✔
284
        /* Convert to a string that has only ASCII chars, replacing anything that is not ASCII
285
         * by replacement_char. */
286

287
        _cleanup_free_ char *ans = new(char, strlen(str) + 1);
98✔
288
        if (!ans)
49✔
289
                return -ENOMEM;
290

291
        char *q = ans;
292

293
        for (const char *p = str; *p; q++) {
385✔
294
                int l;
338✔
295

296
                l = utf8_encoded_valid_unichar(p, SIZE_MAX);
338✔
297
                if (l < 0)  /* Non-UTF-8, let's not even try to propagate the garbage */
338✔
298
                        return l;
299

300
                if (l == 1)
336✔
301
                        *q = *p;
319✔
302
                else
303
                        /* non-ASCII, we need to replace it */
304
                        *q = replacement_char;
17✔
305

306
                p += l;
336✔
307
        }
308
        *q = '\0';
47✔
309

310
        *ret = TAKE_PTR(ans);
47✔
311
        return 0;
47✔
312
}
313

314
/**
315
 * utf8_encode_unichar() - Encode single UCS-4 character as UTF-8
316
 * @out_utf8: output buffer of at least 4 bytes or NULL
317
 * @g: UCS-4 character to encode
318
 *
319
 * This encodes a single UCS-4 character as UTF-8 and writes it into @out_utf8.
320
 * The length of the character is returned. It is not zero-terminated! If the
321
 * output buffer is NULL, only the length is returned.
322
 *
323
 * Returns: The length in bytes that the UTF-8 representation does or would
324
 *          occupy.
325
 */
326
size_t utf8_encode_unichar(char *out_utf8, char32_t g) {
76,175✔
327

328
        if (g < (1 << 7)) {
76,175✔
329
                if (out_utf8)
76,051✔
330
                        out_utf8[0] = g & 0x7f;
76,051✔
331
                return 1;
76,051✔
332
        } else if (g < (1 << 11)) {
124✔
333
                if (out_utf8) {
17✔
334
                        out_utf8[0] = 0xc0 | ((g >> 6) & 0x1f);
17✔
335
                        out_utf8[1] = 0x80 | (g & 0x3f);
17✔
336
                }
337
                return 2;
17✔
338
        } else if (g < (1 << 16)) {
107✔
339
                if (out_utf8) {
96✔
340
                        out_utf8[0] = 0xe0 | ((g >> 12) & 0x0f);
96✔
341
                        out_utf8[1] = 0x80 | ((g >> 6) & 0x3f);
96✔
342
                        out_utf8[2] = 0x80 | (g & 0x3f);
96✔
343
                }
344
                return 3;
96✔
345
        } else if (g < (1 << 21)) {
11✔
346
                if (out_utf8) {
11✔
347
                        out_utf8[0] = 0xf0 | ((g >> 18) & 0x07);
11✔
348
                        out_utf8[1] = 0x80 | ((g >> 12) & 0x3f);
11✔
349
                        out_utf8[2] = 0x80 | ((g >> 6) & 0x3f);
11✔
350
                        out_utf8[3] = 0x80 | (g & 0x3f);
11✔
351
                }
352
                return 4;
11✔
353
        }
354

355
        return 0;
356
}
357

358
char* utf16_to_utf8(const char16_t *s, size_t length /* bytes! */) {
2,380✔
359
        const uint8_t *f;
2,380✔
360
        char *r, *t;
2,380✔
361

362
        if (length == 0)
2,380✔
UNCOV
363
                return new0(char, 1);
×
364

365
        assert(s);
2,380✔
366

367
        if (length == SIZE_MAX) {
2,380✔
368
                length = char16_strlen(s);
6✔
369

370
                if (length > SIZE_MAX/2)
6✔
371
                        return NULL; /* overflow */
372

373
                length *= 2;
6✔
374
        }
375

376
        /* Input length is in bytes, i.e. the shortest possible character takes 2 bytes. Each unicode character may
377
         * take up to 4 bytes in UTF-8. Let's also account for a trailing NUL byte. */
378
        if (length > (SIZE_MAX - 1) / 2)
2,380✔
379
                return NULL; /* overflow */
380

381
        r = new(char, length * 2 + 1);
2,380✔
382
        if (!r)
2,380✔
383
                return NULL;
384

385
        f = (const uint8_t*) s;
386
        t = r;
387

388
        while (f + 1 < (const uint8_t*) s + length) {
78,361✔
389
                char16_t w1, w2;
75,981✔
390

391
                /* see RFC 2781 section 2.2 */
392

393
                w1 = f[1] << 8 | f[0];
75,981✔
394
                f += 2;
75,981✔
395

396
                if (!utf16_is_surrogate(w1)) {
75,981✔
397
                        t += utf8_encode_unichar(t, w1);
75,974✔
398
                        continue;
75,974✔
399
                }
400

401
                if (utf16_is_trailing_surrogate(w1))
7✔
402
                        continue; /* spurious trailing surrogate, ignore */
1✔
403

404
                if (f + 1 >= (const uint8_t*) s + length)
6✔
405
                        break;
406

407
                w2 = f[1] << 8 | f[0];
6✔
408
                f += 2;
6✔
409

410
                if (!utf16_is_trailing_surrogate(w2)) {
6✔
411
                        f -= 2;
1✔
412
                        continue; /* surrogate missing its trailing surrogate, ignore */
1✔
413
                }
414

415
                t += utf8_encode_unichar(t, utf16_surrogate_pair_to_unichar(w1, w2));
5✔
416
        }
417

418
        *t = 0;
2,380✔
419
        return r;
2,380✔
420
}
421

422
size_t utf16_encode_unichar(char16_t *out, char32_t c) {
16✔
423

424
        /* Note that this encodes as little-endian. */
425

426
        switch (c) {
16✔
427

428
        case 0 ... 0xd7ffU:
12✔
429
        case 0xe000U ... 0xffffU:
430
                out[0] = htole16(c);
12✔
431
                return 1;
12✔
432

433
        case 0x10000U ... 0x10ffffU:
4✔
434
                c -= 0x10000U;
4✔
435
                out[0] = htole16((c >> 10) + 0xd800U);
4✔
436
                out[1] = htole16((c & 0x3ffU) + 0xdc00U);
4✔
437
                return 2;
4✔
438

439
        default: /* A surrogate (invalid) */
440
                return 0;
441
        }
442
}
443

444
char16_t *utf8_to_utf16(const char *s, size_t length) {
367✔
445
        char16_t *n, *p;
367✔
446
        int r;
367✔
447

448
        if (length == 0)
367✔
UNCOV
449
                return new0(char16_t, 1);
×
450

451
        assert(s);
367✔
452

453
        if (length == SIZE_MAX)
367✔
454
                length = strlen(s);
366✔
455

456
        if (length > SIZE_MAX - 1)
366✔
457
                return NULL; /* overflow */
458

459
        n = new(char16_t, length + 1);
367✔
460
        if (!n)
367✔
461
                return NULL;
462

463
        p = n;
464

465
        for (size_t i = 0; i < length;) {
5,963✔
466
                char32_t unichar;
5,596✔
467
                size_t e;
5,596✔
468

469
                e = utf8_encoded_expected_len(s[i]);
5,596✔
470
                if (e <= 1) /* Invalid and single byte characters are copied as they are */
5,596✔
471
                        goto copy;
5,580✔
472

473
                if (i + e > length) /* sequence longer than input buffer, then copy as-is */
16✔
UNCOV
474
                        goto copy;
×
475

476
                r = utf8_encoded_to_unichar(s + i, &unichar);
16✔
477
                if (r < 0) /* sequence invalid, then copy as-is */
16✔
UNCOV
478
                        goto copy;
×
479

480
                p += utf16_encode_unichar(p, unichar);
16✔
481
                i += e;
16✔
482
                continue;
16✔
483

484
        copy:
5,580✔
485
                *(p++) = htole16(s[i++]);
5,580✔
486
        }
487

488
        *p = 0;
367✔
489
        return n;
367✔
490
}
491

492
size_t char16_strlen(const char16_t *s) {
631✔
493
        size_t n = 0;
631✔
494

495
        assert(s);
631✔
496

497
        while (*s != 0)
9,408✔
498
                n++, s++;
8,777✔
499

500
        return n;
631✔
501
}
502

503
size_t char16_strsize(const char16_t *s) {
3✔
504
        POINTER_MAY_BE_NULL(s);
3✔
505

506
        return s ? (char16_strlen(s) + 1) * sizeof(*s) : 0;
3✔
507
}
508

509
/* expected size used to encode one unicode char */
510
static int utf8_unichar_to_encoded_len(char32_t unichar) {
88,045✔
511

512
        if (unichar < 0x80)
88,045✔
513
                return 1;
514
        if (unichar < 0x800)
88,045✔
515
                return 2;
516
        if (unichar < 0x10000)
83,621✔
517
                return 3;
518
        if (unichar < 0x200000)
285✔
519
                return 4;
UNCOV
520
        if (unichar < 0x4000000)
×
UNCOV
521
                return 5;
×
522

523
        return 6;
524
}
525

526
/* validate one encoded unicode char and return its length */
527
int utf8_encoded_valid_unichar(const char *str, size_t length /* bytes */) {
930,165,200✔
528
        char32_t unichar;
930,165,200✔
529
        size_t len;
930,165,200✔
530
        int r;
930,165,200✔
531

532
        assert(str);
930,165,200✔
533
        assert(length > 0);
930,165,200✔
534

535
        /* We read until NUL, at most length bytes. SIZE_MAX may be used to disable the length check. */
536

537
        len = utf8_encoded_expected_len(str[0]);
930,165,200✔
538
        if (len == 0)
930,165,200✔
539
                return -EINVAL;
930,165,200✔
540

541
        /* Do we have a truncated multi-byte character? */
542
        if (len > length)
930,165,136✔
543
                return -EINVAL;
544

545
        /* ascii is valid */
546
        if (len == 1)
930,165,128✔
547
                return 1;
548

549
        /* check if expected encoded chars are available */
550
        for (size_t i = 0; i < len; i++)
350,867✔
551
                if ((str[i] & 0x80) != 0x80)
262,817✔
552
                        return -EINVAL;
553

554
        r = utf8_encoded_to_unichar(str, &unichar);
88,050✔
555
        if (r < 0)
88,050✔
556
                return r;
557

558
        /* check if encoded length matches encoded value */
559
        if (utf8_unichar_to_encoded_len(unichar) != (int) len)
88,045✔
560
                return -EINVAL;
561

562
        /* check if value has valid range */
563
        if (!unichar_is_valid(unichar))
88,045✔
564
                return -EINVAL;
6✔
565

566
        return (int) len;
567
}
568

569
size_t utf8_n_codepoints(const char *str) {
6✔
570
        size_t n = 0;
6✔
571

572
        assert(str);
6✔
573

574
        /* Returns the number of UTF-8 codepoints in this string, or SIZE_MAX if the string is not valid UTF-8. */
575

576
        while (*str != 0) {
34✔
577
                int k;
29✔
578

579
                k = utf8_encoded_valid_unichar(str, SIZE_MAX);
29✔
580
                if (k < 0)
29✔
581
                        return SIZE_MAX;
582

583
                str += k;
28✔
584
                n++;
28✔
585
        }
586

587
        return n;
588
}
589

590
size_t utf8_console_width(const char *str) {
473,332✔
591
        POINTER_MAY_BE_NULL(str);
473,332✔
592

593
        if (isempty(str))
473,332✔
594
                return 0;
595

596
        /* Returns the approximate width a string will take on screen when printed on a character cell
597
         * terminal/console. */
598

599
        size_t n = 0;
600
        while (*str) {
6,575,086✔
601
                int w;
6,120,915✔
602

603
                w = utf8_char_console_width(str);
6,120,915✔
604
                if (w < 0)
6,120,915✔
605
                        return SIZE_MAX;
606

607
                n += w;
6,120,914✔
608
                str = utf8_next_char(str);
6,120,914✔
609
        }
610

611
        return n;
612
}
613

614
size_t utf8_last_length(const char *s, size_t n) {
9✔
615
        int r;
9✔
616

617
        assert(s);
9✔
618

619
        if (n == SIZE_MAX)
9✔
620
                n = strlen(s);
7✔
621

622
        /* Determines length in bytes of last UTF-8 codepoint in string. If the string is empty, returns
623
         * zero. Treats invalid UTF-8 codepoints as 1 sized ones. */
624

625
        for (size_t last = 0;;) {
19✔
626
                if (n == 0)
28✔
627
                        return last;
9✔
628

629
                r = utf8_encoded_valid_unichar(s, n);
19✔
630
                if (r <= 0)
19✔
UNCOV
631
                        r = 1; /* treat invalid UTF-8 as byte-wide */
×
632

633
                s += r;
19✔
634
                n -= r;
19✔
635
                last = r;
19✔
636
        }
637
}
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