Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfwevt/ossfuzz/xml_document_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libfwevt xml_document 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_libfwevt.h"
32
33
static const size_t max_string_size = 8 * 1024 * 1024;
34
35
static const int test_codepages[ 3 ] = {
36
  1252,
37
  932,
38
  65001
39
};
40
41
static const uint8_t test_flags[ 3 ] = {
42
  LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS,
43
  LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS | LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DEPENDENCY_IDENTIFIERS,
44
  0
45
};
46
47
int LLVMFuzzerTestOneInput(
48
     const uint8_t *data,
49
     size_t size )
50
5.36k
{
51
5.36k
  libfwevt_xml_document_t *xml_document = NULL;
52
5.36k
  libfwevt_xml_tag_t *root_xml_tag      = NULL;
53
5.36k
  uint16_t *utf16_string                = NULL;
54
5.36k
  uint8_t *utf8_name                    = NULL;
55
5.36k
  uint8_t *utf8_string                  = NULL;
56
5.36k
  size_t binary_data_offset             = 0;
57
5.36k
  size_t test_codepage_index            = 0;
58
5.36k
  size_t test_flags_index               = 0;
59
5.36k
  size_t utf8_name_size                 = 0;
60
5.36k
  size_t utf8_string_size               = 0;
61
5.36k
  size_t utf16_string_size              = 0;
62
63
  /* A binary XML document starts with a fragment header token.
64
   * In some test vectors the document is located at a non-zero
65
   * offset within the data, e.g. when the binary XML document
66
   * is part of a template, so look for the first fragment header
67
   * token.
68
   */
69
5.36k
  if( ( size > 0 )
70
5.36k
   && ( ( data[ 0 ] & 0xbf ) != LIBFWEVT_XML_TOKEN_FRAGMENT_HEADER ) )
71
357
  {
72
357
    for( binary_data_offset = 1;
73
6.42M
         binary_data_offset < size;
74
6.42M
         binary_data_offset++ )
75
6.42M
    {
76
6.42M
      if( ( data[ binary_data_offset ] & 0xbf ) == LIBFWEVT_XML_TOKEN_FRAGMENT_HEADER )
77
313
      {
78
313
        break;
79
313
      }
80
6.42M
    }
81
357
    if( binary_data_offset >= size )
82
44
    {
83
44
      binary_data_offset = 0;
84
44
    }
85
357
  }
86
5.36k
  for( test_codepage_index = 0;
87
21.4k
       test_codepage_index < ( sizeof( test_codepages ) / sizeof( test_codepages[ 0 ] ) );
88
16.0k
       test_codepage_index++ )
89
16.0k
  {
90
16.0k
    for( test_flags_index = 0;
91
64.3k
         test_flags_index < ( sizeof( test_flags ) / sizeof( test_flags[ 0 ] ) );
92
48.2k
         test_flags_index++ )
93
48.2k
    {
94
48.2k
      if( libfwevt_xml_document_initialize(
95
48.2k
               &xml_document,
96
48.2k
               NULL ) != 1 )
97
0
      {
98
0
        continue;
99
0
      }
100
      /* Note a read error is not necessarily fatal
101
       * the getters are still exercised on the resulting
102
       * (empty or partially read) document state
103
       */
104
48.2k
      libfwevt_xml_document_read(
105
48.2k
       xml_document,
106
48.2k
       data,
107
48.2k
       size,
108
48.2k
       binary_data_offset,
109
48.2k
       test_codepages[ test_codepage_index ],
110
48.2k
       test_flags[ test_flags_index ],
111
48.2k
       NULL );
112
113
48.2k
      if( libfwevt_xml_document_get_utf8_xml_string_size(
114
48.2k
               xml_document,
115
48.2k
               &utf8_string_size,
116
48.2k
               NULL ) == 1
117
14.7k
       && ( utf8_string_size > 0 )
118
14.7k
       && ( utf8_string_size < max_string_size ) )
119
14.7k
      {
120
14.7k
        utf8_string = (uint8_t *) malloc( utf8_string_size );
121
122
14.7k
        if( utf8_string != NULL )
123
14.7k
        {
124
14.7k
          libfwevt_xml_document_get_utf8_xml_string(
125
14.7k
           xml_document,
126
14.7k
           utf8_string,
127
14.7k
           utf8_string_size,
128
14.7k
           NULL );
129
14.7k
          free( utf8_string );
130
131
14.7k
          utf8_string = NULL;
132
14.7k
        }
133
14.7k
      }
134
48.2k
      if( libfwevt_xml_document_get_utf16_xml_string_size(
135
48.2k
               xml_document,
136
48.2k
               &utf16_string_size,
137
48.2k
               NULL ) == 1
138
14.7k
       && ( utf16_string_size > 0 )
139
14.7k
       && ( utf16_string_size < max_string_size ) )
140
14.7k
      {
141
        /* Note the string size is measured in UTF-16 characters (uint16_t values)
142
         * the buffer must be large enough in bytes to hold them
143
         */
144
14.7k
        utf16_string = (uint16_t *) malloc( utf16_string_size * sizeof( uint16_t ) );
145
146
14.7k
        if( utf16_string != NULL )
147
14.7k
        {
148
14.7k
          libfwevt_xml_document_get_utf16_xml_string(
149
14.7k
           xml_document,
150
14.7k
           utf16_string,
151
14.7k
           utf16_string_size,
152
14.7k
           NULL );
153
14.7k
          free( utf16_string );
154
155
14.7k
          utf16_string = NULL;
156
14.7k
        }
157
14.7k
      }
158
48.2k
      if( libfwevt_xml_document_get_root_xml_tag(
159
48.2k
               xml_document,
160
48.2k
               &root_xml_tag,
161
48.2k
               NULL ) == 1 )
162
48.2k
      {
163
48.2k
        if( libfwevt_xml_tag_get_utf8_name_size(
164
48.2k
                   root_xml_tag,
165
48.2k
                   &utf8_name_size,
166
48.2k
                   NULL ) == 1
167
15.2k
           && ( utf8_name_size > 0 )
168
15.2k
           && ( utf8_name_size < max_string_size ) )
169
15.2k
        {
170
15.2k
          utf8_name = (uint8_t *) malloc( utf8_name_size );
171
172
15.2k
          if( utf8_name != NULL )
173
15.2k
          {
174
15.2k
            libfwevt_xml_tag_get_utf8_name(
175
15.2k
             root_xml_tag,
176
15.2k
             utf8_name,
177
15.2k
             utf8_name_size,
178
15.2k
             NULL );
179
15.2k
            free( utf8_name );
180
181
15.2k
            utf8_name = NULL;
182
15.2k
          }
183
15.2k
        }
184
48.2k
      }
185
48.2k
      goto on_error_xml_document;
186
187
48.2k
   on_error_xml_document:
188
48.2k
      if( xml_document != NULL )
189
48.2k
      {
190
48.2k
        libfwevt_xml_document_free(
191
48.2k
         &xml_document,
192
48.2k
         NULL );
193
48.2k
      }
194
48.2k
    }
195
16.0k
  }
196
5.36k
  return( 0 );
197
5.36k
}
198
199
} /* extern "C" */