Skip to content

Commit 2b34ebc

Browse files
authored
Merge pull request #2396 from SarletteD/fix/cppcheck-nullable-strcmp
style(mcp): compare nullable strings through one helper
2 parents 5621a1c + 629e9c2 commit 2b34ebc

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 {
@@ -12642,12 +12647,11 @@ static int search_result_cmp(const void *a, const void *b) {
1264212647
if (score_order != 0) {
1264312648
return score_order;
1264412649
}
12645-
int qn_order = strcmp(ra->qualified_name ? ra->qualified_name : "",
12646-
rb->qualified_name ? rb->qualified_name : "");
12650+
int qn_order = nullable_strcmp(ra->qualified_name, rb->qualified_name);
1264712651
if (qn_order != 0) {
1264812652
return qn_order;
1264912653
}
12650-
int file_order = strcmp(ra->file ? ra->file : "", rb->file ? rb->file : "");
12654+
int file_order = nullable_strcmp(ra->file, rb->file);
1265112655
if (file_order != 0) {
1265212656
return file_order;
1265312657
}

0 commit comments

Comments
 (0)