From 629e9c2c1f87e2ac8b5d644d38ff4e935928752a Mon Sep 17 00:00:00 2001 From: Sarlette <282189207+SarletteD@users.noreply.github.com> Date: Sun, 27 Sep 2026 01:40:23 +0200 Subject: [PATCH] style(mcp): compare nullable strings through one helper cppcheck 2.20 (the version the lint job pins) reports knownConditionTrueFalse on two comparators in src/mcp/mcp.c whose tie-breakers compare one strcmp result after another, each call with inline "x ? x : \"\"" fallbacks. Neither function differs between these trees; which of them is flagged depends on the rest of the lint source list. With scripts/lint.sh --ci and cppcheck 2.20.0 built from source: - main itself fails on search_result_cmp ("file_order != 0 is always false"); - adding one more language grammar and its sources moves the finding to project_record_compare ("by_root != 0 is always false"); - carrying each comparator's order in one variable instead still fails, now as redundantAssignment on project_record_compare. Both comparators now call nullable_strcmp(), which orders NULL like "" exactly as the inline fallbacks did. lint-cppcheck passes again on main and with the additional sources. Comparison order and results are unchanged for every input. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Sarlette <282189207+SarletteD@users.noreply.github.com> --- src/mcp/mcp.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c index d6c7bd4686..20d570ba69 100644 --- a/src/mcp/mcp.c +++ b/src/mcp/mcp.c @@ -2723,18 +2723,23 @@ static void project_record_clear(mcp_project_record_t *record) { memset(record, 0, sizeof(*record)); } +/* strcmp that orders NULL like "". */ +static int nullable_strcmp(const char *a, const char *b) { + return strcmp(a ? a : "", b ? b : ""); +} + static int project_record_compare(const void *left, const void *right) { const mcp_project_record_t *a = left; const mcp_project_record_t *b = right; - int by_name = strcmp(a->name ? a->name : "", b->name ? b->name : ""); + int by_name = nullable_strcmp(a->name, b->name); if (by_name != 0) { return by_name; } - int by_root = strcmp(a->root_path ? a->root_path : "", b->root_path ? b->root_path : ""); + int by_root = nullable_strcmp(a->root_path, b->root_path); if (by_root != 0) { return by_root; } - return strcmp(a->db_file ? a->db_file : "", b->db_file ? b->db_file : ""); + return nullable_strcmp(a->db_file, b->db_file); } typedef enum { @@ -12605,12 +12610,11 @@ static int search_result_cmp(const void *a, const void *b) { if (score_order != 0) { return score_order; } - int qn_order = strcmp(ra->qualified_name ? ra->qualified_name : "", - rb->qualified_name ? rb->qualified_name : ""); + int qn_order = nullable_strcmp(ra->qualified_name, rb->qualified_name); if (qn_order != 0) { return qn_order; } - int file_order = strcmp(ra->file ? ra->file : "", rb->file ? rb->file : ""); + int file_order = nullable_strcmp(ra->file, rb->file); if (file_order != 0) { return file_order; }