Skip to content

Commit

Permalink
Apply .clang-format to fuzzers
Browse files Browse the repository at this point in the history
  • Loading branch information
Even Rouault reformatting bot authored and rouault committed Dec 17, 2022
1 parent 28fe215 commit 464e1db
Show file tree
Hide file tree
Showing 17 changed files with 367 additions and 325 deletions.
34 changes: 19 additions & 15 deletions fuzzers/filename_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
#include "cpl_vsi.h"
#include "gdal_priv.h"

extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv);
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv);
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len);

int LLVMFuzzerInitialize(int* /*argc*/, char*** argv)
int LLVMFuzzerInitialize(int * /*argc*/, char ***argv)
{
const char* exe_path = (*argv)[0];
if( CPLGetConfigOption("GDAL_DATA", nullptr) == nullptr )
const char *exe_path = (*argv)[0];
if (CPLGetConfigOption("GDAL_DATA", nullptr) == nullptr)
{
CPLSetConfigOption("GDAL_DATA", CPLGetPath(exe_path));
}
Expand All @@ -53,7 +53,7 @@ int LLVMFuzzerInitialize(int* /*argc*/, char*** argv)
CPLSetConfigOption("GDAL_WMS_ABORT_CURL_REQUEST", "YES");
CPLSetConfigOption("GDAL_HTTP_TIMEOUT", "1");
CPLSetConfigOption("GDAL_HTTP_CONNECTTIMEOUT", "1");
CPLSetConfigOption("GDAL_CACHEMAX", "1000"); // Limit to 1 GB
CPLSetConfigOption("GDAL_CACHEMAX", "1000"); // Limit to 1 GB
#ifdef GTIFF_USE_MMAP
CPLSetConfigOption("GTIFF_USE_MMAP", "YES");
#endif
Expand All @@ -68,36 +68,40 @@ int LLVMFuzzerInitialize(int* /*argc*/, char*** argv)

int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
{
VSIFCloseL(VSIFileFromMemBuffer( "/vsimem/input.tar",
reinterpret_cast<GByte*>(const_cast<uint8_t*>(buf)), len, FALSE ));
VSIFCloseL(VSIFileFromMemBuffer(
"/vsimem/input.tar",
reinterpret_cast<GByte *>(const_cast<uint8_t *>(buf)), len, FALSE));

CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler);

GByte* paby = nullptr;
GByte *paby = nullptr;
vsi_l_offset nSize = 0;
if( !VSIIngestFile(nullptr, "/vsitar//vsimem/input.tar/filename", &paby, &nSize, -1) )
if (!VSIIngestFile(nullptr, "/vsitar//vsimem/input.tar/filename", &paby,
&nSize, -1))
{
VSIUnlink("/vsimem/input.tar");
return 0;
}
const std::string osFilename(reinterpret_cast<const char*>(paby));
const std::string osFilename(reinterpret_cast<const char *>(paby));
VSIFree(paby);

paby = nullptr;
nSize = 0;
int ret = VSIIngestFile(nullptr, "/vsitar//vsimem/input.tar/content", &paby, &nSize, -1);
int ret = VSIIngestFile(nullptr, "/vsitar//vsimem/input.tar/content", &paby,
&nSize, -1);
VSIUnlink("/vsimem/input.tar");
if( !ret )
if (!ret)
{
return 0;
}

const std::string osRealFilename("/vsimem/" + osFilename);
VSIFCloseL(VSIFileFromMemBuffer(osRealFilename.c_str(), paby, static_cast<size_t>(nSize), TRUE ));
VSIFCloseL(VSIFileFromMemBuffer(osRealFilename.c_str(), paby,
static_cast<size_t>(nSize), TRUE));

delete GDALDataset::Open( osRealFilename.c_str() );
delete GDALDataset::Open(osRealFilename.c_str());

VSIUnlink( osRealFilename.c_str() );
VSIUnlink(osRealFilename.c_str());

return 0;
}
40 changes: 21 additions & 19 deletions fuzzers/fuzzingengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,45 @@

extern "C"
{
int LLVMFuzzerTestOneInput(const void *buf, size_t len);
int LLVMFuzzerInitialize(int* argc, char*** argv);
int LLVMFuzzerTestOneInput(const void *buf, size_t len);
int LLVMFuzzerInitialize(int *argc, char ***argv);
}

template<class T> static void CPL_IGNORE_RET_VAL(T) {}
template <class T> static void CPL_IGNORE_RET_VAL(T)
{
}

static void Usage(int, char* argv[])
static void Usage(int, char *argv[])
{
fprintf(stderr, "%s [--help] [-repeat N] filename.\n", argv[0]);
exit(1);
}

int main(int argc, char* argv[])
int main(int argc, char *argv[])
{
LLVMFuzzerInitialize(&argc, &argv);

int nRet = 0;
void* buf = nullptr;
void *buf = nullptr;
int nLen = 0;
int nLoops = 1;
const char* pszFilename = nullptr;
for(int i = 1; i < argc; i++ )
const char *pszFilename = nullptr;
for (int i = 1; i < argc; i++)
{
if( i + 1 < argc && strcmp(argv[i], "-repeat") == 0 )
if (i + 1 < argc && strcmp(argv[i], "-repeat") == 0)
{
nLoops = atoi(argv[i+1]);
nLoops = atoi(argv[i + 1]);
i++;
}
else if( strcmp(argv[i], "-dummy") == 0 )
else if (strcmp(argv[i], "-dummy") == 0)
{
return LLVMFuzzerTestOneInput(" ", 1);
}
else if( strcmp(argv[i], "--help") == 0 )
else if (strcmp(argv[i], "--help") == 0)
{
Usage(argc, argv);
}
else if( argv[i][0] == '-' )
else if (argv[i][0] == '-')
{
fprintf(stderr, "Unrecognized option: %s", argv[i]);
Usage(argc, argv);
Expand All @@ -78,13 +80,13 @@ int main(int argc, char* argv[])
pszFilename = argv[i];
}
}
if( pszFilename == nullptr )
if (pszFilename == nullptr)
{
fprintf(stderr, "No filename specified\n");
Usage(argc, argv);
}
FILE* f = fopen(pszFilename, "rb");
if( !f )
FILE *f = fopen(pszFilename, "rb");
if (!f)
{
fprintf(stderr, "%s does not exist.\n", pszFilename);
exit(1);
Expand All @@ -93,18 +95,18 @@ int main(int argc, char* argv[])
nLen = (int)ftell(f);
fseek(f, 0, SEEK_SET);
buf = malloc(nLen);
if( !buf )
if (!buf)
{
fprintf(stderr, "malloc failed.\n");
fclose(f);
exit(1);
}
CPL_IGNORE_RET_VAL(fread(buf, nLen, 1, f));
fclose(f);
for( int i = 0; i < nLoops; i++ )
for (int i = 0; i < nLoops; i++)
{
nRet = LLVMFuzzerTestOneInput(buf, nLen);
if( nRet != 0 )
if (nRet != 0)
break;
}
free(buf);
Expand Down
Loading

0 comments on commit 464e1db

Please sign in to comment.