]> gitweb.hhaalo.de Git - discspan.git/commitdiff
fix allocation null pointer check
authorBastian Dehn <hhaalo@arcor.de>
Fri, 10 Oct 2025 20:49:52 +0000 (22:49 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Fri, 10 Oct 2025 20:49:52 +0000 (22:49 +0200)
libs/spandisc/src/filehandler.c
libs/spandisc/src/splitter.c
libs/spandisc/tests/splitter-test.c

index 68411299f8e648ffd4625d1bea155537e868a42e..dc98a61ac64dedffd654631deb25a0cf481357f9 100644 (file)
@@ -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);
        }
index c520e31fc619e75bd709e3703f9f1683aa26b7c1..de5c7306e1cb2dd66cce8b347fb7ebeed0c8983d 100644 (file)
@@ -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;
index 88f00a2d2e89f07672fa62c7a19115b8c4205d9b..fe9826ed51837bd133c00e95f4f54658a126d57d 100644 (file)
@@ -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;
 }