Coverage Report

Created: 2026-09-19 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ndpi/fuzz/fuzz_ds_domain_classify.cpp
Line
Count
Source
1
#include "ndpi_api.h"
2
#include "ndpi_private.h"
3
#include "fuzz_common_code.h"
4
5
#include <stdint.h>
6
#include <stdio.h>
7
#include <assert.h>
8
#include "fuzzer/FuzzedDataProvider.h"
9
10
static struct ndpi_detection_module_struct *ndpi_struct = NULL;
11
12
extern "C" {
13
14
#ifdef NDPI_ENABLE_DEBUG_MESSAGES
15
void ndpi_debug_printf(uint16_t proto, struct ndpi_detection_module_struct *ndpi_str, ndpi_log_level_t log_level,
16
                       const char *file_name, const char *func_name, unsigned int line_number, const char *format, ...);
17
#endif
18
19
1.44k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
20
1.44k
  FuzzedDataProvider fuzzed_data(data, size);
21
1.44k
  u_int16_t i, num_iteration, is_added = 0;
22
1.44k
  bool rc;
23
1.44k
  ndpi_domain_classify *d;
24
1.44k
  u_int64_t class_id;
25
1.44k
  std::string value, value_added;
26
27
  /* We don't need a complete (and costly to set up) context!
28
     Setting up manually only what is really needed is complex (and error prone!)
29
     but allow us to be significant faster and to have better coverage */
30
1.44k
  if (ndpi_struct == NULL) {
31
1
    ndpi_struct = (struct ndpi_detection_module_struct *)ndpi_calloc(1, sizeof(struct ndpi_detection_module_struct));
32
#ifdef NDPI_ENABLE_DEBUG_MESSAGES
33
    set_ndpi_debug_function(ndpi_struct, (ndpi_debug_function_ptr)ndpi_debug_printf);
34
#endif
35
1
    if (ndpi_struct) {
36
1
      ndpi_struct->cfg.log_level = NDPI_LOG_DEBUG_EXTRA;
37
1
      ndpi_load_domain_suffixes(ndpi_struct, (char *)"public_suffix_list.dat");
38
1
    }
39
1
  }
40
41
  /* To allow memory allocation failures */
42
1.44k
  fuzz_set_alloc_callbacks_and_seed(size);
43
44
1.44k
  d = ndpi_domain_classify_alloc();
45
46
1.44k
  num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
47
  
48
126k
  for (i = 0; i < num_iteration; i++) {
49
124k
    value = fuzzed_data.ConsumeBytesAsString(fuzzed_data.ConsumeIntegral<u_int8_t>());
50
124k
    class_id = fuzzed_data.ConsumeIntegral<u_int64_t>();
51
124k
    rc = ndpi_domain_classify_add(fuzzed_data.ConsumeBool() ? ndpi_struct : NULL,
52
124k
                                  d, class_id, (char*)value.c_str());
53
    
54
    /* Keep one random entry really added */
55
124k
    if (rc == true && is_added == 0 && fuzzed_data.ConsumeBool()) {
56
790
      value_added = value;
57
790
      is_added = 1;
58
790
    }
59
124k
  }
60
61
1.44k
  ndpi_domain_classify_add_domains(ndpi_struct, d,
62
1.44k
           fuzzed_data.ConsumeIntegralInRange(0, 512),
63
1.44k
           fuzzed_data.ConsumeBool() ? (char *)"random_list.list" : (char *)"wrong_path");
64
65
  /* "Random" search */
66
1.44k
  num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
67
82.5k
  for (i = 0; i < num_iteration; i++) {
68
81.0k
    value = fuzzed_data.ConsumeBytesAsString(fuzzed_data.ConsumeIntegral<u_int8_t>());
69
81.0k
    ndpi_domain_classify_hostname(fuzzed_data.ConsumeBool() ? ndpi_struct : NULL, d, &class_id, (char *)value.c_str());
70
81.0k
  }
71
  
72
  /* Search of an added entry */
73
1.44k
  if (is_added) {
74
790
    ndpi_domain_classify_hostname(ndpi_struct, d, &class_id, (char *)value_added.c_str());
75
790
  }
76
77
1.44k
  ndpi_domain_classify_size(d);
78
1.44k
  ndpi_domain_classify_free(d);
79
  
80
1.44k
  return 0;
81
1.44k
}
82
83
}