Coverage Report

Created: 2023-06-10 07:25

/src/liblouis/tests/fuzzing/fuzz_translate_generic.c
Line
Count
Source (jump to first uncovered line)
1
//
2
// liblouis Braille Translation and Back-Translation Library
3
//
4
// Copyright (C) 2022 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 <stdlib.h>
23
#include <stdint.h>
24
#include <stdio.h>
25
#include <string.h>
26
27
#include <internal.h>
28
#include <liblouis.h>
29
#include <assert.h>
30
31
#define LANGUAGE  "en"
32
33
static int initialized = 0;
34
35
#define BOLDRED(x)  "\x1b[31m\x1b[1m" x "\x1b[0m"
36
37
static const char *table_default;
38
39
static void __attribute__((destructor))
40
0
free_ressources(void) {
41
0
  lou_free();
42
0
}
43
44
void
45
303k
avoid_log(logLevels level, const char *msg) {
46
303k
  (void) level;
47
303k
  (void) msg;
48
303k
}
49
50
extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
51
52
int
53
436
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
54
436
  int inputLen = 0;
55
436
  int outputLen = 0;
56
436
    static int counter = 0;
57
436
  if (!initialized)
58
1
  {
59
1
    lou_registerLogCallback(avoid_log);
60
1
    initialized = 1;
61
1
  }
62
    
63
436
    if (size < 512) {
64
0
        return 0;
65
0
    }
66
67
    // Write first half of fuzz data to a table file.
68
436
    char new_file[256];
69
436
    sprintf(new_file, "/tmp/libfuzzer-%d.ctb", counter);
70
436
    counter++;
71
72
436
  FILE *fp = fopen(new_file, "wb");
73
436
  if (!fp) {
74
0
    return 0;
75
0
    }
76
436
  fwrite(data, 512, 1, fp);
77
436
  fclose(fp);
78
79
    // Adjust data pointer to after the table file data.
80
436
    data += 512;
81
436
    size -= 512;
82
83
    /* check if this works, otherwise bail */
84
436
    if (lou_checkTable(new_file) == 0) {
85
18
        lou_free();
86
18
        unlink(new_file);
87
18
        return 0;
88
18
    }
89
418
  char *mutable_data = NULL;
90
418
    table_default = new_file;
91
92
418
  mutable_data = strndup((char*)data, size);
93
418
  if (!mutable_data) 
94
0
  {
95
0
    perror("malloc");
96
0
    exit(1);
97
0
  }
98
99
418
  widechar *inputText = malloc((size*16+1)*sizeof(widechar));
100
418
  int len = (int)_lou_extParseChars(mutable_data, inputText);
101
418
  free(mutable_data);
102
418
  if (len <= 0) {
103
38
    free(inputText);
104
38
        unlink(new_file);
105
38
        lou_free();
106
38
    return -1;
107
38
  }
108
109
380
  assert(len <= (size*16));
110
380
  inputLen = len;
111
380
  outputLen = len*16;
112
380
  widechar *outputText = malloc((outputLen+1)*sizeof(widechar));
113
114
380
  lou_translateString(table_default, inputText, &inputLen, outputText, &outputLen, NULL, NULL, ucBrl);
115
116
380
  free(inputText);
117
380
  free(outputText);
118
380
    lou_free();
119
380
    unlink(new_file);
120
121
380
  return 0;
122
380
}