From: Bastian Dehn Date: Fri, 10 Oct 2025 21:07:29 +0000 (+0200) Subject: fix allocation null check X-Git-Tag: 1.3.6^2~1 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=6a792509996e417bf884a64972521319036c1ca7;p=feierabend.git fix allocation null check --- diff --git a/src/feierabend.c b/src/feierabend.c index a6c6657..05b7b2f 100644 --- a/src/feierabend.c +++ b/src/feierabend.c @@ -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); diff --git a/src/time_format.c b/src/time_format.c index e7f29c4..d298783 100644 --- a/src/time_format.c +++ b/src/time_format.c @@ -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, "-"); diff --git a/src/xml.c b/src/xml.c index 54f8656..2281a15 100644 --- a/src/xml.c +++ b/src/xml.c @@ -15,9 +15,16 @@ #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); diff --git a/tests/time_format_tests.c b/tests/time_format_tests.c index 46d081e..1b1d731 100644 --- a/tests/time_format_tests.c +++ b/tests/time_format_tests.c @@ -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);