From 0196eb3aa33001e2513fc4ede132362584025695 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sun, 23 Jun 2024 13:55:06 +0200 Subject: [PATCH] fix leaks and add saldo entry --- src/feierabendxml.c | 16 +++++++++++----- src/time_format.c | 5 +++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/feierabendxml.c b/src/feierabendxml.c index dba4921..093df79 100644 --- a/src/feierabendxml.c +++ b/src/feierabendxml.c @@ -82,7 +82,7 @@ void writeEntryNode(xmlTextWriterPtr xmlWriter, int begin_min, int end_hour, int end_min, - int last_saldo) + time_t last_saldo) { feierabend* fabend = malloc(sizeof(feierabend)); init_feierabend(fabend); @@ -344,10 +344,9 @@ void addEntry(int begin_hour, int end_min, const char* file_path) { - char* saldo = malloc(sizeof(char) * MAX_STRING_LENGTH); - memset(saldo, 0, MAX_STRING_LENGTH); - - readLastSaldo(file_path, saldo); + char* saldostr = malloc(sizeof(char) * MAX_STRING_LENGTH); + memset(saldostr, 0, MAX_STRING_LENGTH); + readLastSaldo(file_path, saldostr); xmlChar* xmlElemContent = NULL; xmlTextWriterPtr xmlwriter = xmlNewTextWriterFilename("/dev/stdout", 0); @@ -377,6 +376,9 @@ void addEntry(int begin_hour, } } + time_t* saldo = malloc(sizeof(time_t)); + get_seconds_from_string(saldostr, saldo); + writeEntryNode(xmlwriter, begin_hour, begin_min, end_hour, end_min, *saldo); xmlTextWriterEndElement(xmlwriter); xmlTextWriterEndDocument(xmlwriter); @@ -387,8 +389,12 @@ void addEntry(int begin_hour, xmlCleanupCharEncodingHandlers(); xmlDictCleanup(); + free(xmlElemContent); + xmlElemContent = NULL; free(saldo); saldo = NULL; + free(saldostr); + saldostr = NULL; } int main(int argc, char* argv[]) diff --git a/src/time_format.c b/src/time_format.c index 298c5fc..eec05ed 100644 --- a/src/time_format.c +++ b/src/time_format.c @@ -40,14 +40,19 @@ 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); memset(str, 0, 7); strncpy(str, timestr, 7); + first_str_pos = str; str = strtok(str, ":"); *seconds = atol(str) * ONE_HOUR; str = strtok(NULL, ":"); *seconds += atol(str) * 60; + + free(first_str_pos); + first_str_pos = NULL; } void get_weekday(int wday, char* weekday) -- 2.39.5