Coverage Report

Created: 2025-08-28 06:46

/src/ndpi/fuzz/fuzz_ds_btree.cpp
Line
Count
Source
1
#include "ndpi_api.h"
2
#include "fuzz_common_code.h"
3
4
#include <stdint.h>
5
#include <stdio.h>
6
#include <assert.h>
7
#include "fuzzer/FuzzedDataProvider.h"
8
9
264
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
10
264
  FuzzedDataProvider fuzzed_data(data, size);
11
264
  u_int16_t i, j, num_iteration, num_rows, num_columns, num_q_rows, num_q_columns, num_results;
12
264
  ndpi_btree *b;
13
264
  double **inputs, **q;
14
15
  /* Just to have some data */
16
264
  if (fuzzed_data.remaining_bytes() < 1024)
17
18
    return -1;
18
19
#if 0 /* TODO: ball.c code is not ready to handle memory allocation errors :( */
20
  /* To allow memory allocation failures */
21
  fuzz_set_alloc_callbacks_and_seed(size);
22
#endif
23
24
246
  num_rows = fuzzed_data.ConsumeIntegralInRange(1, 16);
25
246
  num_columns = fuzzed_data.ConsumeIntegralInRange(1, 16);
26
27
246
  inputs = (double **)ndpi_malloc(sizeof(double *) * num_rows);
28
1.77k
  for (i = 0; i < num_rows; i++) {
29
1.53k
    inputs[i] = (double *)ndpi_malloc(sizeof(double) * num_columns);
30
10.2k
    for (j = 0; j < num_columns; j++)
31
8.67k
      inputs[i][j] = fuzzed_data.ConsumeFloatingPoint<double>();
32
1.53k
  }
33
34
246
  num_q_rows = fuzzed_data.ConsumeIntegralInRange(1, 16);
35
246
  num_q_columns = fuzzed_data.ConsumeIntegralInRange(1, 16);
36
37
246
  q = (double **)ndpi_malloc(sizeof(double *) * num_q_rows);
38
1.33k
  for (i = 0; i < num_q_rows; i++) {
39
1.08k
    q[i] = (double *)ndpi_malloc(sizeof(double) * num_q_columns);
40
7.43k
    for (j = 0; j < num_q_columns; j++)
41
6.34k
      q[i][j] = fuzzed_data.ConsumeFloatingPoint<double>();
42
1.08k
  }
43
44
246
  num_results = fuzzed_data.ConsumeIntegralInRange((int)num_q_rows, 16);
45
46
246
  b = ndpi_btree_init(inputs, num_rows, num_columns);
47
48
246
  num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
49
11.4k
  for (i = 0; i < num_iteration; i++) {
50
11.1k
    ndpi_knn result;
51
52
11.1k
    result = ndpi_btree_query(b, q, num_q_rows, num_q_columns, num_results);
53
11.1k
    ndpi_free_knn(result);
54
11.1k
  }
55
56
1.77k
  for (i = 0; i < num_rows; i++)
57
1.53k
    ndpi_free(inputs[i]);
58
246
  ndpi_free(inputs);
59
1.33k
  for (i = 0; i < num_q_rows; i++)
60
1.08k
    ndpi_free(q[i]);
61
246
  ndpi_free(q);
62
246
  ndpi_free_btree(b);
63
64
246
  return 0;
65
264
}