Coverage Report

Created: 2026-04-01 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/net-snmp/testing/fuzzing/ada_fuzz_header.h
Line
Count
Source
1
/*
2
  * Copyright (c) 2021, Net-snmp authors
3
  * All rights reserved.
4
  *
5
  * Redistribution and use in source and binary forms, with or without
6
  * modification, are permitted provided that the following conditions are met:
7
  *
8
  * * Redistributions of source code must retain the above copyright notice, this
9
  *   list of conditions and the following disclaimer.
10
  *
11
  * * Redistributions in binary form must reproduce the above copyright notice,
12
  *   this list of conditions and the following disclaimer in the documentation
13
  *   and/or other materials provided with the distribution.
14
  *
15
  * * Neither the name of the copyright holder nor the names of its
16
  *   contributors may be used to endorse or promote products derived from
17
  *   this software without specific prior written permission.
18
  *
19
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
*/
30
31
/*
32
 * Declare functions.
33
 */
34
void af_gb_init(void);
35
void af_gb_cleanup(void);
36
37
// Simple garbage collector 
38
3.26M
#define GB_SIZE 100
39
40
void *pointer_arr[GB_SIZE];
41
int pointer_idx;
42
43
// If the garbage collector is used then this must be called as first thing
44
// during a fuzz run.
45
16.1k
void af_gb_init() {
46
16.1k
  pointer_idx = 0;
47
48
1.63M
   for (int i = 0; i < GB_SIZE; i++) {
49
1.61M
     pointer_arr[i] = NULL;
50
1.61M
   }
51
16.1k
}
52
53
16.1k
void af_gb_cleanup() {
54
1.63M
  for(int i = 0; i < GB_SIZE; i++) {
55
1.61M
    if (pointer_arr[i] != NULL) {
56
73.8k
      free(pointer_arr[i]);
57
73.8k
    }
58
1.61M
  }
59
16.1k
}
60
61
74.9k
void *af_get_null_terminated(const uint8_t **data, size_t *size) {
62
478k
#define STR_SIZE 75
63
74.9k
  if (*size < STR_SIZE || (int)*size < 0) {
64
9.19k
    return NULL;
65
9.19k
  }
66
67
65.7k
  void *new_s = malloc(STR_SIZE + 1);
68
65.7k
  memcpy(new_s, *data, STR_SIZE);
69
65.7k
  ((uint8_t *)new_s)[STR_SIZE] = '\0';
70
71
65.7k
  *data = *data+STR_SIZE;
72
65.7k
  *size -= STR_SIZE;
73
65.7k
  return new_s;
74
74.9k
}
75
76
9.79k
void *af_gb_get_random_data(const uint8_t **data, size_t *size, size_t to_get) {
77
9.79k
  if (*size < to_get || (int)*size < 0) {
78
1.33k
    return NULL;
79
1.33k
  }
80
81
8.46k
  void *new_s = malloc(to_get);
82
8.46k
  memcpy(new_s, *data, to_get);
83
84
8.46k
  pointer_arr[pointer_idx++] = new_s;
85
  
86
8.46k
  *data = *data + to_get;
87
8.46k
  *size -= to_get;
88
89
8.46k
  return new_s;
90
9.79k
}
91
92
71.6k
void *af_gb_get_null_terminated(const uint8_t **data, size_t *size) {
93
94
71.6k
  void *nstr = af_get_null_terminated(data, size);
95
71.6k
  if (nstr == NULL) {
96
6.21k
    return NULL;
97
6.21k
  }
98
65.4k
  pointer_arr[pointer_idx++] = nstr;
99
65.4k
  return nstr;
100
71.6k
}
101
102
0
void *af_gb_alloc_data(size_t len) {
103
0
  void *ptr = calloc(1, len);
104
0
  pointer_arr[pointer_idx++] = ptr;
105
  
106
0
  return ptr;
107
0
}
108
109
21.2k
short af_get_short(const uint8_t **data, size_t *size) {
110
21.2k
  if (*size <= 0) return 0;
111
20.7k
  short c = (short)(*data)[0];
112
20.7k
  *data += 1;
113
20.7k
  *size -= 1;
114
20.7k
  return c;
115
21.2k
}
116
117
30.2k
int af_get_int(const uint8_t **data, size_t *size) {
118
30.2k
  if (*size <= 4) return 0;
119
19.4k
  const uint8_t *ptr = *data;
120
19.4k
  int val = *((const int*)ptr);
121
19.4k
  *data += 4;
122
19.4k
  *size -= 4;
123
19.4k
  return val;
124
30.2k
}
125
// end simple garbage collector.