Coverage Report

Created: 2026-05-30 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/liblouis/tests/fuzzing/fuzz_backtranslate.c
Line
Count
Source
1
//
2
// liblouis Braille Translation and Back-Translation Library
3
//
4
// Copyright (C) 2023 Anna Stan, Nicolas Morel, Kalilou Mamadou Dramé
5
//
6
// This file is part of liblouis.
7
//
8
// liblouis is free software: you can redistribute it and/or modify it
9
// under the terms of the GNU Lesser General Public License as published
10
// by the Free Software Foundation, either version 2.1 of the License, or
11
// (at your option) any later version.
12
//
13
// liblouis is distributed in the hope that it will be useful, but
14
// WITHOUT ANY WARRANTY; without even the implied warranty of
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
// Lesser General Public License for more details.
17
//
18
// You should have received a copy of the GNU Lesser General Public
19
// License along with liblouis. If not, see <http://www.gnu.org/licenses/>.
20
//
21
22
#include <config.h>
23
24
#include <assert.h>
25
#include <fcntl.h>
26
#include <internal.h>
27
#include <liblouis.h>
28
#include <stdint.h>
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
32
#include <unistd.h>
33
34
#define LANGUAGE "en"
35
36
static int initialized = 0;
37
38
#define BOLDRED(x) "\x1b[31m\x1b[1m" x "\x1b[0m"
39
40
static const char *table_default;
41
42
0
static void __attribute__((destructor)) free_ressources(void) { lou_free(); }
43
44
200k
void avoid_log(logLevels level, const char *msg) {
45
200k
  (void)level;
46
200k
  (void)msg;
47
200k
}
48
49
extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
50
51
1.05k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
52
1.05k
  int inputLen = 0;
53
1.05k
  int outputLen = 0;
54
1.05k
  static int counter = 0;
55
1.05k
  if (!initialized) {
56
2
    lou_registerLogCallback(avoid_log);
57
2
    initialized = 1;
58
2
  }
59
60
1.05k
  if (size < 512) {
61
0
    return 0;
62
0
  }
63
64
  // Write first half of fuzz data to a table file.
65
1.05k
  char new_file[256];
66
1.05k
  sprintf(new_file, "/tmp/libfuzzer-%d.ctb", counter);
67
1.05k
  counter++;
68
69
1.05k
  FILE *fp = fopen(new_file, "wb");
70
1.05k
  if (!fp) {
71
0
    return 0;
72
0
  }
73
1.05k
  fwrite(data, 512, 1, fp);
74
1.05k
  fclose(fp);
75
76
  // Adjust data pointer to after the table file data.
77
1.05k
  data += 512;
78
1.05k
  size -= 512;
79
80
  /* check if this works, otherwise bail */
81
1.05k
  if (lou_checkTable(new_file) == 0) {
82
330
    lou_free();
83
330
    unlink(new_file);
84
330
    return 0;
85
330
  }
86
727
  char *mutable_data = NULL;
87
727
  table_default = new_file;
88
89
727
  mutable_data = strndup((char *)data, size);
90
727
  if (!mutable_data) {
91
0
    perror("malloc");
92
0
    exit(1);
93
0
  }
94
95
727
  widechar *inputText = malloc((size * 16 + 1) * sizeof(widechar));
96
727
  int len = (int)_lou_extParseChars(mutable_data, inputText);
97
727
  free(mutable_data);
98
727
  if (len <= 0) {
99
14
    free(inputText);
100
14
    unlink(new_file);
101
14
    lou_free();
102
14
    return -1;
103
14
  }
104
105
727
  assert(len <= (size * 16));
106
713
  inputLen = len;
107
713
  outputLen = len * 16;
108
713
  widechar *outputText = malloc((outputLen + 1) * sizeof(widechar));
109
110
713
  lou_backTranslateString(table_default, inputText, &inputLen, outputText,
111
713
                      &outputLen, NULL, NULL, ucBrl);
112
113
713
  free(inputText);
114
713
  free(outputText);
115
116
713
  lou_free();
117
713
  unlink(new_file);
118
119
713
  return 0;
120
713
}
121
122
extern int LLVMFuzzerRunDriver(int *argc, char ***argv,
123
                              int (*UserCb)(const uint8_t *Data, size_t Size));
124
125
0
int main2(int argc, char *argv[]) {
126
       //LLVMFuzzerRunDriver(LLVMFuzzerRunDriver(&argcargc, &argv, LLVMFuzzerTestOneInput);
127
0
}