Coverage Report

Created: 2026-07-10 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libodraw/ossfuzz/handle_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libodraw file type
3
 *
4
 * Copyright (C) 2010-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
/* Note that some of the OSS-Fuzz engines use C++
26
 */
27
extern "C" {
28
29
#include "ossfuzz_libbfio.h"
30
#include "ossfuzz_libodraw.h"
31
32
#if !defined( LIBODRAW_HAVE_BFIO )
33
34
/* Opens a handle using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBODRAW_EXTERN \
38
int libodraw_handle_open_file_io_handle(
39
     libodraw_handle_t *handle,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libodraw_error_t **error );
43
44
#endif /* !defined( LIBODRAW_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
     const uint8_t *data,
48
     size_t size )
49
4.53k
{
50
4.53k
  uint8_t buffer[ 512 ];
51
52
4.53k
  libbfio_handle_t *file_io_handle = NULL;
53
4.53k
  libbfio_pool_t *file_io_pool     = NULL;
54
4.53k
  libodraw_handle_t *handle        = NULL;
55
4.53k
  off64_t media_offset             = 0;
56
4.53k
  size64_t media_size              = 0;
57
4.53k
  int read_iterator                = 0;
58
59
4.53k
  if( libbfio_memory_range_initialize(
60
4.53k
       &file_io_handle,
61
4.53k
       NULL ) != 1 )
62
0
  {
63
0
    return( 0 );
64
0
  }
65
4.53k
  if( libbfio_memory_range_set(
66
4.53k
       file_io_handle,
67
4.53k
       (uint8_t *) data,
68
4.53k
       size,
69
4.53k
       NULL ) != 1 )
70
0
  {
71
0
    goto on_error_libbfio;
72
0
  }
73
4.53k
  if( libodraw_handle_initialize(
74
4.53k
       &handle,
75
4.53k
       NULL ) != 1 )
76
0
  {
77
0
    goto on_error_libbfio;
78
0
  }
79
4.53k
  if( libodraw_handle_open_file_io_handle(
80
4.53k
       handle,
81
4.53k
       file_io_handle,
82
4.53k
       LIBODRAW_OPEN_READ,
83
4.53k
       NULL ) != 1 )
84
3.48k
  {
85
3.48k
    goto on_error_libodraw;
86
3.48k
  }
87
1.05k
  if( libodraw_handle_get_media_size(
88
1.05k
       handle,
89
1.05k
       &media_size,
90
1.05k
       NULL ) != 1 )
91
0
  {
92
0
    goto on_error_libodraw;
93
0
  }
94
1.05k
  for( read_iterator = 0;
95
7.05k
       read_iterator < 128;
96
5.99k
       read_iterator++ )
97
7.00k
  {
98
7.00k
    if( media_offset >= media_size )
99
864
    {
100
864
      break;
101
864
    }
102
6.14k
    if( libodraw_handle_read_buffer_at_offset(
103
6.14k
         handle,
104
6.14k
         buffer,
105
6.14k
         497,
106
6.14k
         media_offset,
107
6.14k
         NULL ) == -1 )
108
149
    {
109
149
      goto on_error_libodraw;
110
149
    }
111
5.99k
    media_offset += 497;
112
5.99k
  }
113
909
  libodraw_handle_close(
114
909
   handle,
115
909
   NULL );
116
117
4.53k
on_error_libodraw:
118
4.53k
  libodraw_handle_free(
119
4.53k
   &handle,
120
4.53k
   NULL );
121
122
4.53k
on_error_libbfio:
123
4.53k
  libbfio_handle_free(
124
4.53k
   &file_io_handle,
125
4.53k
   NULL );
126
127
4.53k
  return( 0 );
128
4.53k
}
129
130
} /* extern "C" */
131