Coverage Report

Created: 2025-11-02 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
261
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
10
261
  FuzzedDataProvider fuzzed_data(data, size);
11
261
  u_int16_t i, j, num_iteration, num_rows, num_columns, num_q_rows, num_q_columns, num_results;
12
261
  ndpi_btree *b;
13
261
  double **inputs, **q;
14
15
  /* Just to have some data */
16
261
  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
243
  num_rows = fuzzed_data.ConsumeIntegralInRange(1, 16);
25
243
  num_columns = fuzzed_data.ConsumeIntegralInRange(1, 16);
26
27
243
  inputs = (double **)ndpi_malloc(sizeof(double *) * num_rows);
28
1.82k
  for (i = 0; i < num_rows; i++) {
29
1.58k
    inputs[i] = (double *)ndpi_malloc(sizeof(double) * num_columns);
30
10.0k
    for (j = 0; j < num_columns; j++)
31
8.41k
      inputs[i][j] = fuzzed_data.ConsumeFloatingPoint<double>();
32
1.58k
  }
33
34
243
  num_q_rows = fuzzed_data.ConsumeIntegralInRange(1, 16);
35
243
  num_q_columns = fuzzed_data.ConsumeIntegralInRange(1, 16);
36
37
243
  q = (double **)ndpi_malloc(sizeof(double *) * num_q_rows);
38
1.24k
  for (i = 0; i < num_q_rows; i++) {
39
1.00k
    q[i] = (double *)ndpi_malloc(sizeof(double) * num_q_columns);
40
7.27k
    for (j = 0; j < num_q_columns; j++)
41
6.27k
      q[i][j] = fuzzed_data.ConsumeFloatingPoint<double>();
42
1.00k
  }
43
44
243
  num_results = fuzzed_data.ConsumeIntegralInRange((int)num_q_rows, 16);
45
46
243
  b = ndpi_btree_init(inputs, num_rows, num_columns);
47
48
243
  num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
49
11.2k
  for (i = 0; i < num_iteration; i++) {
50
10.9k
    ndpi_knn result;
51
52
10.9k
    result = ndpi_btree_query(b, q, num_q_rows, num_q_columns, num_results);
53
10.9k
    ndpi_free_knn(result);
54
10.9k
  }
55
56
1.82k
  for (i = 0; i < num_rows; i++)
57
1.58k
    ndpi_free(inputs[i]);
58
243
  ndpi_free(inputs);
59
1.24k
  for (i = 0; i < num_q_rows; i++)
60
1.00k
    ndpi_free(q[i]);
61
243
  ndpi_free(q);
62
243
  ndpi_free_btree(b);
63
64
243
  return 0;
65
261
}