Coverage Report

Created: 2026-11-12 08:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/fuzzers/gdal_vector_translate_fuzzer.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  GDAL
4
 * Purpose:  Fuzzer
5
 * Author:   Even Rouault, even.rouault at spatialys.com
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2018, Even Rouault <even.rouault at spatialys.com>
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a
11
 * copy of this software and associated documentation files (the "Software"),
12
 * to deal in the Software without restriction, including without limitation
13
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14
 * and/or sell copies of the Software, and to permit persons to whom the
15
 * Software is furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included
18
 * in all copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26
 * DEALINGS IN THE SOFTWARE.
27
 ****************************************************************************/
28
29
#include "gdal.h"
30
#include "cpl_conv.h"
31
#include "cpl_string.h"
32
#include "cpl_error.h"
33
#include "cpl_vsi.h"
34
#include "gdal_utils.h"
35
36
#ifndef REGISTER_FUNC
37
82
#define REGISTER_FUNC OGRRegisterAll
38
#endif
39
40
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv);
41
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len);
42
43
int LLVMFuzzerInitialize(int * /*argc*/, char ***argv)
44
82
{
45
82
    const char *exe_path = (*argv)[0];
46
82
    if (CPLGetConfigOption("GDAL_DATA", nullptr) == nullptr)
47
82
    {
48
82
        CPLSetConfigOption("GDAL_DATA", CPLGetPathSafe(exe_path).c_str());
49
82
    }
50
82
    CPLSetConfigOption("CPL_TMPDIR", "/tmp");
51
82
    CPLSetConfigOption("DISABLE_OPEN_REAL_NETCDF_FILES", "YES");
52
82
    CPLSetConfigOption("GDAL_HTTP_TIMEOUT", "1");
53
82
    CPLSetConfigOption("GDAL_HTTP_CONNECTTIMEOUT", "1");
54
#ifdef OGR_SKIP
55
    CPLSetConfigOption("OGR_SKIP", OGR_SKIP);
56
#endif
57
82
    REGISTER_FUNC();
58
82
    return 0;
59
82
}
60
61
int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
62
9.53k
{
63
9.53k
    VSILFILE *fp = VSIFileFromMemBuffer(
64
9.53k
        "/vsimem/test.tar",
65
9.53k
        reinterpret_cast<GByte *>(const_cast<uint8_t *>(buf)), len, FALSE);
66
9.53k
    VSIFCloseL(fp);
67
68
9.53k
    CPLPushErrorHandler(CPLQuietErrorHandler);
69
70
9.53k
    char **papszArgv = nullptr;
71
72
9.53k
    CPLString osOutFilename("out");
73
9.53k
    fp = VSIFOpenL("/vsitar//vsimem/test.tar/cmd.txt", "rb");
74
9.53k
    if (fp != nullptr)
75
9.13k
    {
76
9.13k
        const char *pszLine = nullptr;
77
9.13k
        if ((pszLine = CPLReadLineL(fp)) != nullptr)
78
9.13k
        {
79
9.13k
            osOutFilename = pszLine;
80
9.13k
            osOutFilename = osOutFilename.replaceAll('/', '_');
81
9.13k
        }
82
9.13k
        int nCandidateLayerNames = 0;
83
37.5k
        while ((pszLine = CPLReadLineL(fp)) != nullptr)
84
28.7k
        {
85
28.7k
            if (pszLine[0] != '-')
86
13.6k
            {
87
13.6k
                nCandidateLayerNames++;
88
13.6k
                if (nCandidateLayerNames == 10)
89
386
                    break;
90
13.6k
            }
91
28.4k
            papszArgv = CSLAddString(papszArgv, pszLine);
92
28.4k
        }
93
9.13k
        VSIFCloseL(fp);
94
9.13k
    }
95
96
9.53k
    char **papszDrivers = CSLAddString(nullptr, "CSV");
97
9.53k
    GDALDatasetH hSrcDS =
98
9.53k
        GDALOpenEx("/vsitar//vsimem/test.tar/in", GDAL_OF_VECTOR, papszDrivers,
99
9.53k
                   nullptr, nullptr);
100
9.53k
    CSLDestroy(papszDrivers);
101
102
9.53k
    if (papszArgv != nullptr && hSrcDS != nullptr)
103
9.09k
    {
104
9.09k
        const int nLayerCount = GDALDatasetGetLayerCount(hSrcDS);
105
70.4k
        for (int i = 0; i < nLayerCount; i++)
106
62.0k
        {
107
62.0k
            OGRLayerH hLayer = GDALDatasetGetLayer(hSrcDS, i);
108
62.0k
            if (hLayer)
109
62.0k
            {
110
62.0k
                int nFieldCount =
111
62.0k
                    OGR_FD_GetFieldCount(OGR_L_GetLayerDefn(hLayer));
112
62.0k
                if (nFieldCount > 100)
113
711
                {
114
711
                    papszArgv = CSLAddString(papszArgv, "-limit");
115
711
                    papszArgv = CSLAddString(papszArgv, "100");
116
711
                    break;
117
711
                }
118
62.0k
            }
119
62.0k
        }
120
121
9.09k
        GDALVectorTranslateOptions *psOptions =
122
9.09k
            GDALVectorTranslateOptionsNew(papszArgv, nullptr);
123
9.09k
        if (psOptions)
124
8.95k
        {
125
8.95k
            CPLString osFullOutFilename("/vsimem/" + osOutFilename);
126
8.95k
            GDALDatasetH hOutDS =
127
8.95k
                GDALVectorTranslate(osFullOutFilename.c_str(), nullptr, 1,
128
8.95k
                                    &hSrcDS, psOptions, nullptr);
129
8.95k
            if (hOutDS)
130
6.96k
            {
131
6.96k
                GDALDriverH hOutDrv = GDALGetDatasetDriver(hOutDS);
132
6.96k
                GDALClose(hOutDS);
133
134
                // Try re-opening generated file
135
6.96k
                GDALClose(GDALOpenEx(osFullOutFilename, GDAL_OF_VECTOR, nullptr,
136
6.96k
                                     nullptr, nullptr));
137
138
6.96k
                if (hOutDrv)
139
6.96k
                    GDALDeleteDataset(hOutDrv, osFullOutFilename);
140
6.96k
            }
141
8.95k
            GDALVectorTranslateOptionsFree(psOptions);
142
8.95k
        }
143
9.09k
    }
144
9.53k
    CSLDestroy(papszArgv);
145
9.53k
    GDALClose(hSrcDS);
146
147
9.53k
    VSIRmdirRecursive("/vsimem/");
148
149
9.53k
    CPLPopErrorHandler();
150
151
9.53k
    return 0;
152
9.53k
}