From 641ff4e4e2a7de0db388cf7df73ea3e47e5583dd Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 20 Sep 2022 17:24:17 +0200 Subject: [PATCH] fix calloc/malloc --- Image/Thresholding.h | 2 +- Stdlib/HashTable.h | 16 +++++++++------- Utils/ArraySort.h | 4 ++-- Utils/ArrayUtils.h | 2 +- Utils/FileUtils.h | 4 ++-- Utils/Rng/StringUtils.h | 4 ++-- Utils/StringUtils.h | 16 ++++++++-------- Utils/TestUtils.h | 8 ++++---- 8 files changed, 29 insertions(+), 27 deletions(-) diff --git a/Image/Thresholding.h b/Image/Thresholding.h index 99b02c5..0b9e6f5 100644 --- a/Image/Thresholding.h +++ b/Image/Thresholding.h @@ -27,7 +27,7 @@ namespace Image { cv::Size dim = in.size(); cv::Mat out(dim, in.type()); - float *intImg = malloc(dim.width * dim.height * sizeof(float)); + float *intImg = (float *) malloc(dim.width * dim.height * sizeof(float)); float sum; cv::Vec3b bgr; diff --git a/Stdlib/HashTable.h b/Stdlib/HashTable.h index 85f3bd5..0007436 100644 --- a/Stdlib/HashTable.h +++ b/Stdlib/HashTable.h @@ -50,7 +50,7 @@ namespace Stdlib { ht *create_table(void) { - ht *table = malloc(sizeof(ht)); + ht *table = (ht *) malloc(sizeof(ht)); if (table == NULL) { return NULL; } @@ -58,7 +58,7 @@ namespace Stdlib { table->size = 0; table->max = 16; - table->entries = calloc(table->max, sizeof(entry)); + table->entries = (entry *) calloc(table->max, sizeof(entry)); if (table->entries == NULL) { free(table); return NULL; @@ -89,7 +89,7 @@ namespace Stdlib { const char *_set_entry(entry *entries, size_t max, const char *key, void *value, size_t *size) { unsigned long long hash = hash_key(key); - size_t index = (size_t)(hash & (unsigned long long)(max - 1)); + size_t index = (size_t)(hash & (unsigned long long)(max - 1)); while (entries[index].key != NULL) { if (strcmp(key, entries[index].key) == 0) { @@ -107,7 +107,7 @@ namespace Stdlib { if (size != NULL) { key = strdup(key); if (key == NULL) { - return NULL + return NULL; } ++(*size); @@ -126,7 +126,7 @@ namespace Stdlib { return false; } - entry *new_entries = calloc(new_max, sizeof(entry)); + entry *new_entries = (entry *) calloc(new_max, sizeof(entry)); if (new_entries == NULL) { return false; } @@ -140,7 +140,7 @@ namespace Stdlib { free(table->entries); table->entries = new_entries; - table->max = new_max; + table->max = new_max; return true; } @@ -178,7 +178,7 @@ namespace Stdlib { if (table->entries[i].key != NULL) { entry tmp = table->entries[i]; - it->key = tmp.key; + it->key = tmp.key; it->value = tmp.value; return true; @@ -196,6 +196,8 @@ namespace Stdlib { free(table->entries); free(table); + + table = NULL; } }; } diff --git a/Utils/ArraySort.h b/Utils/ArraySort.h index bee8cf7..0c35bfd 100644 --- a/Utils/ArraySort.h +++ b/Utils/ArraySort.h @@ -46,10 +46,10 @@ namespace Utils { } inline - void reverse_char(char *arr, int size) + void reverse_char(char **arr, int size) { for (int low = 0, high = size - 1; low < high; ++low, --high) { - char tmp = arr[low]; + char* tmp = arr[low]; arr[low] = arr[high]; arr[high] = tmp; } diff --git a/Utils/ArrayUtils.h b/Utils/ArrayUtils.h index ecd8f5f..a6b109a 100644 --- a/Utils/ArrayUtils.h +++ b/Utils/ArrayUtils.h @@ -27,7 +27,7 @@ namespace Utils { for (int i = 0; i < length - 1; ++i) { if (strcmp(id, argv[i]) == 0) { - return argv[i + 1]; + return i + 1 >= length ? NULL : argv[i + 1]; } } diff --git a/Utils/FileUtils.h b/Utils/FileUtils.h index d3494e4..ecb4a00 100644 --- a/Utils/FileUtils.h +++ b/Utils/FileUtils.h @@ -95,14 +95,14 @@ namespace Utils { FILE *fp = fopen(filename, "rb"); if (!fp) { - return NULL; + return file; } fseek(fp, 0, SEEK_END); file.size = ftell(fp); fseek(fp, 0, SEEK_SET); - file.content = malloc(file.size); + file.content = (char *) malloc(file.size); if (!file.content) { fprintf(stderr, "CRITICAL: malloc failed"); } diff --git a/Utils/Rng/StringUtils.h b/Utils/Rng/StringUtils.h index e8112fd..61f3791 100644 --- a/Utils/Rng/StringUtils.h +++ b/Utils/Rng/StringUtils.h @@ -20,12 +20,12 @@ namespace Utils { inline char* generate_string( size_t min = 10, size_t max = 10, - char* charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", int charsetLength = 62 + char *charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", int charsetLength = 62 ) { srand(time(0)); size_t length = (rand() % (max - min + 1)) + min; - char* randomString = malloc(length + 1); + char *randomString = (char *) malloc(length + 1); for (size_t i = 0; i < length; ++i) { randomString[i] = charset[rand() % charsetLength]; diff --git a/Utils/StringUtils.h b/Utils/StringUtils.h index 1e43f74..5c71844 100644 --- a/Utils/StringUtils.h +++ b/Utils/StringUtils.h @@ -40,10 +40,10 @@ namespace Utils { text_diff computeLCSDiff (char **from, int fromSize, char **to, int toSize) { - char **diffValues = malloc(fromSize * toSize * sizeof(char *)); - int *diffMasks = calloc(fromSize * toSize, sizeof(int)); + char **diffValues = (char **) malloc(fromSize * toSize * sizeof(char *)); + int *diffMasks = (int *) calloc(fromSize * toSize, sizeof(int)); - int *dm = calloc((fromSize + 1) * (toSize + 1), sizeof(int)); + int *dm = (int *) calloc((fromSize + 1) * (toSize + 1), sizeof(int)); if (!diffValues || !diffMasks || !dm) { fprintf(stderr, "CRITICAL: malloc failed"); @@ -77,7 +77,7 @@ namespace Utils { while (i > 0 || j > 0) { if (j > 0 && dm[i * fromSize + (j - 1)] == dm[i * fromSize + j]) { - diffValues[diffIndex] = malloc((strlen(to[j - 1]) + 1) * sizeof(char)); + diffValues[diffIndex] = (char *) malloc((strlen(to[j - 1]) + 1) * sizeof(char)); if (!diffValues[diffIndex]) { fprintf(stderr, "CRITICAL: malloc failed"); } @@ -92,7 +92,7 @@ namespace Utils { } if (i > 0 && dm[(i - 1) * fromSize + j] == dm[i * fromSize + j]) { - diffValues[diffIndex] = malloc((strlen(from[i - 1]) + 1) * sizeof(char)); + diffValues[diffIndex] = (char *) malloc((strlen(from[i - 1]) + 1) * sizeof(char)); if (!diffValues[diffIndex]) { fprintf(stderr, "CRITICAL: malloc failed"); } @@ -106,7 +106,7 @@ namespace Utils { continue } - diffValues[diffIndex] = malloc((strlen(from[i - 1]) + 1) * sizeof(char)); + diffValues[diffIndex] = (char *) malloc((strlen(from[i - 1]) + 1) * sizeof(char)); if (!diffValues[diffIndex]) { fprintf(stderr, "CRITICAL: malloc failed"); } @@ -124,8 +124,8 @@ namespace Utils { free(dm); - diffValues = realloc(diffValues, diffIndex * sizeof(char *)); - diffMasks = realloc(diffMasks, diffIndex * sizeof(int)); + diffValues = (char **) realloc(diffValues, diffIndex * sizeof(char *)); + diffMasks = (int *) realloc(diffMasks, diffIndex * sizeof(int)); if (!diffValues || !diffMasks) { fprintf(stderr, "CRITICAL: malloc failed"); diff --git a/Utils/TestUtils.h b/Utils/TestUtils.h index 5a24626..1848fe0 100644 --- a/Utils/TestUtils.h +++ b/Utils/TestUtils.h @@ -15,22 +15,22 @@ #ifdef _MSC_VER #define ASSERT_EQUALS(a, b, t1, t2) (\ - if (a == b) { \ + if ((a) == (b)) { \ printf("."); \ } else { \ printf("[F]"); \ printf("\n\n%s - %i: ", __FILE__, __LINE__); \ - printf(t1, a); printf(" != "); printf(t2, b); printf("\n"); \ + printf((t1), (a)); printf(" != "); printf((t2), (b)); printf("\n"); \ return 0; } \ ) #define ASSERT_EQUALS_WITH_DELTA(a, b, delta, t1, t2) (\ - if (oms_abs(a - b) <= _delta) { \ + if (oms_abs((a) - (b)) <= (delta)) { \ printf("."); \ } else { \ printf("[F]"); \ printf("\n\n%s - %i: ", __FILE__, __LINE__); \ - printf(t1, a); printf(" != "); printf(t2, b); printf("\n"); \ + printf((t1), (a)); printf(" != "); printf((t2), (b)); printf("\n"); \ return 0; } \ ) #else