From 4c1806c5b0c8987491640b31857a686326bca12a Mon Sep 17 00:00:00 2001 From: Christian Hoene Date: Sun, 24 Jan 2021 15:50:59 +0100 Subject: [PATCH] Addressed coverity issues and some reformatting --- CMakeLists.txt | 2 +- src/hdf/btree.c | 2 +- src/hdf/dataobject.c | 7 +++++++ src/hdf/fractalhead.c | 2 +- src/hdf/gcol.c | 4 ++++ src/hrtf/check.c | 3 ++- src/hrtf/tools.c | 3 ++- src/tests/easy.c | 4 ++-- 8 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 94cff52..4c28ae4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.8) +cmake_minimum_required(VERSION 2.8.12) project(libmysofa C CXX) include(CheckCCompilerFlag) diff --git a/src/hdf/btree.c b/src/hdf/btree.c index 561ca9b..a378370 100644 --- a/src/hdf/btree.c +++ b/src/hdf/btree.c @@ -266,7 +266,7 @@ int treeRead(struct READER *reader, struct DATAOBJECT *data) { mylog("elements %d size %d\n", elements, size); - if (elements <=0 || size <=0 || elements >= 0x100000 || size > 0x10) + if (elements <= 0 || size <= 0 || elements >= 0x100000 || size > 0x10) return MYSOFA_INVALID_FORMAT; // LCOV_EXCL_LINE if (!(output = malloc(elements * size))) { return MYSOFA_NO_MEMORY; // LCOV_EXCL_LINE diff --git a/src/hdf/dataobject.c b/src/hdf/dataobject.c index 6832b50..b606057 100644 --- a/src/hdf/dataobject.c +++ b/src/hdf/dataobject.c @@ -68,6 +68,10 @@ static int readOHDRHeaderMessageDataspace1(struct READER *reader, ds->dimension_size[i] = readValue(reader, reader->superblock.size_of_lengths); mylog(" dimension %d %" PRIu64 "\n", i, ds->dimension_size[i]); + if (ds->dimension_size[i] > 1000000) { + mylog("dimension_size is too large\n"); // LCOV_EXCL_LINE + return MYSOFA_INVALID_FORMAT; // LCOV_EXCL_LINE + } } else readValue(reader, reader->superblock.size_of_lengths); } @@ -836,6 +840,9 @@ int readDataDim(struct READER *reader, struct DATAOBJECT *da, struct DATATYPE *dt, struct DATASPACE *ds, int dim) { int i, err; + if (dim >= sizeof(ds->dimension_size) / sizeof(ds->dimension_size[0])) + return MYSOFA_UNSUPPORTED_FORMAT; // LCOV_EXCL_LINE + for (i = 0; i < ds->dimension_size[dim]; i++) { if (dim + 1 < ds->dimensionality) { if (!!(err = readDataDim(reader, da, dt, ds, dim + 1))) { diff --git a/src/hdf/fractalhead.c b/src/hdf/fractalhead.c index c8dd8f3..851c994 100644 --- a/src/hdf/fractalhead.c +++ b/src/hdf/fractalhead.c @@ -196,7 +196,7 @@ static int directblockRead(struct READER *reader, struct DATAOBJECT *dataobject, /* TODO: Get definition of this field */ unknown3 = readValue(reader, 2); - if(unknown3 != 0x0000) + if (unknown3 != 0x0000) return MYSOFA_INVALID_FORMAT; len = fgetc(reader->fhd); diff --git a/src/hdf/gcol.c b/src/hdf/gcol.c index db13f4c..3980929 100644 --- a/src/hdf/gcol.c +++ b/src/hdf/gcol.c @@ -40,6 +40,10 @@ static int readGCOL(struct READER *reader) { address = ftell(reader->fhd); end = address; collection_size = readValue(reader, reader->superblock.size_of_lengths); + if (collection_size > 0x400000000) { + mylog("collection_size is too large\n"); + return MYSOFA_INVALID_FORMAT; + } end += collection_size - 8; while (ftell(reader->fhd) <= end - 8 - reader->superblock.size_of_lengths) { diff --git a/src/hrtf/check.c b/src/hrtf/check.c index 3eed1b2..624ac13 100644 --- a/src/hrtf/check.c +++ b/src/hrtf/check.c @@ -68,7 +68,8 @@ MYSOFA_EXPORT int mysofa_check(struct MYSOFA_HRTF *hrtf) { ============================================================================== */ - if (hrtf->C != 3 || hrtf->I != 1 || hrtf->E != 1 || hrtf->R != 2 || hrtf->M == 0) + if (hrtf->C != 3 || hrtf->I != 1 || hrtf->E != 1 || hrtf->R != 2 || + hrtf->M == 0) return MYSOFA_INVALID_DIMENSIONS; // LCOV_EXCL_LINE /* verify format */ diff --git a/src/hrtf/tools.c b/src/hrtf/tools.c index e91f62c..48f1c3d 100644 --- a/src/hrtf/tools.c +++ b/src/hrtf/tools.c @@ -25,7 +25,8 @@ char *mysofa_strdup(const char *str) { int verifyAttribute(struct MYSOFA_ATTRIBUTE *attr, char *name, char *value) { while (attr) { - if (attr->name && !strcmp(name, attr->name) && attr->value && !strcmp(value, attr->value)) + if (attr->name && !strcmp(name, attr->name) && attr->value && + !strcmp(value, attr->value)) return 1; attr = attr->next; } diff --git a/src/tests/easy.c b/src/tests/easy.c index 032a493..19f8451 100644 --- a/src/tests/easy.c +++ b/src/tests/easy.c @@ -19,8 +19,8 @@ void test_easy_open() { int err = 0; int filterlength; - easy = mysofa_open("tests/MIT_KEMAR_normal_pinna.old.sofa", 8000., &filterlength, - &err); + easy = mysofa_open("tests/MIT_KEMAR_normal_pinna.old.sofa", 8000., + &filterlength, &err); if (!easy) { CU_FAIL_FATAL("Error reading file."); return;