aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIvan Nardi <12729895+IvanNardi@users.noreply.github.com>2023-09-10 18:44:50 +0200
committerGitHub <noreply@github.com>2023-09-10 18:44:50 +0200
commitef6085370f75ae7c6c53bfbd0e36c588483a5047 (patch)
tree06e494b8a197fa1ab944a92cec561f600f6e6f1f /src
parent2c5e22123e3de10bc1115ef5e4103ef591757736 (diff)
fuzz: add fuzzers to test bitmap64 and domain_classify data structures (#2082)
Diffstat (limited to 'src')
-rw-r--r--src/include/ndpi_api.h16
-rw-r--r--src/lib/ndpi_bitmap64.c17
-rw-r--r--src/lib/ndpi_domain_classify.c18
-rw-r--r--src/lib/ndpi_hash.c12
4 files changed, 41 insertions, 22 deletions
diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h
index f3580e59a..9189a3a95 100644
--- a/src/include/ndpi_api.h
+++ b/src/include/ndpi_api.h
@@ -1800,12 +1800,12 @@ extern "C" {
/* ******************************* */
- u_int32_t ndpi_quick_hash(unsigned char *str, u_int str_len);
- u_int64_t ndpi_quick_hash64(char *str, u_int str_len);
- u_int32_t ndpi_hash_string(char *str);
- u_int32_t ndpi_rev_hash_string(char *str);
- u_int32_t ndpi_hash_string_len(char *str, u_int len);
- u_int32_t ndpi_murmur_hash(char *str, u_int str_len);
+ u_int32_t ndpi_quick_hash(const unsigned char *str, u_int str_len);
+ u_int64_t ndpi_quick_hash64(const char *str, u_int str_len);
+ u_int32_t ndpi_hash_string(const char *str);
+ u_int32_t ndpi_rev_hash_string(const char *str);
+ u_int32_t ndpi_hash_string_len(const char *str, u_int len);
+ u_int32_t ndpi_murmur_hash(const char *str, u_int str_len);
/* ******************************* */
@@ -2100,13 +2100,13 @@ extern "C" {
void ndpi_domain_classify_free(ndpi_domain_classify *s);
u_int32_t ndpi_domain_classify_size(ndpi_domain_classify *s);
bool ndpi_domain_classify_add(ndpi_domain_classify *s,
- u_int8_t class_id, char *domain);
+ u_int8_t class_id, const char *domain);
u_int32_t ndpi_domain_classify_add_domains(ndpi_domain_classify *s,
u_int8_t class_id,
char *file_path);
bool ndpi_domain_classify_contains(ndpi_domain_classify *s,
u_int8_t *class_id /* out */,
- char *domain);
+ const char *domain);
/* ******************************* */
diff --git a/src/lib/ndpi_bitmap64.c b/src/lib/ndpi_bitmap64.c
index f254c1483..6403eea7a 100644
--- a/src/lib/ndpi_bitmap64.c
+++ b/src/lib/ndpi_bitmap64.c
@@ -79,6 +79,9 @@ bool ndpi_bitmap64_compress(ndpi_bitmap64 *_b) {
if(!b)
return(false);
+ if(b->is_compressed)
+ return(true);
+
if(b->num_used_entries > 0) {
if(b->num_used_entries > 1)
qsort(b->entries, b->num_used_entries,
@@ -110,10 +113,13 @@ bool ndpi_bitmap64_compress(ndpi_bitmap64 *_b) {
if(binary_fuse16_populate(b->entries, b->num_used_entries, &b->bitmap)) {
ndpi_free(b->entries), b->num_used_entries = b->num_allocated_entries = 0;
b->entries = NULL;
- } else
+ } else {
+ binary_fuse16_free(&b->bitmap);
return(false);
- } else
+ }
+ } else {
return(false);
+ }
b->is_compressed = true;
@@ -145,7 +151,10 @@ bool ndpi_bitmap64_set(ndpi_bitmap64 *_b, u_int64_t value) {
rc = (u_int64_t*)ndpi_realloc(b->entries,
sizeof(u_int64_t)*b->num_allocated_entries,
sizeof(u_int64_t)*new_len);
- if(rc == NULL) return(false);
+ if(rc == NULL) {
+ b->is_compressed = false;
+ return(false);
+ }
b->entries = rc, b->num_allocated_entries = new_len;
}
@@ -165,6 +174,8 @@ bool ndpi_bitmap64_isset(ndpi_bitmap64 *_b, u_int64_t value) {
return(false);
if(!b->is_compressed) ndpi_bitmap64_compress(b);
+ if(!b->is_compressed)
+ return(false);
return(binary_fuse16_contain(value, &b->bitmap));
}
diff --git a/src/lib/ndpi_domain_classify.c b/src/lib/ndpi_domain_classify.c
index 385796e08..917c897f6 100644
--- a/src/lib/ndpi_domain_classify.c
+++ b/src/lib/ndpi_domain_classify.c
@@ -68,6 +68,9 @@ void ndpi_domain_classify_free(ndpi_domain_classify *s) {
u_int32_t ndpi_domain_classify_size(ndpi_domain_classify *s) {
u_int32_t i, tot_len = sizeof(ndpi_domain_classify);
+ if(!s)
+ return(0);
+
for(i=0; i<MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS; i++) {
if(s->classes[i].domains != NULL) {
tot_len += ndpi_bitmap64_size(s->classes[i].domains);
@@ -82,9 +85,14 @@ u_int32_t ndpi_domain_classify_size(ndpi_domain_classify *s) {
bool ndpi_domain_classify_add(ndpi_domain_classify *s,
u_int8_t class_id,
- char *domain) {
+ const char *domain) {
u_int32_t i;
- char *dot = strrchr(domain, '.');
+ char *dot;
+
+ if(!s || !domain)
+ return(false);
+
+ dot = strrchr(domain, '.');
if(!dot) return(false);
if((!strcmp(dot, ".arpa")) || (!strcmp(dot, ".local")))
@@ -184,11 +192,11 @@ static bool is_valid_domain_char(u_char c) {
bool ndpi_domain_classify_contains(ndpi_domain_classify *s,
u_int8_t *class_id /* out */,
- char *domain) {
+ const char *domain) {
u_int32_t i, len;
- char *dot, *elem;
+ const char *dot, *elem;
- if(!domain) return(false);
+ if(!domain || !s) return(false);
if((len = strlen(domain)) == 0) return(false);
if((dot = strrchr(domain, '.')) == NULL) return(false);
if((!strcmp(dot, ".arpa")) || (!strcmp(dot, ".local"))) return(false);
diff --git a/src/lib/ndpi_hash.c b/src/lib/ndpi_hash.c
index 8dd7beab0..567e1d67c 100644
--- a/src/lib/ndpi_hash.c
+++ b/src/lib/ndpi_hash.c
@@ -30,14 +30,14 @@
/* ******************************************************************** */
/* Based on djb2 hash - http://www.cse.yorku.ca/~oz/hash.html */
-u_int32_t ndpi_murmur_hash(char *str, u_int str_len) {
+u_int32_t ndpi_murmur_hash(const char *str, u_int str_len) {
return(MurmurHash((void*)str, str_len, 0x87654321));
}
/* ******************************************************************** */
/* Based on djb2 hash - http://www.cse.yorku.ca/~oz/hash.html */
-u_int32_t ndpi_quick_hash(unsigned char *str, u_int str_len) {
+u_int32_t ndpi_quick_hash(const unsigned char *str, u_int str_len) {
u_int32_t hash = 5381, i;
for(i=0; i<str_len; i++)
@@ -49,7 +49,7 @@ u_int32_t ndpi_quick_hash(unsigned char *str, u_int str_len) {
/* ******************************************************************** */
/* Based on Daniel Lemire code */
-u_int64_t ndpi_quick_hash64(char *str, u_int str_len) {
+u_int64_t ndpi_quick_hash64(const char *str, u_int str_len) {
u_int64_t h = 0;
u_int i;
@@ -68,7 +68,7 @@ u_int64_t ndpi_quick_hash64(char *str, u_int str_len) {
See also http://burtleburtle.net/bob/hash/spooky.html
*/
-u_int32_t ndpi_hash_string(char *str) {
+u_int32_t ndpi_hash_string(const char *str) {
u_int32_t hash, i;
for(hash = i = 0; str[i] != '\0'; ++i) {
@@ -86,7 +86,7 @@ u_int32_t ndpi_hash_string(char *str) {
/* ******************************************************************** */
-u_int32_t ndpi_rev_hash_string(char *str) {
+u_int32_t ndpi_rev_hash_string(const char *str) {
u_int32_t hash, i;
int len = strlen(str);
@@ -109,7 +109,7 @@ u_int32_t ndpi_rev_hash_string(char *str) {
/* ******************************************************************** */
/* Same as above but with strings with lenght */
-u_int32_t ndpi_hash_string_len(char *str, u_int len) {
+u_int32_t ndpi_hash_string_len(const char *str, u_int len) {
u_int32_t hash, i;
for(hash = i = 0; i< len; ++i) {