/src/libvsgpt/ossfuzz/partition_fuzzer.cc
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * OSS-Fuzz target for libvsgpt volume type | 
| 3 |  |  * | 
| 4 |  |  * Copyright (C) 2019-2024, 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_libvsgpt.h" | 
| 31 |  |  | 
| 32 |  | #if !defined( LIBVSGPT_HAVE_BFIO ) | 
| 33 |  |  | 
| 34 |  | /* Opens a volume using a Basic File IO (bfio) handle | 
| 35 |  |  * Returns 1 if successful or -1 on error | 
| 36 |  |  */ | 
| 37 |  | LIBVSGPT_EXTERN \ | 
| 38 |  | int libvsgpt_volume_open_file_io_handle( | 
| 39 |  |      libvsgpt_volume_t *volume, | 
| 40 |  |      libbfio_handle_t *file_io_handle, | 
| 41 |  |      int access_flags, | 
| 42 |  |      libvsgpt_error_t **error ); | 
| 43 |  |  | 
| 44 |  | #endif /* !defined( LIBVSGPT_HAVE_BFIO ) */ | 
| 45 |  |  | 
| 46 |  | int LLVMFuzzerTestOneInput( | 
| 47 |  |      const uint8_t *data, | 
| 48 |  |      size_t size ) | 
| 49 | 9.52k | { | 
| 50 | 9.52k |   libbfio_handle_t *file_io_handle = NULL; | 
| 51 | 9.52k |   libvsgpt_partition_t *partition  = NULL; | 
| 52 | 9.52k |   libvsgpt_volume_t *volume        = NULL; | 
| 53 | 9.52k |   int number_of_partitions         = 0; | 
| 54 |  |  | 
| 55 | 9.52k |   if( libbfio_memory_range_initialize( | 
| 56 | 9.52k |        &file_io_handle, | 
| 57 | 9.52k |        NULL ) != 1 ) | 
| 58 | 0 |   { | 
| 59 | 0 |     return( 0 ); | 
| 60 | 0 |   } | 
| 61 | 9.52k |   if( libbfio_memory_range_set( | 
| 62 | 9.52k |        file_io_handle, | 
| 63 | 9.52k |        (uint8_t *) data, | 
| 64 | 9.52k |        size, | 
| 65 | 9.52k |        NULL ) != 1 ) | 
| 66 | 0 |   { | 
| 67 | 0 |     goto on_error_libbfio; | 
| 68 | 0 |   } | 
| 69 | 9.52k |   if( libvsgpt_volume_initialize( | 
| 70 | 9.52k |        &volume, | 
| 71 | 9.52k |        NULL ) != 1 ) | 
| 72 | 0 |   { | 
| 73 | 0 |     goto on_error_libbfio; | 
| 74 | 0 |   } | 
| 75 | 9.52k |   if( libvsgpt_volume_open_file_io_handle( | 
| 76 | 9.52k |        volume, | 
| 77 | 9.52k |        file_io_handle, | 
| 78 | 9.52k |        LIBVSGPT_OPEN_READ, | 
| 79 | 9.52k |        NULL ) != 1 ) | 
| 80 | 4.58k |   { | 
| 81 | 4.58k |     goto on_error_libvsgpt_volume; | 
| 82 | 4.58k |   } | 
| 83 | 4.94k |   if( libvsgpt_volume_get_number_of_partitions( | 
| 84 | 4.94k |        volume, | 
| 85 | 4.94k |        &number_of_partitions, | 
| 86 | 4.94k |        NULL ) != 1 ) | 
| 87 | 0 |   { | 
| 88 | 0 |     goto on_error_libvsgpt_volume; | 
| 89 | 0 |   } | 
| 90 | 4.94k |   if( number_of_partitions > 0 ) | 
| 91 | 2.97k |   { | 
| 92 | 2.97k |     if( libvsgpt_volume_get_partition_by_index( | 
| 93 | 2.97k |          volume, | 
| 94 | 2.97k |          0, | 
| 95 | 2.97k |          &partition, | 
| 96 | 2.97k |          NULL ) == 1 ) | 
| 97 | 672 |     { | 
| 98 | 672 |       libvsgpt_partition_free( | 
| 99 | 672 |        &partition, | 
| 100 | 672 |        NULL ); | 
| 101 | 672 |     } | 
| 102 | 2.97k |   } | 
| 103 | 4.94k |   libvsgpt_volume_close( | 
| 104 | 4.94k |    volume, | 
| 105 | 4.94k |    NULL ); | 
| 106 |  |  | 
| 107 | 9.52k | on_error_libvsgpt_volume: | 
| 108 | 9.52k |   libvsgpt_volume_free( | 
| 109 | 9.52k |    &volume, | 
| 110 | 9.52k |    NULL ); | 
| 111 |  |  | 
| 112 | 9.52k | on_error_libbfio: | 
| 113 | 9.52k |   libbfio_handle_free( | 
| 114 | 9.52k |    &file_io_handle, | 
| 115 | 9.52k |    NULL ); | 
| 116 |  |  | 
| 117 | 9.52k |   return( 0 ); | 
| 118 | 9.52k | } | 
| 119 |  |  | 
| 120 |  | } /* extern "C" */ | 
| 121 |  |  |