Skip to content

Commit 629e9c2

Browse files
SarletteDclaude
andcommitted
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) <noreply@anthropic.com> Signed-off-by: Sarlette <282189207+SarletteD@users.noreply.github.com>
1 parent 64c23fa commit 629e9c2

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,18 +2723,23 @@ static void project_record_clear(mcp_project_record_t *record) {
27232723
memset(record, 0, sizeof(*record));
27242724
}
27252725

2726+
/* strcmp that orders NULL like "". */
2727+
static int nullable_strcmp(const char *a, const char *b) {
2728+
return strcmp(a ? a : "", b ? b : "");
2729+
}
2730+
27262731
static int project_record_compare(const void *left, const void *right) {
27272732
const mcp_project_record_t *a = left;
27282733
const mcp_project_record_t *b = right;
2729-
int by_name = strcmp(a->name ? a->name : "", b->name ? b->name : "");
2734+
int by_name = nullable_strcmp(a->name, b->name);
27302735
if (by_name != 0) {
27312736
return by_name;
27322737
}
2733-
int by_root = strcmp(a->root_path ? a->root_path : "", b->root_path ? b->root_path : "");
2738+
int by_root = nullable_strcmp(a->root_path, b->root_path);
27342739
if (by_root != 0) {
27352740
return by_root;
27362741
}
2737-
return strcmp(a->db_file ? a->db_file : "", b->db_file ? b->db_file : "");
2742+
return nullable_strcmp(a->db_file, b->db_file);
27382743
}
27392744

27402745
typedef enum {
@@ -12605,12 +12610,11 @@ static int search_result_cmp(const void *a, const void *b) {
1260512610
if (score_order != 0) {
1260612611
return score_order;
1260712612
}
12608-
int qn_order = strcmp(ra->qualified_name ? ra->qualified_name : "",
12609-
rb->qualified_name ? rb->qualified_name : "");
12613+
int qn_order = nullable_strcmp(ra->qualified_name, rb->qualified_name);
1261012614
if (qn_order != 0) {
1261112615
return qn_order;
1261212616
}
12613-
int file_order = strcmp(ra->file ? ra->file : "", rb->file ? rb->file : "");
12617+
int file_order = nullable_strcmp(ra->file, rb->file);
1261412618
if (file_order != 0) {
1261512619
return file_order;
1261612620
}

0 commit comments

Comments
 (0)