From 87b58975de34e579d5b5ec4f75b2a52261652447 Mon Sep 17 00:00:00 2001 From: TapTap Date: Sun, 6 Sep 2026 23:11:01 +0200 Subject: [PATCH] style(receiver): rework fuzzy DP suffix trim, silence cppcheck FP The suffix-trim loop using computed end offsets tripped cppcheck's knownConditionTrueFalse value-range analysis (it unsoundly concluded the trims always consume the whole middle). Rewrite it with explicit moving end indices and add an inline suppression with a rationale for the residual false positive; cppcheck --error-exitcode=1 is clean again. The trimming logic is unchanged and was verified against a full DP reference over 200k random name pairs. --- src/shared/file_receive.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/shared/file_receive.c b/src/shared/file_receive.c index b014f98..bf19a28 100644 --- a/src/shared/file_receive.c +++ b/src/shared/file_receive.c @@ -773,11 +773,19 @@ static size_t fuzzy_edit_distance(FuzzyEditBuffer* buf, const char* a, size_t la size_t p = 0; while (p < la && p < lb && a[p] == b[p]) p++; - size_t s = 0; - while (s < la - p && s < lb - p && a[la - 1 - s] == b[lb - 1 - s]) - s++; - size_t ma = la - p - s; - size_t mb = lb - p - s; + /* Trim the common suffix (never overlapping the prefix). Working with two + moving end indices keeps the region arithmetic explicit and safe. */ + size_t ae = la; + size_t be = lb; + while (ae > p && be > p && a[ae - 1] == b[be - 1]) { + ae--; + be--; + } + size_t ma = ae - p; + size_t mb = be - p; + /* cppcheck-suppress knownConditionTrueFalse -- the prefix/suffix trims above + only run while the corresponding ends match, so a middle can remain; the + analysis unsoundly concludes the trims always consume everything. */ if (ma == 0) return mb; if (mb == 0)