GCC Code Coverage Report


Directory: ./
File: src/misspell.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 51 51 100.0%
Functions: 4 4 100.0%
Branches: 30 30 100.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2018 Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 (or any
6 * later) as published by the Free Software Foundation.
7 */
8
9 #include <nft.h>
10
11 #include <limits.h>
12 #include <utils.h>
13 #include <misspell.h>
14
15 enum string_distance_function {
16 DELETION = 0, /* m1 */
17 INSERTION, /* m2 */
18 TRANSFORMATION, /* m3 */
19 };
20 #define DISTANCE_MAX (TRANSFORMATION + 1)
21
22 14287 static unsigned int min_distance(unsigned int *cost)
23 {
24 14287 unsigned int min = UINT_MAX;
25 int k;
26
27
2/2
✓ Branch 0 (6→3) taken 42861 times.
✓ Branch 1 (6→7) taken 14287 times.
57148 for (k = 0; k < DISTANCE_MAX; k++) {
28
2/2
✓ Branch 0 (3→4) taken 24289 times.
✓ Branch 1 (3→5) taken 18572 times.
42861 if (cost[k] < min)
29 24289 min = cost[k];
30 }
31
32 14287 return min;
33 }
34
35 /* A simple implementation of "The string-to-string correction problem (1974)"
36 * by Robert A. Wagner.
37 */
38 620 static unsigned int string_distance(const char *a, const char *b)
39 {
40 620 unsigned int len_a = strlen(a);
41 620 unsigned int len_b = strlen(b);
42 unsigned int *distance;
43 unsigned int i, j, ret;
44
45 620 distance = xzalloc((len_a + 1) * (len_b + 1) * sizeof(unsigned int));
46
47 #define DISTANCE(__i, __j) distance[(__i) * len_b + (__j)]
48
49
2/2
✓ Branch 0 (5→4) taken 2853 times.
✓ Branch 1 (5→6) taken 620 times.
3473 for (i = 0; i <= len_a; i++)
50 2853 DISTANCE(i, 0) = i;
51
2/2
✓ Branch 0 (8→7) taken 3907 times.
✓ Branch 1 (8→9) taken 620 times.
4527 for (j = 0; j <= len_b; j++)
52 3907 DISTANCE(0, j) = j;
53
54
2/2
✓ Branch 0 (20→10) taken 2233 times.
✓ Branch 1 (20→21) taken 620 times.
2853 for (i = 1; i <= len_a; i++) {
55
2/2
✓ Branch 0 (18→11) taken 14287 times.
✓ Branch 1 (18→19) taken 2233 times.
16520 for (j = 1; j <= len_b; j++) {
56 14287 unsigned int subcost = (a[i] == b[j]) ? 0 : 1;
57 unsigned int cost[3];
58
59 14287 cost[DELETION] = DISTANCE(i - 1, j) + 1;
60 14287 cost[INSERTION] = DISTANCE(i, j - 1) + 1;
61 14287 cost[TRANSFORMATION] = DISTANCE(i - 1, j - 1) + subcost;
62 14287 DISTANCE(i, j) = min_distance(cost);
63
64
4/4
✓ Branch 0 (12→13) taken 11000 times.
✓ Branch 1 (12→17) taken 3287 times.
✓ Branch 2 (13→14) taken 9387 times.
✓ Branch 3 (13→17) taken 1613 times.
14287 if (i > 1 && j > 1 &&
65
2/2
✓ Branch 0 (14→15) taken 795 times.
✓ Branch 1 (14→17) taken 8592 times.
9387 a[i] == b[j - 1] &&
66
2/2
✓ Branch 0 (15→16) taken 8 times.
✓ Branch 1 (15→17) taken 787 times.
795 a[i - 1] == b[j])
67 8 DISTANCE(i, j) =
68 8 min(DISTANCE(i, j),
69 DISTANCE(i - 2, j - 2) + subcost);
70 }
71 }
72
73 620 ret = DISTANCE(len_a, len_b);
74
75 620 free(distance);
76
77 620 return ret;
78 }
79
80 1334 void string_misspell_init(struct string_misspell_state *st)
81 {
82 1334 st->obj = NULL;
83 1334 st->min_distance = UINT_MAX;
84 1334 }
85
86 648 int string_misspell_update(const char *a, const char *b,
87 void *obj, struct string_misspell_state *st)
88 {
89 unsigned int len_a, len_b, max_len, min_len, distance, threshold;
90
91 648 len_a = strlen(a);
92 648 len_b = strlen(b);
93
94 648 max_len = max(len_a, len_b);
95 648 min_len = min(len_a, len_b);
96
97
2/2
✓ Branch 0 (2→3) taken 28 times.
✓ Branch 1 (2→4) taken 620 times.
648 if (max_len <= 1)
98 28 return 0;
99
100
2/2
✓ Branch 0 (4→5) taken 188 times.
✓ Branch 1 (4→8) taken 432 times.
620 if (max_len - min_len <= 1)
101
2/2
✓ Branch 0 (5→6) taken 144 times.
✓ Branch 1 (5→7) taken 44 times.
188 threshold = max(div_round_up(max_len, 3), 1);
102 else
103 432 threshold = div_round_up(max_len + 2, 3);
104
105 620 distance = string_distance(a, b);
106
2/2
✓ Branch 0 (10→11) taken 456 times.
✓ Branch 1 (10→12) taken 164 times.
620 if (distance > threshold)
107 456 return 0;
108
2/2
✓ Branch 0 (12→13) taken 47 times.
✓ Branch 1 (12→14) taken 117 times.
164 else if (distance < st->min_distance) {
109 47 st->min_distance = distance;
110 47 st->obj = obj;
111 47 return 1;
112 }
113 117 return 0;
114 }
115