From a7a846a655afe4050c2707ae70512c7a1ecf9677 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Fri, 10 Oct 2025 22:49:52 +0200 Subject: [PATCH] fix allocation null pointer check --- libs/spandisc/src/filehandler.c | 10 ++++++++++ libs/spandisc/src/splitter.c | 6 ++++++ libs/spandisc/tests/splitter-test.c | 6 +++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/libs/spandisc/src/filehandler.c b/libs/spandisc/src/filehandler.c index 6841129..dc98a61 100644 --- a/libs/spandisc/src/filehandler.c +++ b/libs/spandisc/src/filehandler.c @@ -32,6 +32,10 @@ void read_file_stat(char* path, medium_t* srcdata) void fill_array_from_file(FILE* in, medium_t* srcdata) { char* path = malloc(sizeof(char) * MAX_PATH_LENGTH); + + if (path == NULL) + return; + memset(path, 0, MAX_PATH_LENGTH); while (fgets(path, MAX_PATH_LENGTH, in) != NULL) { @@ -46,6 +50,9 @@ void read_input_file(const char* input, medium_t* srcdata) { FILE* in = fopen(input, "r"); + if (in == NULL) + return; + fill_array_from_file(in, srcdata); fclose(in); @@ -55,6 +62,9 @@ void write_output_file(const char* output, medium_t* disc) { FILE* out = fopen(output, "w"); + if (out == NULL) + return; + for (uint32_t i = 0; i < disc->length; i++) { fprintf(out, "%s\n", disc->filearray[i]->name); } diff --git a/libs/spandisc/src/splitter.c b/libs/spandisc/src/splitter.c index c520e31..de5c730 100644 --- a/libs/spandisc/src/splitter.c +++ b/libs/spandisc/src/splitter.c @@ -12,6 +12,9 @@ collection_t* init_collection() { collection_t* col = malloc(sizeof(collection_t)); + if (col == NULL) + return NULL; + col->length = 0; col->disc = NULL; @@ -22,6 +25,9 @@ medium_t* init_medium() { medium_t* medium = malloc(sizeof(medium_t)); + if (medium == NULL) + return NULL; + medium->nr = 0; medium->freespace = 0; medium->length = 0; diff --git a/libs/spandisc/tests/splitter-test.c b/libs/spandisc/tests/splitter-test.c index 88f00a2..fe9826e 100644 --- a/libs/spandisc/tests/splitter-test.c +++ b/libs/spandisc/tests/splitter-test.c @@ -18,7 +18,11 @@ char* strdup(const char* str) { uint64_t length = strlen(str) + 1; char* new_str = malloc(sizeof(char) * length); - memcpy(new_str, str, length); + + if (new_str == NULL) + return NULL; + + new_str = strncpy(new_str, str, length); return new_str; } -- 2.47.3