]> git.scottworley.com Git - tl-append/blobdiff - tl-append-test.c
test: Fix memory leak
[tl-append] / tl-append-test.c
index 04c679e1b2d2e3b482a86c760b54a109650574ca..30cfaaec299f3c994215e00d8c0362698e479084 100644 (file)
@@ -28,9 +28,12 @@ const ex_t CONSUMED = {((time_t)-2), ((time_t)-2), NULL};
 static int is_end(ex_t exp) {
   return exp.a == END.a && exp.b == END.b && exp.message == END.message;
 }
+static void consume(ex_t *exp) {
+  exp->a = CONSUMED.a;
+  exp->b = CONSUMED.b;
+}
 static int is_consumed(ex_t exp) {
-  return exp.a == CONSUMED.a && exp.b == CONSUMED.b &&
-         exp.message == CONSUMED.message;
+  return exp.a == CONSUMED.a && exp.b == CONSUMED.b;
 }
 static ex_t expectation(time_t a, time_t b, const char *message) {
   ex_t exp;
@@ -126,7 +129,7 @@ static void verify_log_contents(const ex_t exps[]) {
 static void consume_expectation(ex_t exps[], const char *line) {
   for (size_t i = 0; !is_end(exps[i]); i++) {
     if (line_problem(&exps[i], line) == NULL) {
-      exps[i] = CONSUMED;
+      consume(&exps[i]);
       return;
     }
   }
@@ -171,7 +174,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 +197,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) {
@@ -255,7 +259,7 @@ static void write_and_read_two_lines() {
 static void write_to_locked_log(char *lock_types[]) {
   remove_logfile();
   ex_t e1 = write_to_tl_append("begin\n");
-  FILE *f = fopen(FILENAME, "a");
+  FILE *f = fopen(FILENAME, "ae");
   if (f == NULL)
     die_err("Couldn't open file for locking");
   for (int i = 0; lock_types[i]; i++)
@@ -304,6 +308,9 @@ static void write_concurrently() {
   }
   results[PARALLELISM] = END;
   verify_log_contents_unordered(results);
+  for (int i = 0; i < PARALLELISM; i++) {
+    free((void *)results[i].message);
+  }
 }
 
 int main() {
@@ -313,7 +320,7 @@ int main() {
   write_to_locked_log((char *[]){NULL});
   write_to_locked_log((char *[]){"fcntl", NULL});
   write_to_locked_log((char *[]){"flock", NULL});
-  write_to_locked_log((char *[]){"flock", "fcntl", NULL});
+  write_to_locked_log((char *[]){"flock", "fcntl", NULL}); /* Deadlock risk! */
   write_to_locked_log((char *[]){"fcntl", "flock", NULL});
   write_concurrently();
 }