Skip to content

Commit 8053521

Browse files
committed
various C linting
- add a check for a truncated path (PATH_MAX is indicative only) - remove an unused variable - add some static and const - simplify header path - add IWYU to scons
1 parent d328a41 commit 8053521

9 files changed

+29
-25
lines changed

SConstruct

+1-1
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ else:
657657
# check _mm_crc32_u64 (SSE4.2) support:
658658
conf.check_mm_crc32_u64()
659659

660-
if 'clang' in os.path.basename(conf.env['CC']):
660+
if any(cc in os.path.basename(conf.env['CC']) for cc in ('clang', 'include-what-you-use')):
661661
conf.env.Append(CCFLAGS=['-fcolor-diagnostics']) # Colored warnings
662662
conf.env.Append(CCFLAGS=['-Qunused-arguments']) # Hide wrong messages
663663
conf.env.Append(CCFLAGS=['-Wno-bad-function-cast'])

lib/checksum.c

-2
Original file line numberDiff line numberDiff line change
@@ -1009,15 +1009,13 @@ gboolean rm_digest_equal(RmDigest *a, RmDigest *b) {
10091009
/* all the "easy" ways failed... do manual check of all buffers */
10101010
GSList *a_iter = pa->buffers;
10111011
GSList *b_iter = pb->buffers;
1012-
guint bytes = 0;
10131012
while(a_iter && b_iter) {
10141013
if(!rm_buffer_equal(a_iter->data, b_iter->data)) {
10151014
rm_log_error_line(
10161015
"Paranoid digest compare found mismatch - must be hash collision in "
10171016
"shadow hash");
10181017
return false;
10191018
}
1020-
bytes += ((RmBuffer *)a_iter->data)->len;
10211019
a_iter = a_iter->next;
10221020
b_iter = b_iter->next;
10231021
}

lib/checksums/highwayhash.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static void Permute(const uint64_t v[4], uint64_t* permuted) {
117117
permuted[3] = (v[1] >> 32) | (v[1] << 32);
118118
}
119119

120-
void PermuteAndUpdate(HighwayHashState* state) {
120+
static void PermuteAndUpdate(HighwayHashState* state) {
121121
uint64_t permuted[4];
122122
Permute(state->v0, permuted);
123123
Update(permuted, state);

lib/checksums/murmur3.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static inline uint64_t fmix64(uint64_t k) {
134134

135135
//-----------------------------------------------------------------------------
136136

137-
MurmurHash3_x86_32_state *MurmurHash3_x86_32_new() {
137+
MurmurHash3_x86_32_state *MurmurHash3_x86_32_new(void) {
138138
return g_slice_new0(MurmurHash3_x86_32_state);
139139
}
140140

lib/hash-utility.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
#include <stdlib.h>
2929
#include <string.h>
3030

31-
#include "../lib/config.h"
32-
#include "../lib/hasher.h"
33-
#include "../lib/utilities.h"
31+
#include "config.h"
32+
#include "hasher.h"
33+
#include "utilities.h"
34+
#include "hash-utility.h"
3435

3536
typedef struct RmHasherSession {
3637
/* Internal */

lib/pathtricia.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,21 @@ typedef struct RmPathIter {
9797
char path_buf[PATH_MAX];
9898
} RmPathIter;
9999

100-
void rm_path_iter_init(RmPathIter *iter, const char *path) {
100+
static void rm_path_iter_init(RmPathIter *iter, const char *path) {
101101
if(*path == '/') {
102102
path++;
103103
}
104104

105-
memset(iter->path_buf, 0, PATH_MAX);
106105
strncpy(iter->path_buf, path, PATH_MAX);
106+
if (iter->path_buf[PATH_MAX - 1]) {
107+
iter->path_buf[PATH_MAX - 1] = 0;
108+
rm_log_error("path too long, truncated to: %s", iter->path_buf);
109+
}
107110

108111
iter->curr_elem = iter->path_buf;
109112
}
110113

111-
char *rm_path_iter_next(RmPathIter *iter) {
114+
static char *rm_path_iter_next(RmPathIter *iter) {
112115
char *elem_begin = iter->curr_elem;
113116

114117
if(elem_begin && (iter->curr_elem = strchr(elem_begin, '/'))) {

lib/pathtricia.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <glib.h>
3030
#include <stdbool.h>
31+
#include <stddef.h>
3132

3233
typedef struct _RmNode {
3334
/* Element of the path */

lib/replay.c

+14-14
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static RmParrot *rm_parrot_open(RmSession *session, const char *json_path, bool
152152
rm_trie_init(&polly->directory_trie);
153153

154154
for(GSList *iter = session->cfg->paths; iter; iter = iter->next) {
155-
RmPath *rmpath = iter->data;
155+
const RmPath *rmpath = iter->data;
156156
RmStat stat_buf;
157157
if(rm_sys_stat(rmpath->path, &stat_buf) != -1) {
158158
g_hash_table_add(polly->disk_ids, GUINT_TO_POINTER(stat_buf.st_dev));
@@ -310,7 +310,7 @@ static RmFile *rm_parrot_try_next(RmParrot *polly) {
310310
}
311311

312312
/* Fake the checksum using RM_DIGEST_EXT */
313-
JsonNode *cksum_node = json_object_get_member(object, "checksum");
313+
const JsonNode *cksum_node = json_object_get_member(object, "checksum");
314314
if(cksum_node != NULL) {
315315
const char *cksum = json_object_get_string_member(object, "checksum");
316316
if(cksum != NULL) {
@@ -319,7 +319,7 @@ static RmFile *rm_parrot_try_next(RmParrot *polly) {
319319
}
320320

321321
/* Fix the hardlink relationship */
322-
JsonNode *hardlink_of = json_object_get_member(object, "hardlink_of");
322+
const JsonNode *hardlink_of = json_object_get_member(object, "hardlink_of");
323323
if(hardlink_of != NULL) {
324324
rm_file_hardlink_add(polly->last_original, file);
325325
} else {
@@ -426,11 +426,11 @@ static RmFile *rm_parrot_next(RmParrot *polly) {
426426

427427
#define FAIL_MSG(msg) rm_log_debug(RED "[" msg "]\n" RESET)
428428

429-
static bool rm_parrot_check_depth(RmCfg *cfg, RmFile *file) {
429+
static bool rm_parrot_check_depth(const RmCfg *cfg, const RmFile *file) {
430430
return (file->depth == 0 || file->depth <= cfg->depth);
431431
}
432432

433-
static bool rm_parrot_check_size(RmCfg *cfg, RmFile *file) {
433+
static bool rm_parrot_check_size(const RmCfg *cfg, const RmFile *file) {
434434
if(cfg->limits_specified == false) {
435435
return true;
436436
}
@@ -451,7 +451,7 @@ static bool rm_parrot_check_size(RmCfg *cfg, RmFile *file) {
451451
return false;
452452
}
453453

454-
static bool rm_parrot_check_hidden(RmCfg *cfg, _UNUSED RmFile *file,
454+
static bool rm_parrot_check_hidden(const RmCfg *cfg, _UNUSED RmFile *file,
455455
const char *file_path) {
456456
if(cfg->ignore_hidden == false && cfg->partial_hidden == false) {
457457
// no need to check.
@@ -507,7 +507,7 @@ static bool rm_parrot_check_path(RmParrot *polly, RmFile *file, const char *file
507507
*/
508508

509509
for(GSList *iter = cfg->paths; iter; iter = iter->next) {
510-
RmPath *rmpath = iter->data;
510+
const RmPath *rmpath = iter->data;
511511
size_t path_len = strlen(rmpath->path);
512512

513513
if(strncmp(file_path, rmpath->path, path_len) == 0) {
@@ -527,7 +527,7 @@ static bool rm_parrot_check_path(RmParrot *polly, RmFile *file, const char *file
527527
return (highest_match > 0);
528528
}
529529

530-
static bool rm_parrot_check_types(RmCfg *cfg, RmFile *file) {
530+
static bool rm_parrot_check_types(RmCfg *cfg, const RmFile *file) {
531531
switch(file->lint_type) {
532532
case RM_LINT_TYPE_DUPE_CANDIDATE:
533533
return cfg->find_duplicates;
@@ -623,15 +623,15 @@ static void rm_parrot_fix_duplicate_entries(RmParrotCage *cage, GQueue *group) {
623623
}
624624

625625
static void rm_parrot_fix_must_match_tagged(RmParrotCage *cage, GQueue *group) {
626-
RmCfg *cfg = cage->session->cfg;
626+
const RmCfg *cfg = cage->session->cfg;
627627
if(!(cfg->must_match_tagged || cfg->must_match_untagged)) {
628628
return;
629629
}
630630

631631
bool has_prefd = false, has_non_prefd = false;
632632

633633
for(GList *iter = group->head; iter; iter = iter->next) {
634-
RmFile *file = iter->data;
634+
const RmFile *file = iter->data;
635635
if(file->lint_type != RM_LINT_TYPE_DUPE_CANDIDATE &&
636636
file->lint_type != RM_LINT_TYPE_DUPE_DIR_CANDIDATE) {
637637
// -k and -m only applies to dupes.
@@ -684,12 +684,12 @@ static void rm_parrot_update_stats(RmParrotCage *cage, RmFile *file) {
684684
}
685685

686686
static void rm_parrot_cage_write_group(RmParrotCage *cage, GQueue *group, bool pack_directories) {
687-
RmCfg *cfg = cage->session->cfg;
687+
const RmCfg *cfg = cage->session->cfg;
688688

689689
if(cfg->filter_mtime) {
690690
gsize older = 0;
691691
for(GList *iter = group->head; iter; iter = iter->next) {
692-
RmFile *file = iter->data;
692+
const RmFile *file = iter->data;
693693
older += (file->mtime >= cfg->min_mtime);
694694
}
695695

@@ -877,7 +877,7 @@ static void rm_parrot_merge_identical_groups(RmParrotCage *cage) {
877877
GQueue *existing_group = g_hash_table_lookup(digest_to_group, head_file->digest);
878878

879879
if(existing_group != NULL) {
880-
RmFile *existing_head_file = existing_group->head->data;
880+
const RmFile *existing_head_file = existing_group->head->data;
881881

882882
/* Merge only groups with the same type */
883883
if(existing_head_file->lint_type == head_file->lint_type) {
@@ -914,7 +914,7 @@ void rm_parrot_cage_flush(RmParrotCage *cage) {
914914
* If so, we need to merge them up again using treemerge.c
915915
*/
916916
for(GList *iter = cage->parrots->head; iter; iter = iter->next) {
917-
RmParrot *parrot = iter->data;
917+
const RmParrot *parrot = iter->data;
918918
if(parrot->pack_directories) {
919919
pack_directories = true;
920920
break;

lib/xattr.c

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "config.h"
2828

2929
#include <errno.h>
30+
#include <stdlib.h>
3031
#include <string.h>
3132
#include <sys/types.h>
3233

0 commit comments

Comments
 (0)