Skip to content

Commit 59b336d

Browse files
committed
cppcheck fixes
1 parent 07f6f32 commit 59b336d

File tree

5 files changed

+65
-41
lines changed

5 files changed

+65
-41
lines changed

src/bloom.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <unistd.h> /* close */
2121
#include "bloom.h"
2222

23+
typedef char* caddr_t;
2324

2425
#define CHECK_BIT_CHAR(c, k) ((c) & (1 << (k)))
2526
#define CHECK_BIT(A, k) (CHECK_BIT_CHAR(A[((k) / 8)], ((k) % 8)))
@@ -46,10 +47,10 @@ static uint64_t* __default_hash(int num_hashes, const char *str);
4647
static uint64_t __fnv_1a(const char *key, int seed);
4748
static void __calculate_optimal_hashes(BloomFilter *bf);
4849
static void __read_from_file(BloomFilter *bf, FILE *fp, short on_disk, const char *filename);
49-
static void __write_to_file(BloomFilter *bf, FILE *fp, short on_disk);
50+
static void __write_to_file(const BloomFilter *bf, FILE *fp, short on_disk);
5051
static void __update_elements_added_on_disk(BloomFilter *bf);
5152
static int __sum_bits_set_char(unsigned char c);
52-
static int __check_if_union_or_intersection_ok(BloomFilter *res, BloomFilter *bf1, BloomFilter *bf2);
53+
static int __check_if_union_or_intersection_ok(const BloomFilter *res, const BloomFilter *bf1, const BloomFilter *bf2);
5354

5455

