]> gitweb.hhaalo.de Git - feierabend.git/commitdiff
fix allocation null check
authorBastian Dehn <hhaalo@arcor.de>
Fri, 10 Oct 2025 21:07:29 +0000 (23:07 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Fri, 10 Oct 2025 21:07:29 +0000 (23:07 +0200)
src/feierabend.c
src/time_format.c
src/xml.c
tests/time_format_tests.c

index a6c66574908ea43fd5648eaf70ed36bd17a3afdf..05b7b2f626801be83eb2762e6ef50ca0ca6c3d8b 100644 (file)
@@ -11,6 +11,9 @@ feierabend* _feierabend_init()
 {
        feierabend* fabend = malloc(sizeof(feierabend));
 
+       if (fabend == NULL)
+               return NULL;
+
        fabend->now = malloc(sizeof(time_t));
        fabend->begin = malloc(sizeof(time_t));
        fabend->worktime = malloc(sizeof(time_t));
@@ -102,6 +105,9 @@ void _print_brutto_worktime(feierabend* fabend)
 {
        char* timestr = malloc(MAX_TIME_STR_LENGTH);
 
+       if (timestr == NULL)
+               return;
+
        *fabend->worktime = get_brutto_worktime(*fabend->begin, *fabend->now);
        get_time_str(*fabend->worktime, timestr);
        printf("%-26s%6s\n", "Brutto Arbeitzeit:", timestr);
@@ -113,6 +119,9 @@ void _print_netto_worktime(feierabend* fabend)
 {
        char* timestr = malloc(MAX_TIME_STR_LENGTH);
 
+       if (timestr == NULL)
+               return;
+
        *fabend->worktime = get_current_worktime(*fabend->begin, *fabend->now);
        get_time_str(*fabend->worktime, timestr);
        printf("%-26s%6s\n", "Netto Arbeitzeit:", timestr);
@@ -125,6 +134,9 @@ void _print_current_breaktime(feierabend* fabend)
 {
        char* timestr = malloc(MAX_TIME_STR_LENGTH);
 
+       if (timestr == NULL)
+               return;
+
        *fabend->worktime = get_brutto_worktime(*fabend->begin, *fabend->now);
        *fabend->worktime = get_break_time(*fabend->worktime);
        get_time_str(*fabend->worktime, timestr);
@@ -138,6 +150,9 @@ void _print_time_to_end_worktime(feierabend* fabend)
 {
        char* timestr = malloc(MAX_TIME_STR_LENGTH);
 
+       if (timestr == NULL)
+               return;
+
        *fabend->worktime = get_current_worktime_diff_to_end_eight_hour(*fabend->begin, *fabend->now);
        get_time_str(*fabend->worktime, timestr);
        printf("Arbeitzeit bis %02d:%02d Std: %6s\n",
@@ -153,6 +168,9 @@ void _print_time_to_max_end_worktime(feierabend* fabend)
 {
        char* timestr = malloc(MAX_TIME_STR_LENGTH);
 
+       if (timestr == NULL)
+               return;
+
        *fabend->worktime = get_current_worktime_diff_to_end_ten_hour(*fabend->begin, *fabend->now);
        get_time_str(*fabend->worktime, timestr);
        printf("Arbeitzeit bis 10:00 Std: %6s\n", timestr);
index e7f29c41df8122fd5903099883cc4d639ae0199b..d2987838f527ad9396fb17c41f5bbd6363319f0a 100644 (file)
@@ -44,11 +44,14 @@ void get_time_str_overtime(time_t timediff, char* timestr)
 
 void get_seconds_from_string(const char* timestr, time_t* seconds)
 {
-       char* first_str_pos = NULL;
        char* str = malloc(sizeof(char) * 7);
+
+       if (str == NULL)
+               return;
+
        memset(str, 0, 7);
        strncpy(str, timestr, 7);
-       first_str_pos = str;
+       char* first_str_pos = str;
        int minus = 0;
        
        if (str[0] == '-')
@@ -125,9 +128,12 @@ int get_year_from_str(const char* date)
 {
        int value = 0;
        char* str = malloc(sizeof(char) * 11);
-       char* str_first_pos = NULL;
+
+       if (str == NULL)
+               return 0;
+
        strncpy(str, date, 11);
-       str_first_pos = str;
+       char* str_first_pos = str;
 
        str = strtok(str, "-");
        value = atoi(str);
@@ -141,9 +147,12 @@ int get_month_from_str(const char* date)
 {
        int value = 0;
        char* str = malloc(sizeof(char) * 11);
-       char* str_first_pos = NULL;
+
+       if (str == NULL)
+               return 0;
+
        strncpy(str, date, 11);
-       str_first_pos = str;
+       char* str_first_pos = str;
 
        str = strtok(str, "-");
        str = strtok(NULL, "-");
@@ -158,9 +167,12 @@ int get_day_from_str(const char* date)
 {
        int value = 0;
        char* str = malloc(sizeof(char) * 11);
-       char* str_first_pos = NULL;
+
+       if (str == NULL)
+               return 0;
+
        strncpy(str, date, 11);
-       str_first_pos = str;
+       char* str_first_pos = str;
 
        str = strtok(str, "-");
        str = strtok(NULL, "-");
index 54f865639ac59e1fb625f202cbb4e87fcb908349..2281a15f9d0cad6e32a1ece8abe605ff03fd8b1b 100644 (file)
--- a/src/xml.c
+++ b/src/xml.c
 
 #define MAX_STRING_LENGTH 36
 
-void init_memFile(memFile* mem) {
+memFile* init_memFile() {
+       memFile* mem = malloc(sizeof(memFile));
+
+       if (mem == NULL)
+               return NULL;
+
        mem->data = malloc(sizeof(char*));
        mem->size = malloc(sizeof(size_t));
+
+       return mem;
 }
 
 void free_memFile(memFile* mem) {
@@ -32,6 +39,9 @@ feierabend* feierabend_init()
 {
        feierabend* fabend = malloc(sizeof(feierabend));
 
+       if (fabend == NULL)
+               return NULL;
+
        fabend->now = malloc(sizeof(time_t));
        fabend->begin = malloc(sizeof(time_t));
        fabend->worktime = malloc(sizeof(time_t));
@@ -52,6 +62,10 @@ void feierabend_free(feierabend* fabend)
 void readStdInIntoMemory(memFile* mem)
 {
        char* buf = malloc(sizeof(char));
+
+       if (buf == NULL)
+               return;
+
        FILE* memstream = open_memstream(mem->data, mem->size);
        FILE* file = fdopen(0, "r");
 
@@ -394,11 +408,14 @@ void addEntry(const char* date,
        int end_hour,
        int end_min)
 {
-       memFile* fileContent = malloc(sizeof(memFile));
-       init_memFile(fileContent);
+       memFile* fileContent = init_memFile();
        readStdInIntoMemory(fileContent);
 
        char* saldostr = malloc(sizeof(char) * MAX_STRING_LENGTH);
+
+       if (saldostr == NULL)
+               return;
+
        memset(saldostr, 0, MAX_STRING_LENGTH);
        readLastSaldo(fileContent, saldostr);
 
@@ -433,6 +450,10 @@ void addEntry(const char* date,
        }
 
        time_t* saldo = malloc(sizeof(time_t));
+
+       if (saldo == NULL)
+               return;
+
        get_seconds_from_string(saldostr, saldo);
        writeEntryNode(xmlwriter, date, begin_hour, begin_min, end_hour, end_min, *saldo);
        xmlTextWriterEndElement(xmlwriter);
index 46d081edb85346b3f8949bc06e038bac6269707d..1b1d731c0b6875c94cf2c8ef0cf06d9d47fbc9ac 100644 (file)
@@ -11,6 +11,10 @@ void get_zero_hour_and_fifteen_minutes()
 {
        time_t time = 900;
        char* timestring = malloc(sizeof(char) * 7);
+
+       if (timestring == NULL)
+               return;
+
        memset(timestring, 0, 7);
 
        get_time_str(time, timestring);
@@ -25,6 +29,10 @@ void get_five_hour_fourty_five()
 {
        time_t time = 20700;
        char* timestring = malloc(sizeof(char) * 7);
+
+       if (timestring == NULL)
+               return;
+
        memset(timestring, 0, 7);
 
        get_time_str(time, timestring);
@@ -39,6 +47,10 @@ void get_minus_five_minutes()
 {
        time_t time = -300;
        char* timestring = malloc(sizeof(char) * 7);
+
+       if (timestring == NULL)
+               return;
+
        memset(timestring, 0, 7);
 
        get_time_str(time, timestring);
@@ -53,6 +65,10 @@ void get_zero_hour_and_fifteen_minutes_overtime()
 {
        time_t time = 900;
        char* timestring = malloc(sizeof(char) * 7);
+
+       if (timestring == NULL)
+               return;
+
        memset(timestring, 0, 7);
 
        get_time_str_overtime(time, timestring);
@@ -67,6 +83,10 @@ void get_five_hour_fourty_five_overtime()
 {
        time_t time = 20700;
        char* timestring = malloc(sizeof(char) * 7);
+
+       if (timestring == NULL)
+               return;
+
        memset(timestring, 0, 7);
 
        get_time_str_overtime(time, timestring);
@@ -81,6 +101,10 @@ void get_minus_five_minutes_overtime()
 {
        time_t time = -300;
        char* timestring = malloc(sizeof(char) * 7);
+
+       if (timestring == NULL)
+               return;
+
        memset(timestring, 0, 7);
 
        get_time_str_overtime(time, timestring);
@@ -95,6 +119,10 @@ void get_zero_overtime()
 {
        time_t time = 0;
        char* timestring = malloc(sizeof(char) * 7);
+
+       if (timestring == NULL)
+               return;
+
        memset(timestring, 0, 7);
 
        get_time_str_overtime(time, timestring);