Skip to content

Commit

Permalink
Run pre-commit to reformat .c, .h, .cpp and .py files
Browse files Browse the repository at this point in the history
  • Loading branch information
Even Rouault reformatting bot authored and rouault committed Sep 28, 2023
1 parent 26a8a26 commit d3c6274
Show file tree
Hide file tree
Showing 246 changed files with 79,612 additions and 71,698 deletions.
16 changes: 8 additions & 8 deletions fuzzers/mapfuzzer.c
Expand Up @@ -21,24 +21,24 @@ limitations under the License.
#define kMaxInputLength 10240

extern int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
extern int LLVMFuzzerInitialize(int* argc, char*** argv);
extern int LLVMFuzzerInitialize(int *argc, char ***argv);

int LLVMFuzzerInitialize(int* argc, char*** argv)
{
(void)argc;
(void)argv;
return 0;
int LLVMFuzzerInitialize(int *argc, char ***argv) {
(void)argc;
(void)argv;
return 0;
}

extern int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

if (Size < kMinInputLength || Size > kMaxInputLength){
if (Size < kMinInputLength || Size > kMaxInputLength) {
return 1;
}

// .map extension is required otherwise default pattern validation of
// msLoadMap() would reject it
char* filename = msStrdup(CPLSPrintf("%s.map", CPLGenerateTempFilename(NULL)));
char *filename =
msStrdup(CPLSPrintf("%s.map", CPLGenerateTempFilename(NULL)));
FILE *fp = fopen(filename, "wb");
if (!fp) {
msFree(filename);
Expand Down
146 changes: 64 additions & 82 deletions fuzzers/reproducer_main.cpp
Expand Up @@ -31,95 +31,77 @@
#include <stdio.h>
#include <string.h>

extern "C"
{
extern "C" {
int LLVMFuzzerTestOneInput(const void *buf, size_t len);
int LLVMFuzzerInitialize(int* argc, char*** argv);
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[])
{
fprintf(stderr, "%s [--help] [-repeat N] filename.\n", argv[0]);
exit(1);
static void Usage(int, char *argv[]) {
fprintf(stderr, "%s [--help] [-repeat N] filename.\n", argv[0]);
exit(1);
}

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

int nRet = 0;
void* buf = nullptr;
size_t nLen = 0;
int nLoops = 1;
const char* pszFilename = nullptr;
for(int i = 1; i < argc; i++ )
{
if( i + 1 < argc && strcmp(argv[i], "-repeat") == 0 )
{
nLoops = atoi(argv[i+1]);
// Limit to INT_MAX - 1 :-)
// to make fun of coverity scan that complains about tainted data
// being used as a loop boundary.
if( nLoops > INT_MAX - 1 )
nLoops = INT_MAX - 1;
i++;
}
else if( strcmp(argv[i], "-dummy") == 0 )
{
return LLVMFuzzerTestOneInput(" ", 1);
}
else if( strcmp(argv[i], "--help") == 0 )
{
Usage(argc, argv);
}
else if( argv[i][0] == '-' )
{
fprintf(stderr, "Unrecognized option: %s", argv[i]);
Usage(argc, argv);
}
else
{
pszFilename = argv[i];
}
}
if( pszFilename == nullptr )
{
fprintf(stderr, "No filename specified\n");
Usage(argc, argv);
}
FILE* f = fopen(pszFilename, "rb");
if( !f )
{
fprintf(stderr, "%s does not exist.\n", pszFilename);
exit(1);
}
fseek(f, 0, SEEK_END);
long pos = ftell(f);
if( pos < 0 )
{
fprintf(stderr, "ftell() failed.\n");
fclose(f);
exit(1);
}
nLen = (size_t)pos;
fseek(f, 0, SEEK_SET);
buf = malloc(nLen);
if( !buf )
{
fprintf(stderr, "malloc failed.\n");
fclose(f);
exit(1);
int nRet = 0;
void *buf = nullptr;
size_t nLen = 0;
int nLoops = 1;
const char *pszFilename = nullptr;
for (int i = 1; i < argc; i++) {
if (i + 1 < argc && strcmp(argv[i], "-repeat") == 0) {
nLoops = atoi(argv[i + 1]);
// Limit to INT_MAX - 1 :-)
// to make fun of coverity scan that complains about tainted data
// being used as a loop boundary.
if (nLoops > INT_MAX - 1)
nLoops = INT_MAX - 1;
i++;
} else if (strcmp(argv[i], "-dummy") == 0) {
return LLVMFuzzerTestOneInput(" ", 1);
} else if (strcmp(argv[i], "--help") == 0) {
Usage(argc, argv);
} else if (argv[i][0] == '-') {
fprintf(stderr, "Unrecognized option: %s", argv[i]);
Usage(argc, argv);
} else {
pszFilename = argv[i];
}
CPL_IGNORE_RET_VAL(fread(buf, nLen, 1, f));
}
if (pszFilename == nullptr) {
fprintf(stderr, "No filename specified\n");
Usage(argc, argv);
}
FILE *f = fopen(pszFilename, "rb");
if (!f) {
fprintf(stderr, "%s does not exist.\n", pszFilename);
exit(1);
}
fseek(f, 0, SEEK_END);
long pos = ftell(f);
if (pos < 0) {
fprintf(stderr, "ftell() failed.\n");
fclose(f);
for( int i = 0; i < nLoops; i++ )
{
nRet = LLVMFuzzerTestOneInput(buf, nLen);
if( nRet != 0 )
break;
}
free(buf);
return nRet;
exit(1);
}
nLen = (size_t)pos;
fseek(f, 0, SEEK_SET);
buf = malloc(nLen);
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++) {
nRet = LLVMFuzzerTestOneInput(buf, nLen);
if (nRet != 0)
break;
}
free(buf);
return nRet;
}
79 changes: 37 additions & 42 deletions fuzzers/shapefuzzer.c
Expand Up @@ -8,32 +8,30 @@
#include <stdbool.h>

extern int LLVMFuzzerTestOneInput(GByte *data, size_t size);
extern int LLVMFuzzerInitialize(int* argc, char*** argv);

static
void *msMemmem(const void *haystack, size_t haystack_len,
const void * const needle, const size_t needle_len)
{
if (haystack_len == 0) return NULL;
if (needle_len == 0) return NULL;

for (const char *h = (const char*)haystack;
haystack_len >= needle_len;
++h, --haystack_len) {
if (!memcmp(h, needle, needle_len)) {
return (void*)h;
}
}
extern int LLVMFuzzerInitialize(int *argc, char ***argv);

static void *msMemmem(const void *haystack, size_t haystack_len,
const void *const needle, const size_t needle_len) {
if (haystack_len == 0)
return NULL;
if (needle_len == 0)
return NULL;

for (const char *h = (const char *)haystack; haystack_len >= needle_len;
++h, --haystack_len) {
if (!memcmp(h, needle, needle_len)) {
return (void *)h;
}
}
return NULL;
}

static VSILFILE *
SegmentFile(const char *filename, GByte **data_p, size_t *size_p)
{
static VSILFILE *SegmentFile(const char *filename, GByte **data_p,
size_t *size_p) {
GByte *data = *data_p;
size_t size = *size_p;

GByte *separator = (GByte*)msMemmem(data, size, "deadbeef", 8);
GByte *separator = (GByte *)msMemmem(data, size, "deadbeef", 8);
if (separator != NULL) {
size = separator - data;
*data_p = separator + 8;
Expand All @@ -45,20 +43,18 @@ SegmentFile(const char *filename, GByte **data_p, size_t *size_p)
return VSIFileFromMemBuffer(filename, data, size, false);
}

int LLVMFuzzerInitialize(int* argc, char*** argv)
{
(void)argc;
(void)argv;
return 0;
int LLVMFuzzerInitialize(int *argc, char ***argv) {
(void)argc;
(void)argv;
return 0;
}

int
LLVMFuzzerTestOneInput(GByte *data, size_t size)
{
int LLVMFuzzerTestOneInput(GByte *data, size_t size) {
/* this fuzzer expects three files concatenated, separated by the
string "deadbeef"; you can generate such a file by typing:
{ cat foo.shp; echo -n "deadbeef"; cat foo.shx; echo -n "deadbeef"; cat foo.dbf; } >/tmp/corpus/start
{ cat foo.shp; echo -n "deadbeef"; cat foo.shx; echo -n "deadbeef"; cat
foo.dbf; } >/tmp/corpus/start
then run the fuzzer:
Expand All @@ -71,25 +67,24 @@ LLVMFuzzerTestOneInput(GByte *data, size_t size)

shapefileObj file;
errorObj *ms_error = msGetErrorObj();
if (msShapefileOpenVirtualFile(&file, "/vsimem/foo.shp", shp, shx, dbf, false) == 0) {
if (file.numshapes > 100 * 1000 )
{
VSIStatBufL sStat;
if( VSIStatL("/vsimem/foo.shx", &sStat) == 0 &&
sStat.st_size >= 100 &&
file.numshapes > (int)(sStat.st_size - 100) / 8 )
{
file.numshapes = (int)(sStat.st_size - 100) / 8;
}
if (msShapefileOpenVirtualFile(&file, "/vsimem/foo.shp", shp, shx, dbf,
false) == 0) {
if (file.numshapes > 100 * 1000) {
VSIStatBufL sStat;
if (VSIStatL("/vsimem/foo.shx", &sStat) == 0 && sStat.st_size >= 100 &&
file.numshapes > (int)(sStat.st_size - 100) / 8) {
file.numshapes = (int)(sStat.st_size - 100) / 8;
}
}
for (int i = 0; i < file.numshapes; ++i) {
shapeObj shape;
msInitShape(&shape);
msSHPReadShape(file.hSHP, i, &shape);
msFreeShape(&shape);
// Give up as soon an error is triggered to avoid too long processing time.
if( ms_error->code != MS_NOERR )
break;
// Give up as soon an error is triggered to avoid too long processing
// time.
if (ms_error->code != MS_NOERR)
break;
}

msShapefileClose(&file);
Expand Down
19 changes: 12 additions & 7 deletions msautotest/api/run_test.py
Expand Up @@ -8,17 +8,17 @@
#
###############################################################################
# Copyright (c) 2002, Frank Warmerdam <warmerdam@pobox.com>
#
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Expand All @@ -29,20 +29,25 @@
###############################################################################

import os
import pytest
import sys

sys.path.append( '../pymod' )
import pytest

sys.path.append("../pymod")

import mstestlib

@pytest.mark.parametrize('map,out_file,command', mstestlib.get_pytests(os.path.dirname(os.path.abspath(__file__))))

@pytest.mark.parametrize(
"map,out_file,command",
mstestlib.get_pytests(os.path.dirname(os.path.abspath(__file__))),
)
def test(map, out_file, command, extra_args):
mstestlib.run_pytest(map, out_file, command, extra_args)


###############################################################################
# main()

if __name__ == '__main__':
if __name__ == "__main__":
sys.exit(mstestlib.pytest_main())

0 comments on commit d3c6274

Please sign in to comment.