5556
int bloom_filter_init_alt(BloomFilter *bf, uint64_t estimated_elements, float false_positive_rate, BloomHashFunction hash_function) {
@@ -119,7 +120,7 @@ int bloom_filter_clear(BloomFilter *bf) {
119120
return BLOOM_SUCCESS;
120121
}
121122

122-
void bloom_filter_stats(BloomFilter *bf) {
123+
void bloom_filter_stats(const BloomFilter *bf) {
123124
const char *is_on_disk = (bf->__is_on_disk == 0 ? "no" : "yes");
124125
uint64_t size_on_disk = bloom_filter_export_size(bf);
125126

@@ -150,19 +151,19 @@ int bloom_filter_add_string(BloomFilter *bf, const char *str) {
150151
}
151152

152153

153-
int bloom_filter_check_string(BloomFilter *bf, const char *str) {
154+
int bloom_filter_check_string(const BloomFilter *bf, const char *str) {
154155
uint64_t *hashes = bloom_filter_calculate_hashes(bf, str, bf->number_hashes);
155156
int res = bloom_filter_check_string_alt(bf, hashes, bf->number_hashes);
156157
free(hashes);
157158
return res;
158159
}
159160

160-
uint64_t* bloom_filter_calculate_hashes(BloomFilter *bf, const char *str, unsigned int number_hashes) {
161+
uint64_t* bloom_filter_calculate_hashes(const BloomFilter *bf, const char *str, unsigned int number_hashes) {
161162
return bf->hash_function(number_hashes, str);
162163
}
163164

164165
/* Add a string to a bloom filter using the defined hashes */
165-
int bloom_filter_add_string_alt(BloomFilter *bf, uint64_t *hashes, unsigned int number_hashes_passed) {
166+
int bloom_filter_add_string_alt(BloomFilter *bf, const uint64_t *hashes, unsigned int number_hashes_passed) {
166167
if (number_hashes_passed < bf->number_hashes) {
167168
fprintf(stderr, "Error: not enough hashes passed in to correctly check!\n");
168169
return BLOOM_FAILURE;
@@ -183,7 +184,7 @@ int bloom_filter_add_string_alt(BloomFilter *bf, uint64_t *hashes, unsigned int
183184
}
184185

185186
/* Check if a string is in the bloom filter using the passed hashes */
186-
int bloom_filter_check_string_alt(BloomFilter *bf, uint64_t *hashes, unsigned int number_hashes_passed) {
187+
int bloom_filter_check_string_alt(const BloomFilter *bf, const uint64_t *hashes, unsigned int number_hashes_passed) {
187188
if (number_hashes_passed < bf->number_hashes) {
188189
fprintf(stderr, "Error: not enough hashes passed in to correctly check!\n");
189190
return BLOOM_FAILURE;
@@ -201,14 +202,14 @@ int bloom_filter_check_string_alt(BloomFilter *bf, uint64_t *hashes, unsigned in
201202
return r;
202203
}
203204

204-
float bloom_filter_current_false_positive_rate(BloomFilter *bf) {
205+
float bloom_filter_current_false_positive_rate(const BloomFilter *bf) {
205206
int num = bf->number_hashes * bf->elements_added;
206207
double d = -num / (float) bf->number_bits;
207208
double e = exp(d);
208209
return pow((1 - e), bf->number_hashes);
209210
}
210211

211-
int bloom_filter_export(BloomFilter *bf, const char *filepath) {
212+
int bloom_filter_export(const BloomFilter *bf, const char *filepath) {
212213
// if the bloom is initialized on disk, no need to export it
213214
if (bf->__is_on_disk == 1) {
214215
return BLOOM_SUCCESS;
@@ -251,7 +252,7 @@ int bloom_filter_import_on_disk_alt(BloomFilter *bf, const char *filepath, Bloom
251252
return BLOOM_SUCCESS;
252253
}
253254

254-
char* bloom_filter_export_hex_string(BloomFilter *bf) {
255+
char* bloom_filter_export_hex_string(const BloomFilter *bf) {
255256
uint64_t i, bytes = sizeof(uint64_t) * 2 + sizeof(float) + (bf->bloom_length);
256257
char* hex = (char*)calloc((bytes * 2 + 1), sizeof(char));
257258
for (i = 0; i < bf->bloom_length; ++i) {
@@ -302,19 +303,19 @@ int bloom_filter_import_hex_string_alt(BloomFilter *bf, const char *hex, BloomHa
302303
return BLOOM_SUCCESS;
303304
}
304305

305-
uint64_t bloom_filter_export_size(BloomFilter *bf) {
306+
uint64_t bloom_filter_export_size(const BloomFilter *bf) {
306307
return (uint64_t)(bf->bloom_length * sizeof(unsigned char)) + (2 * sizeof(uint64_t)) + sizeof(float);
307308
}
308309

309-
uint64_t bloom_filter_count_set_bits(BloomFilter *bf) {
310+
uint64_t bloom_filter_count_set_bits(const BloomFilter *bf) {
310311
uint64_t i, res = 0;
311312
for (i = 0; i < bf->bloom_length; ++i) {
312313
res += __sum_bits_set_char(bf->bloom[i]);
313314
}
314315
return res;
315316
}
316317

317-
uint64_t bloom_filter_estimate_elements(BloomFilter *bf) {
318+
uint64_t bloom_filter_estimate_elements(const BloomFilter *bf) {
318319
return bloom_filter_estimate_elements_by_values(bf->number_bits, bloom_filter_count_set_bits(bf), bf->number_hashes);
319320
}
320321

@@ -337,7 +338,7 @@ int bloom_filter_union(BloomFilter *res, BloomFilter *bf1, BloomFilter *bf2) {
337338
return BLOOM_SUCCESS;
338339
}
339340

340-
uint64_t bloom_filter_count_union_bits_set(BloomFilter *bf1, BloomFilter *bf2) {
341+
uint64_t bloom_filter_count_union_bits_set(const BloomFilter *bf1, const BloomFilter *bf2) {
341342
// Ensure the bloom filters can be unioned
342343
if (__check_if_union_or_intersection_ok(bf1, bf1, bf2) == BLOOM_FAILURE) { // use bf1 as res
343344
return BLOOM_FAILURE;
@@ -367,7 +368,7 @@ void bloom_filter_set_elements_to_estimated(BloomFilter *bf) {
367368
__update_elements_added_on_disk(bf);
368369
}
369370

370-
uint64_t bloom_filter_count_intersection_bits_set(BloomFilter *bf1, BloomFilter *bf2) {
371+
uint64_t bloom_filter_count_intersection_bits_set(const BloomFilter *bf1, const BloomFilter *bf2) {
371372
// Ensure the bloom filters can be used in an intersection
372373
if (__check_if_union_or_intersection_ok(bf1, bf1, bf2) == BLOOM_FAILURE) { // use bf1 as res
373374
return BLOOM_FAILURE;
@@ -379,7 +380,7 @@ uint64_t bloom_filter_count_intersection_bits_set(BloomFilter *bf1, BloomFilter
379380
return res;
380381
}
381382

382-
float bloom_filter_jaccard_index(BloomFilter *bf1, BloomFilter *bf2) {
383+
float bloom_filter_jaccard_index(const BloomFilter *bf1, const BloomFilter *bf2) {
383384
// Ensure the bloom filters can be used in an intersection and union
384385
if (__check_if_union_or_intersection_ok(bf1, bf1, bf2) == BLOOM_FAILURE) { // use bf1 as res
385386
return (float)BLOOM_FAILURE;
@@ -411,7 +412,7 @@ static int __sum_bits_set_char(unsigned char c) {
411412
return bits_set_table[c];
412413
}
413414

414-
static int __check_if_union_or_intersection_ok(BloomFilter *res, BloomFilter *bf1, BloomFilter *bf2) {
415+
static int __check_if_union_or_intersection_ok(const BloomFilter *res, const BloomFilter *bf1, const BloomFilter *bf2) {
415416
if (res->number_hashes != bf1->number_hashes || bf1->number_hashes != bf2->number_hashes) {
416417
return BLOOM_FAILURE;
417418
} else if (res->number_bits != bf1->number_bits || bf1->number_bits != bf2->number_bits) {
@@ -423,7 +424,7 @@ static int __check_if_union_or_intersection_ok(BloomFilter *res, BloomFilter *bf
423424
}
424425

425426
/* NOTE: this assumes that the file handler is open and ready to use */
426-
static void __write_to_file(BloomFilter *bf, FILE *fp, short on_disk) {
427+
static void __write_to_file(const BloomFilter *bf, FILE *fp, short on_disk) {
427428
if (on_disk == 0) {
428429
fwrite(bf->bloom, bf->bloom_length, 1, fp);
429430
} else {

src/bloom.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,23 @@ static __inline__ int bloom_filter_import_on_disk(BloomFilter *bf, const char *f
8989
}
9090

9191
/* Export the current bloom filter to file */
92-
int bloom_filter_export(BloomFilter *bf, const char *filepath);
92+
int bloom_filter_export(const BloomFilter *bf, const char *filepath);
9393

9494
/* Export and import as a hex string; not space effecient but allows for storing
9595
multiple blooms in a single file or in a database, etc.
9696
9797
NOTE: It is up to the caller to free the allocated memory */
98-
char* bloom_filter_export_hex_string(BloomFilter *bf);
98+
char* bloom_filter_export_hex_string(const BloomFilter *bf);
9999
int bloom_filter_import_hex_string_alt(BloomFilter *bf, const char *hex, BloomHashFunction hash_function);
100-
static __inline__ int bloom_filter_import_hex_string(BloomFilter *bf, char *hex) {
100+
static __inline__ int bloom_filter_import_hex_string(BloomFilter *bf, const char *hex) {
101101
return bloom_filter_import_hex_string_alt(bf, hex, NULL);
102102
}
103103

104104
/* Set or change the hashing function */
105105
void bloom_filter_set_hash_function(BloomFilter *bf, BloomHashFunction hash_function);
106106

107107
/* Print out statistics about the bloom filter */
108-
void bloom_filter_stats(BloomFilter *bf);
108+
void bloom_filter_stats(const BloomFilter *bf);
109109

110110
/* Release all memory used by the bloom filter */
111111
int bloom_filter_destroy(BloomFilter *bf);
@@ -117,37 +117,37 @@ int bloom_filter_clear(BloomFilter *bf);
117117
int bloom_filter_add_string(BloomFilter *bf, const char *str);
118118

119119
/* Add a string to a bloom filter using the defined hashes */
120-
int bloom_filter_add_string_alt(BloomFilter *bf, uint64_t *hashes, unsigned int number_hashes_passed);
120+
int bloom_filter_add_string_alt(BloomFilter *bf, const uint64_t *hashes, unsigned int number_hashes_passed);
121121

122122
/* Check to see if a string (or element) is or is not in the bloom filter */
123-
int bloom_filter_check_string(BloomFilter *bf, const char *str);
123+
int bloom_filter_check_string(const BloomFilter *bf, const char *str);
124124

125125
/* Check if a string is in the bloom filter using the passed hashes */
126-
int bloom_filter_check_string_alt(BloomFilter *bf, uint64_t *hashes, unsigned int number_hashes_passed);
126+
int bloom_filter_check_string_alt(const BloomFilter *bf, const uint64_t *hashes, unsigned int number_hashes_passed);
127127

128128
/* Calculates the current false positive rate based on the number of inserted elements */
129-
float bloom_filter_current_false_positive_rate(BloomFilter *bf);
129+
float bloom_filter_current_false_positive_rate(const BloomFilter *bf);
130130

131131
/* Count the number of bits set to 1 */
132-
uint64_t bloom_filter_count_set_bits(BloomFilter *bf);
132+
uint64_t bloom_filter_count_set_bits(const BloomFilter *bf);
133133

134134
/* Estimate the number of unique elements in a Bloom Filter instead of using the overall count
135135
https://en.wikipedia.org/wiki/Bloom_filter#Approximating_the_number_of_items_in_a_Bloom_filter
136136
m = bits in Bloom filter
137137
k = number hashes
138138
X = count of flipped bits in filter */
139-
uint64_t bloom_filter_estimate_elements(BloomFilter *bf);
139+
uint64_t bloom_filter_estimate_elements(const BloomFilter *bf);
140140
uint64_t bloom_filter_estimate_elements_by_values(uint64_t m, uint64_t X, int k);
141141

142142
/* Wrapper to set the inserted elements count to the estimated elements calculation */
143143
void bloom_filter_set_elements_to_estimated(BloomFilter *bf);
144144

145145
/* Generate the desired number of hashes for the provided string
146146
NOTE: It is up to the caller to free the allocated memory */
147-
uint64_t* bloom_filter_calculate_hashes(BloomFilter *bf, const char *str, unsigned int number_hashes);
147+
uint64_t* bloom_filter_calculate_hashes(const BloomFilter *bf, const char *str, unsigned int number_hashes);
148148

149149
/* Calculate the size the bloom filter will take on disk when exported in bytes */
150-
uint64_t bloom_filter_export_size(BloomFilter *bf);
150+
uint64_t bloom_filter_export_size(const BloomFilter *bf);
151151

152152
/*******************************************************************************
153153
Merging, Intersection, and Jaccard Index Functions
@@ -157,18 +157,18 @@ uint64_t bloom_filter_export_size(BloomFilter *bf);
157157

158158
/* Merge Bloom Filters - inserts information into res */
159159
int bloom_filter_union(BloomFilter *res, BloomFilter *bf1, BloomFilter *bf2);
160-
uint64_t bloom_filter_count_union_bits_set(BloomFilter *bf1, BloomFilter *bf2);
160+
uint64_t bloom_filter_count_union_bits_set(const BloomFilter *bf1, const BloomFilter *bf2);
161161

162162
/* Find the intersection of Bloom Filters - insert into res with the intersection
163163
The number of inserted elements is updated to the estimated elements calculation */
164164
int bloom_filter_intersect(BloomFilter *res, BloomFilter *bf1, BloomFilter *bf2);
165-
uint64_t bloom_filter_count_intersection_bits_set(BloomFilter *bf1, BloomFilter *bf2);
165+
uint64_t bloom_filter_count_intersection_bits_set(const BloomFilter *bf1, const BloomFilter *bf2);
166166

167167
/* Calculate the Jacccard Index of the Bloom Filters
168168
NOTE: The closer to 1 the index, the closer in bloom filters. If it is 1, then
169169
the Bloom Filters contain the same elements, 0.5 would mean about 1/2 the same
170170
elements are in common. 0 would mean the Bloom Filters are completely different. */
171-
float bloom_filter_jaccard_index(BloomFilter *bf1, BloomFilter *bf2);
171+
float bloom_filter_jaccard_index(const BloomFilter *bf1, const BloomFilter *bf2);
172172

173173

174174
#ifdef __cplusplus

tests/bloom_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#define ELEMENTS 50000
1515
#define FALSE_POSITIVE_RATE 0.05
16-
#define KEY_LEN 10
16+
#define KEY_LEN 32
1717

1818
#define KNRM "\x1B[0m"
1919
#define KRED "\x1B[31m"

tests/minunit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
#include <math.h>
6767

6868
/* Maximum length of last message */
69-
#define MINUNIT_MESSAGE_LEN 1024
69+
#define MINUNIT_MESSAGE_LEN 4096
7070
/* Accuracy with which floats are compared */
7171
#define MINUNIT_EPSILON 1E-12
7272

tests/testsuite.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <sys/stat.h>
55
#include <sys/types.h>
66

7+
#include <openssl/evp.h>
8+
79
#include <openssl/md5.h>
810

911
#include "minunit.h"
@@ -918,18 +920,39 @@ static int calculate_md5sum(const char* filename, char* digest) {
918920
}
919921

920922
int n;
921-
MD5_CTX c;
922923
char buf[512];
923924
ssize_t bytes;
924925
unsigned char out[MD5_DIGEST_LENGTH];
925-
926-
MD5_Init(&c);
926+
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
927+
if (mdctx == NULL) {
928+
perror("EVP_MD_CTX_new failed");
929+
fclose(file_ptr);
930+
return 1;
931+
}
932+
if (EVP_DigestInit_ex(mdctx, EVP_md5(), NULL) != 1) {
933+
perror("EVP_DigestInit_ex failed");
934+
EVP_MD_CTX_free(mdctx);
935+
fclose(file_ptr);
936+
return 1;
937+
}
927938
do {
928939
bytes = fread(buf, 1, 512, file_ptr);
929-
MD5_Update(&c, buf, bytes);
940+
if (bytes > 0) {
941+
if (EVP_DigestUpdate(mdctx, buf, bytes) != 1) {
942+
perror("EVP_DigestUpdate failed");
943+
EVP_MD_CTX_free(mdctx);
944+
fclose(file_ptr);
945+
return 1;
946+
}
947+
}
930948
} while(bytes > 0);
931-
932-
MD5_Final(out, &c);
949+
if (EVP_DigestFinal_ex(mdctx, out, NULL) != 1) {
950+
perror("EVP_DigestFinal_ex failed");
951+
EVP_MD_CTX_free(mdctx);
952+
fclose(file_ptr);
953+
return 1;
954+
}
955+
EVP_MD_CTX_free(mdctx);
933956

934957
for (n = 0; n < MD5_DIGEST_LENGTH; n++) {
935958
char hex[3] = {0};

0 commit comments

Comments
 (0)