From: Scott Worley Date: Thu, 14 Sep 2023 06:43:01 +0000 (-0700) Subject: Fix memory leak X-Git-Tag: v1.0.1~3 X-Git-Url: http://git.scottworley.com/tl-append/commitdiff_plain/9b76e9d2c7bdd413e36432b64a0022852a484dea?ds=sidebyside Fix memory leak --- diff --git a/Changelog b/Changelog index 899f4dc..7a11b0e 100644 --- a/Changelog +++ b/Changelog @@ -1,5 +1,8 @@ # Changelog +## Unrelased +- Fix memory leak + ## [1.0.0] - 2023-09-13 Initial release diff --git a/common.c b/common.c index 747111b..88e7b73 100644 --- a/common.c +++ b/common.c @@ -17,7 +17,7 @@ void die_err(const char *message) { exit(1); } -const char *encode_time(time_t t) { +char *encode_time(time_t t) { struct tm tm; localtime_r(&t, &tm); const size_t size = 20; diff --git a/common.h b/common.h index 93d722f..3563d00 100644 --- a/common.h +++ b/common.h @@ -6,4 +6,4 @@ void die(const char *message); void die_err(const char *message); -const char *encode_time(time_t t); +char *encode_time(time_t t); diff --git a/tl-append-test.c b/tl-append-test.c index e712c6f..ee5d3f7 100644 --- a/tl-append-test.c +++ b/tl-append-test.c @@ -171,7 +171,7 @@ static void test_encode_time() { if (tt == (time_t)-1) die_err("Can't pack time?"); - const char *encoded = encode_time(tt); + char *encoded = encode_time(tt); /* Loose check to allow for daylight savings time changes between the current * time and the target time. :( */ assert(encoded[0] == '2'); @@ -194,6 +194,7 @@ static void test_encode_time() { assert(encoded[17] == '1'); assert(encoded[18] == '6'); assert(encoded[19] == '\0'); + free(encoded); } static FILE *take_lock(FILE *f, char *lock_type) { diff --git a/tl-append.c b/tl-append.c index 52d2602..edea9e9 100644 --- a/tl-append.c +++ b/tl-append.c @@ -136,7 +136,7 @@ static void write_acknowledgment(conf_t *conf) { } static void lock_and_write_line(conf_t *conf, const char *line) { - const char *now = encode_time(time(NULL)); + char *now = encode_time(time(NULL)); FILE *f = fopen(FILENAME, "a"); take_lock(conf, f); @@ -145,6 +145,7 @@ static void lock_and_write_line(conf_t *conf, const char *line) { if (fclose(f) != 0) die_err("Error closing output file"); + free(now); write_acknowledgment(conf); }