Coverage Report

Created: 2026-11-12 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtpms/src/tpm2/RuntimeUtils.c
Line
Count
Source
1
// SPDX-License-Identifier: BSD-2-Clause
2
3
#include <stdbool.h>
4
#include <stdio.h>
5
#include <string.h>
6
7
#include <RuntimeUtils_fp.h>
8
9
/*
10
 * RuntimeUtilsDedupStrItems does in-place deduplication of comma-separated
11
 * items in a string. If an item contains '=' (rsa-min-size=) then the part
12
 * before the '=' is deduplicated. When deduplicating always the later item is
13
 * kept.
14
 */
15
void RuntimeUtilsDedupStrItems(char *input)
16
0
{
17
0
    char *comma, *equals, *dup, *ncomma;
18
0
    char *pos = input;
19
0
    size_t slen;
20
0
    bool found;
21
0
    char exp;
22
23
0
    while (true) {
24
0
        comma = strchr(pos, ',');
25
0
        if (!comma)
26
0
            return;
27
28
        /* temporarily terminate string here */
29
0
        *comma = '\0';
30
0
        equals = strchr(pos, '=');
31
0
        if (equals) {
32
0
            *equals = '\0';
33
0
            exp = '=';
34
0
            slen = equals - pos;
35
0
        } else {
36
0
            exp = ',';
37
0
            slen = comma - pos;
38
0
        }
39
40
0
        found = false;
41
0
        ncomma = comma;
42
        /* search for string after the comma */
43
0
        while (true) {
44
0
            dup = strstr(ncomma + 1, pos);
45
0
            if (dup) {
46
                /* ensure 'dup' is a whole token: valid left boundary AND right boundary */
47
0
                if ((dup[-1] == ',' || dup[-1] == 0) &&
48
0
                    (dup[slen] == exp || dup[slen] == 0)) {
49
0
                    memmove(pos, comma + 1, strlen(comma + 1) + 1);
50
                    /* keep pos as-is */
51
0
                    found = true;
52
0
                    break;
53
0
                }
54
                /* only a prefix matched; continue search after comma */
55
0
                ncomma = strchr(dup, ',');
56
0
                if (!ncomma)
57
0
                    break;
58
0
            } else {
59
0
                break;
60
0
            }
61
0
        }
62
0
        if (!found) {
63
0
            *comma = ',';
64
0
            if (equals)
65
0
               *equals = '=';
66
0
            pos = comma + 1;
67
0
        }
68
0
    }
69
0
}
70
71
#if defined(HAVE_MAIN)
72
/*
73
 * gcc src/tpm2/RuntimeUtils.c -DHAVE_MAIN -Isrc/tpm2 -o RuntimeUtils
74
 */
75
int main(void)
76
{
77
    char buffer[200];
78
    const char *exp;
79
    int ret = 0;
80
81
    strncpy(buffer,
82
            "foo=bar,fox=baz,foo=bar,xyz,abc,xyz,xyz,abc,foo,bar,other-foo,bar",
83
            sizeof(buffer) - 1);
84
    RuntimeUtilsDedupStrItems(buffer);
85
    exp = "fox=baz,foo=bar,xyz,abc,foo,other-foo,bar";
86
    if (strcmp(buffer, exp) != 0) {
87
        printf("Unexpected result string\nexpected: %s\nactual  : %s\n", exp, buffer);
88
        ret = 1;
89
    }
90
    return ret;
91
}
92
#endif