Coverage Report

Created: 2025-07-02 06:28

/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
319
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
10
319
  FuzzedDataProvider fuzzed_data(data, size);
11
319
  u_int16_t i, j, num_iteration, num_rows, num_columns, num_q_rows, num_q_columns, num_results;
12
319
  ndpi_btree *b;
13
319
  double **inputs, **q;
14
15
  /* Just to have some data */
16
319
  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
301
  num_rows = fuzzed_data.ConsumeIntegralInRange(1, 16);
25
301
  num_columns = fuzzed_data.ConsumeIntegralInRange(1, 16);
26
27
301
  inputs = (double **)ndpi_malloc(sizeof(double *) * num_rows);
28
2.54k
  for (i = 0; i < num_rows; i++) {
29
2.24k
    inputs[i] = (double *)ndpi_malloc(sizeof(double) * num_columns);
30
12.4k
    for (j = 0; j < num_columns; j++)
31
10.1k
      inputs[i][j] = fuzzed_data.ConsumeFloatingPoint<double>();
32
2.24k
  }
33
34
301
  num_q_rows = fuzzed_data.ConsumeIntegralInRange(1, 16);
35
301
  num_q_columns = fuzzed_data.ConsumeIntegralInRange(1, 16);
36
37
301
  q = (double **)ndpi_malloc(sizeof(double *) * num_q_rows);
38
1.96k
  for (i = 0; i < num_q_rows; i++) {
39
1.66k
    q[i] = (double *)ndpi_malloc(sizeof(double) * num_q_columns);
40
9.64k
    for (j = 0; j < num_q_columns; j++)
41
7.98k
      q[i][j] = fuzzed_data.ConsumeFloatingPoint<double>();
42
1.66k
  }
43
44
301
  num_results = fuzzed_data.ConsumeIntegralInRange((int)num_q_rows, 16);
45
46
301
  b = ndpi_btree_init(inputs, num_rows, num_columns);
47
48
301
  num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
49
17.5k
  for (i = 0; i < num_iteration; i++) {
50
17.2k
    ndpi_knn result;
51
52
17.2k
    result = ndpi_btree_query(b, q, num_q_rows, num_q_columns, num_results);
53
17.2k
    ndpi_free_knn(result);
54
17.2k
  }
55
56
2.54k
  for (i = 0; i < num_rows; i++)
57
2.24k
    ndpi_free(inputs[i]);
58
301
  ndpi_free(inputs);
59
1.96k
  for (i = 0; i < num_q_rows; i++)
60
1.66k
    ndpi_free(q[i]);
61
301
  ndpi_free(q);
62
301
  ndpi_free_btree(b);
63
64
301
  return 0;
65
319
}