Coverage Report

Created: 2026-10-19 06:45

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.28M
#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
15.8k
void af_gb_init() {
46
15.8k
  pointer_idx = 0;
47
48
1.59M
   for (int i = 0; i < GB_SIZE; i++) {
49
1.58M
     pointer_arr[i] = NULL;
50
1.58M
   }
51
15.8k
}
52
53
15.8k
void af_gb_cleanup() {
54
1.59M
  for(int i = 0; i < GB_SIZE; i++) {
55
1.58M
    if (pointer_arr[i] != NULL) {
56
75.1k
      free(pointer_arr[i]);
57
75.1k
    }
58
1.58M
  }
59
15.8k
}
60
61
76.3k
void *af_get_null_terminated(const uint8_t **data, size_t *size) {
62
489k
#define STR_SIZE 75
63
76.3k
  if (*size < STR_SIZE || (int)*size < 0) {
64
9.10k
    return NULL;
65
9.10k
  }
66
67
67.2k
  void *new_s = malloc(STR_SIZE + 1);
68
67.2k
  memcpy(new_s, *data, STR_SIZE);
69
67.2k
  ((uint8_t *)new_s)[STR_SIZE] = '\0';
70
71
67.2k
  *data = *data+STR_SIZE;
72
67.2k
  *size -= STR_SIZE;
73
67.2k
  return new_s;
74
76.3k
}
75
76
9.52k
void *af_gb_get_random_data(const uint8_t **data, size_t *size, size_t to_get) {
77
9.52k
  if (pointer_idx >= GB_SIZE || *size < to_get || (int)*size < 0) {
78
1.30k
    return NULL;
79
1.30k
  }
80
81
8.22k
  void *new_s = malloc(to_get);
82
8.22k
  memcpy(new_s, *data, to_get);
83
84
8.22k
  pointer_arr[pointer_idx++] = new_s;
85
  
86
8.22k
  *data = *data + to_get;
87
8.22k
  *size -= to_get;
88
89
8.22k
  return new_s;
90
9.52k
}
91
92
73.0k
void *af_gb_get_null_terminated(const uint8_t **data, size_t *size) {
93
73.0k
  if (pointer_idx >= GB_SIZE) {
94
0
    return NULL;
95
0
  }
96
97
73.0k
  void *nstr = af_get_null_terminated(data, size);
98
73.0k
  if (nstr == NULL) {
99
6.16k
    return NULL;
100
6.16k
  }
101
66.9k
  pointer_arr[pointer_idx++] = nstr;
102
66.9k
  return nstr;
103
73.0k
}
104
105
0
void *af_gb_alloc_data(size_t len) {
106
0
  if (pointer_idx >= GB_SIZE) {
107
0
    return NULL;
108
0
  }
109
0
  void *ptr = calloc(1, len);
110
0
  pointer_arr[pointer_idx++] = ptr;
111
  
112
0
  return ptr;
113
0
}
114
115
20.1k
short af_get_short(const uint8_t **data, size_t *size) {
116
20.1k
  if (*size <= 0) return 0;
117
19.6k
  short c = (short)(*data)[0];
118
19.6k
  *data += 1;
119
19.6k
  *size -= 1;
120
19.6k
  return c;
121
20.1k
}
122
123
30.0k
int af_get_int(const uint8_t **data, size_t *size) {
124
30.0k
  int val;
125
126
30.0k
  if (*size < sizeof(val)) return 0;
127
19.2k
  memcpy(&val, *data, sizeof(val));
128
19.2k
  *data += sizeof(val);
129
19.2k
  *size -= sizeof(val);
130
19.2k
  return val;
131
30.0k
}
132
// end simple garbage collector.