From 8fdcae34e13aae8aaa3e70015627bc6af0f595df Mon Sep 17 00:00:00 2001 From: Agustin Ranieri Date: Wed, 24 Aug 2022 20:12:05 -0300 Subject: [PATCH 1/4] temporal: fix memory leak, show error --- src/commons/temporal.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/commons/temporal.c b/src/commons/temporal.c index 1fa24495..4989b39d 100644 --- a/src/commons/temporal.c +++ b/src/commons/temporal.c @@ -33,6 +33,8 @@ char *temporal_get_string_time(const char* format) { char* milisec; if(clock_gettime(CLOCK_REALTIME, log_timespec) == -1) { + error_show("Error getting date!"); + free(str_time); return NULL; } milisec = string_from_format("%03ld", log_timespec->tv_nsec / 1000000); @@ -53,12 +55,12 @@ char *temporal_get_string_time(const char* format) { t_temporal* temporal_create(void) { t_temporal* self = malloc(sizeof(t_temporal)); - + self->elapsed_ms = 0; self->state = TEMPORAL_STATUS_RUNNING; - + clock_gettime(CLOCK_MONOTONIC_RAW, &self->current); - + return self; } @@ -70,7 +72,7 @@ int64_t temporal_gettime(t_temporal* temporal) { if (temporal->state == TEMPORAL_STATUS_STOPPED) { return temporal->elapsed_ms; } - + int64_t delta_ms = calculate_delta_ms(temporal); return delta_ms + temporal->elapsed_ms; @@ -101,8 +103,8 @@ int64_t temporal_diff(t_temporal* temporal_1, t_temporal* temporal_2) { static int64_t calculate_delta_ms(t_temporal* temporal) { struct timespec now; clock_gettime(CLOCK_MONOTONIC_RAW, &now); - + int64_t delta_ms = (now.tv_sec - temporal->current.tv_sec) * 1000 + (now.tv_nsec - temporal->current.tv_nsec) / 1000000; - + return delta_ms; -} \ No newline at end of file +} From 8c351391085f7774cbd31a856efb854a0e24ff56 Mon Sep 17 00:00:00 2001 From: Agustin Ranieri Date: Wed, 24 Aug 2022 20:13:58 -0300 Subject: [PATCH 2/4] temporal: code style --- src/commons/temporal.c | 19 +++++++---------- src/commons/temporal.h | 28 ++++++++++++------------- tests/integration-tests/temporal/main.c | 6 +++--- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/commons/temporal.c b/src/commons/temporal.c index 4989b39d..67ebcf74 100644 --- a/src/commons/temporal.c +++ b/src/commons/temporal.c @@ -14,11 +14,8 @@ * along with this program. If not, see . */ #include "temporal.h" -#include "error.h" #include "string.h" -#include -#include #include #include #include @@ -32,14 +29,14 @@ char *temporal_get_string_time(const char* format) { struct tm* log_tm = malloc(sizeof(struct tm)); char* milisec; - if(clock_gettime(CLOCK_REALTIME, log_timespec) == -1) { + if (clock_gettime(CLOCK_REALTIME, log_timespec) == -1) { error_show("Error getting date!"); free(str_time); return NULL; } milisec = string_from_format("%03ld", log_timespec->tv_nsec / 1000000); - for(char* ms = strstr(str_time, "%MS"); ms != NULL; ms = strstr(ms + 3, "%MS")) { + for (char* ms = strstr(str_time, "%MS"); ms != NULL; ms = strstr(ms + 3, "%MS")) { memcpy(ms, milisec, 3); } @@ -57,7 +54,7 @@ t_temporal* temporal_create(void) { t_temporal* self = malloc(sizeof(t_temporal)); self->elapsed_ms = 0; - self->state = TEMPORAL_STATUS_RUNNING; + self->status = TEMPORAL_STATUS_RUNNING; clock_gettime(CLOCK_MONOTONIC_RAW, &self->current); @@ -69,7 +66,7 @@ void temporal_destroy(t_temporal* temporal) { } int64_t temporal_gettime(t_temporal* temporal) { - if (temporal->state == TEMPORAL_STATUS_STOPPED) { + if (temporal->status == TEMPORAL_STATUS_STOPPED) { return temporal->elapsed_ms; } @@ -79,20 +76,20 @@ int64_t temporal_gettime(t_temporal* temporal) { } void temporal_stop(t_temporal* temporal) { - if (temporal->state == TEMPORAL_STATUS_STOPPED) { + if (temporal->status == TEMPORAL_STATUS_STOPPED) { return; } temporal->elapsed_ms += calculate_delta_ms(temporal); - temporal->state = TEMPORAL_STATUS_STOPPED; + temporal->status = TEMPORAL_STATUS_STOPPED; } void temporal_resume(t_temporal* temporal) { - if (temporal->state == TEMPORAL_STATUS_RUNNING) { + if (temporal->status == TEMPORAL_STATUS_RUNNING) { return; } - temporal->state = TEMPORAL_STATUS_RUNNING; + temporal->status = TEMPORAL_STATUS_RUNNING; clock_gettime(CLOCK_MONOTONIC_RAW, &temporal->current); } diff --git a/src/commons/temporal.h b/src/commons/temporal.h index b2bfd9f4..1e3cea69 100644 --- a/src/commons/temporal.h +++ b/src/commons/temporal.h @@ -20,24 +20,13 @@ #include /** - * @NAME: temporal_get_string_time - * @DESC: Retorna un string con la hora actual, - * con el formato recibido por parámetro. - * Ejemplos: - * temporal_get_string_time("%d/%m/%y") => "30/09/20" - * temporal_get_string_time("%H:%M:%S:%MS") => "12:51:59:331" - * temporal_get_string_time("%d/%m/%y %H:%M:%S") => "30/09/20 12:51:59" - */ - char *temporal_get_string_time(const char* format); - - /** - * @NAME: t_state + * @NAME: t_temporal_status * @DESC: Estado de una variable temporal. */ typedef enum { TEMPORAL_STATUS_STOPPED, TEMPORAL_STATUS_RUNNING - } t_state; + } t_temporal_status; /** * @NAME: t_temporal @@ -46,9 +35,20 @@ typedef struct { struct timespec current; int64_t elapsed_ms; - t_state state; + t_temporal_status status; } t_temporal; + /** + * @NAME: temporal_get_string_time + * @DESC: Retorna un string con la hora actual, + * con el formato recibido por parámetro. + * Ejemplos: + * temporal_get_string_time("%d/%m/%y") => "30/09/20" + * temporal_get_string_time("%H:%M:%S:%MS") => "12:51:59:331" + * temporal_get_string_time("%d/%m/%y %H:%M:%S") => "30/09/20 12:51:59" + */ + char *temporal_get_string_time(const char* format); + /** * @NAME: temporal_create * @DESC: Crea una variable temporal e inicia su cronómetro. diff --git a/tests/integration-tests/temporal/main.c b/tests/integration-tests/temporal/main.c index b8022e39..4ef67118 100644 --- a/tests/integration-tests/temporal/main.c +++ b/tests/integration-tests/temporal/main.c @@ -106,7 +106,7 @@ void test_temporal_diff() { temporal_destroy(temporal3); } -void test_temporal_state() { +void test_temporal_status() { /** * @DESC: Verificar el comportamiento del temporal en sus diferentes estados. * En este test se verifica que no ocurra ningún error al usar el temporal de cualquier forma. @@ -128,7 +128,7 @@ void test_temporal_state() { temporal_resume(temporal3); sleep(1); - printf("test_temporal_state: %lums (expected: ~1000ms)\n\n", temporal_gettime(temporal3)); + printf("test_temporal_status: %lums (expected: ~1000ms)\n\n", temporal_gettime(temporal3)); temporal_destroy(temporal3); } @@ -156,7 +156,7 @@ int main(int argc, char** argv) { test_temporal_stop(); test_temporal_resume(); test_temporal_diff(); - test_temporal_state(); + test_temporal_status(); return (EXIT_SUCCESS); } \ No newline at end of file From 8f7c48ec98f2dd27edd6188bf43c528ef8c19fc2 Mon Sep 17 00:00:00 2001 From: Agustin Ranieri Date: Wed, 24 Aug 2022 20:16:56 -0300 Subject: [PATCH 3/4] temporal: local variables in stack --- src/commons/temporal.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/commons/temporal.c b/src/commons/temporal.c index 67ebcf74..6d463d6b 100644 --- a/src/commons/temporal.c +++ b/src/commons/temporal.c @@ -25,27 +25,25 @@ static int64_t calculate_delta_ms(t_temporal* temporal); char *temporal_get_string_time(const char* format) { char* str_time = strdup(format); - struct timespec* log_timespec = malloc(sizeof(struct timespec)); - struct tm* log_tm = malloc(sizeof(struct tm)); + struct timespec log_timespec; + struct tm log_tm; char* milisec; - if (clock_gettime(CLOCK_REALTIME, log_timespec) == -1) { + if (clock_gettime(CLOCK_REALTIME, &log_timespec) == -1) { error_show("Error getting date!"); free(str_time); return NULL; } - milisec = string_from_format("%03ld", log_timespec->tv_nsec / 1000000); + milisec = string_from_format("%03ld", log_timespec.tv_nsec / 1000000); for (char* ms = strstr(str_time, "%MS"); ms != NULL; ms = strstr(ms + 3, "%MS")) { memcpy(ms, milisec, 3); } - localtime_r(&log_timespec->tv_sec, log_tm); - strftime(str_time, strlen(format) + 1, str_time, log_tm); + localtime_r(&log_timespec.tv_sec, &log_tm); + strftime(str_time, strlen(format) + 1, str_time, &log_tm); free(milisec); - free(log_tm); - free(log_timespec); return str_time; } From 6ed2579271e58ceca438454ba80a5aaa4e3ce962 Mon Sep 17 00:00:00 2001 From: Agustin Ranieri Date: Wed, 24 Aug 2022 20:22:36 -0300 Subject: [PATCH 4/4] temporal: add missing include --- src/commons/temporal.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/commons/temporal.c b/src/commons/temporal.c index 6d463d6b..81c5e19b 100644 --- a/src/commons/temporal.c +++ b/src/commons/temporal.c @@ -14,6 +14,7 @@ * along with this program. If not, see . */ #include "temporal.h" +#include "error.h" #include "string.h" #include