Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libuna/ossfuzz/scsu_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libuna SCSU stream type
3
 *
4
 * Copyright (C) 2011-2026, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <stddef.h>
23
#include <stdint.h>
24
25
#include <stdlib.h>
26
27
/* Note that some of the OSS-Fuzz engines use C++
28
 */
29
extern "C" {
30
31
#include "ossfuzz_libuna.h"
32
33
static const size_t max_string_size = 8 * 1024 * 1024;
34
35
static void fuzzer_utf8_string_from_scsu_stream(
36
     const uint8_t *data,
37
     size_t size )
38
923
{
39
923
  libuna_utf8_character_t *utf8_string  = NULL;
40
923
  size_t utf8_string_index              = 0;
41
923
  size_t utf8_string_size               = 0;
42
43
923
  if( libuna_utf8_string_size_from_scsu_stream(
44
923
       data,
45
923
       size,
46
923
       &utf8_string_size,
47
923
       NULL ) == 1
48
838
   && ( utf8_string_size > 0 )
49
769
   && ( utf8_string_size < max_string_size ) )
50
769
  {
51
769
    utf8_string = (libuna_utf8_character_t *) malloc( utf8_string_size );
52
53
769
    if( utf8_string != NULL )
54
769
    {
55
769
      libuna_utf8_string_copy_from_scsu_stream(
56
769
       utf8_string,
57
769
       utf8_string_size,
58
769
       data,
59
769
       size,
60
769
       NULL );
61
62
769
      utf8_string_index = 0;
63
769
      libuna_utf8_string_with_index_copy_from_scsu_stream(
64
769
       utf8_string,
65
769
       utf8_string_size / 2,
66
769
       &utf8_string_index,
67
769
       data,
68
769
       size,
69
769
       NULL );
70
71
769
      free( utf8_string );
72
73
769
      utf8_string = NULL;
74
769
    }
75
769
  }
76
923
}
77
78
static void fuzzer_utf16_string_from_scsu_stream(
79
     const uint8_t *data,
80
     size_t size )
81
923
{
82
923
  libuna_utf16_character_t *utf16_string   = NULL;
83
923
  size_t utf16_string_index                = 0;
84
923
  size_t utf16_string_size                 = 0;
85
86
923
  if( libuna_utf16_string_size_from_scsu_stream(
87
923
       data,
88
923
       size,
89
923
       &utf16_string_size,
90
923
       NULL ) == 1
91
838
   && ( utf16_string_size > 0 )
92
769
   && ( utf16_string_size < max_string_size ) )
93
769
  {
94
769
    utf16_string = (libuna_utf16_character_t *) malloc( utf16_string_size * sizeof( libuna_utf16_character_t ) );
95
96
769
    if( utf16_string != NULL )
97
769
    {
98
769
      libuna_utf16_string_copy_from_scsu_stream(
99
769
       utf16_string,
100
769
       utf16_string_size,
101
769
       data,
102
769
       size,
103
769
       NULL );
104
105
769
      utf16_string_index = 0;
106
769
      libuna_utf16_string_with_index_copy_from_scsu_stream(
107
769
       utf16_string,
108
769
       utf16_string_size / 2,
109
769
       &utf16_string_index,
110
769
       data,
111
769
       size,
112
769
       NULL );
113
114
769
      free( utf16_string );
115
116
769
      utf16_string = NULL;
117
769
    }
118
769
  }
119
923
}
120
121
static void fuzzer_utf32_string_from_scsu_stream(
122
     const uint8_t *data,
123
     size_t size )
124
923
{
125
923
  libuna_utf32_character_t *utf32_string   = NULL;
126
923
  size_t utf32_string_index                = 0;
127
923
  size_t utf32_string_size                 = 0;
128
129
923
  if( libuna_utf32_string_size_from_scsu_stream(
130
923
       data,
131
923
       size,
132
923
       &utf32_string_size,
133
923
       NULL ) == 1
134
838
   && ( utf32_string_size > 0 )
135
769
   && ( utf32_string_size < max_string_size ) )
136
769
  {
137
769
    utf32_string = (libuna_utf32_character_t *) malloc( utf32_string_size * sizeof( libuna_utf32_character_t ) );
138
139
769
    if( utf32_string != NULL )
140
769
    {
141
769
      libuna_utf32_string_copy_from_scsu_stream(
142
769
       utf32_string,
143
769
       utf32_string_size,
144
769
       data,
145
769
       size,
146
769
       NULL );
147
148
769
      utf32_string_index = 0;
149
769
      libuna_utf32_string_with_index_copy_from_scsu_stream(
150
769
       utf32_string,
151
769
       utf32_string_size / 2,
152
769
       &utf32_string_index,
153
769
       data,
154
769
       size,
155
769
       NULL );
156
157
769
      free( utf32_string );
158
159
769
      utf32_string = NULL;
160
769
    }
161
769
  }
162
923
}
163
164
int LLVMFuzzerTestOneInput(
165
     const uint8_t *data,
166
     size_t size )
167
8.90k
{
168
8.90k
  fuzzer_utf8_string_from_scsu_stream(
169
8.90k
   data,
170
8.90k
   size );
171
172
8.90k
  fuzzer_utf16_string_from_scsu_stream(
173
8.90k
   data,
174
8.90k
   size );
175
176
8.90k
  fuzzer_utf32_string_from_scsu_stream(
177
8.90k
   data,
178
8.90k
   size );
179
180
8.90k
  return( 0 );
181
8.90k
}
182
183
} /* extern "C" */