Skip to content

Commit

Permalink
Merge pull request #163 from sisoputnfrba/fix/temporal-string
Browse files Browse the repository at this point in the history
temporal_get_string_time: fix memory leak on error
  • Loading branch information
RaniAgus authored Aug 24, 2022
2 parents 820bfbb + 6ed2579 commit 117a9e1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 41 deletions.
46 changes: 22 additions & 24 deletions src/commons/temporal.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include "error.h"
#include "string.h"

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
Expand All @@ -28,37 +26,37 @@ 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")) {
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;
}

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);

return self;
}

Expand All @@ -67,30 +65,30 @@ 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;
}

int64_t delta_ms = calculate_delta_ms(temporal);

return delta_ms + temporal->elapsed_ms;
}

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);
}

Expand All @@ -101,8 +99,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;
}
}
28 changes: 14 additions & 14 deletions src/commons/temporal.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,13 @@
#include <time.h>

/**
* @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
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions tests/integration-tests/temporal/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}

0 comments on commit 117a9e1

Please sign in to comment.