Coverage Report

Created: 2025-12-30 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fuzz_textfile.c
Line
Count
Source
1
/* Copyright 2022 Google LLC
2
Licensed under the Apache License, Version 2.0 (the "License");
3
you may not use this file except in compliance with the License.
4
You may obtain a copy of the License at
5
      http://www.apache.org/licenses/LICENSE-2.0
6
Unless required by applicable law or agreed to in writing, software
7
distributed under the License is distributed on an "AS IS" BASIS,
8
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
See the License for the specific language governing permissions and
10
limitations under the License.
11
*/
12
#include <config.h>
13
14
#include <stdint.h>
15
#include <stdio.h>
16
#include <string.h>
17
#include <stdlib.h>
18
#include <syslog.h>
19
#include <fcntl.h>
20
#include <unistd.h>
21
22
#include "src/textfile.h"
23
24
25
1.84k
char *get_null_terminated(const uint8_t **data, size_t *size) {
26
12.7k
#define STR_SIZE 75
27
1.84k
  if (*size < STR_SIZE || (int)*size < 0) {
28
46
    return NULL;
29
46
  }
30
31
1.80k
  char *new_s = malloc(STR_SIZE + 1);
32
1.80k
  memcpy(new_s, *data, STR_SIZE);
33
1.80k
  new_s[STR_SIZE] = '\0';
34
35
1.80k
  *data = *data+STR_SIZE;
36
1.80k
  *size -= STR_SIZE;
37
1.80k
  return new_s;
38
1.84k
}
39
40
616
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
41
616
  uint8_t *data_ptr = data;
42
616
  size_t size_val = size;
43
44
616
  char *key1 = get_null_terminated(&data_ptr, &size_val);
45
616
  char *val1 = get_null_terminated(&data_ptr, &size_val);
46
616
  char *key2 = get_null_terminated(&data_ptr, &size_val);
47
48
616
  if (!key1 || !val1 || !key2) {
49
18
    goto cleanup;
50
18
  }
51
  // Create a file with rest of content
52
598
  char filename[256];
53
598
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
54
598
  FILE *fp = fopen(filename, "wb");
55
598
  if (!fp) {
56
0
    return 0;
57
0
  }
58
598
  fwrite(data_ptr, size_val, 1, fp);
59
598
  fclose(fp);
60
61
598
  textfile_put(filename, key1, val1);
62
598
  textfile_get(filename, key2);
63
64
598
  unlink(filename);
65
66
616
cleanup:
67
68
616
  if (key1 != NULL) {
69
603
    free(key1);
70
603
    key1 = NULL;
71
603
  }
72
616
  if (val1 != NULL) {
73
601
    free(val1);
74
601
    val1 = NULL;
75
601
  }
76
616
  if (key2 != NULL) {
77
598
    free(key2);
78
598
    key2 = NULL;
79
598
  }
80
81
616
  return 0;
82
598
}