memleak in unittest; found by valgrind
authordjm <djm@openbsd.org>
Mon, 16 Jul 2018 03:09:59 +0000 (03:09 +0000)
committerdjm <djm@openbsd.org>
Mon, 16 Jul 2018 03:09:59 +0000 (03:09 +0000)
regress/usr.bin/ssh/unittests/hostkeys/test_iterate.c

index 3853a5f..6c1a6ad 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: test_iterate.c,v 1.5 2017/04/30 23:33:48 djm Exp $ */
+/*     $OpenBSD: test_iterate.c,v 1.6 2018/07/16 03:09:59 djm Exp $ */
 /*
  * Regress test for hostfile.h hostkeys_foreach()
  *
@@ -135,6 +135,17 @@ prepare_expected(struct expected *expected, size_t n)
        }
 }
 
+static void
+cleanup_expected(struct expected *expected, size_t n)
+{
+       size_t i;
+
+       for (i = 0; i < n; i++) {
+               sshkey_free(expected[i].l.key);
+               expected[i].l.key = NULL;
+       }
+}
+
 struct expected expected_full[] = {
        { NULL, -1, -1, 0, 0, 0, 0, -1, {
                NULL,                           /* path, don't care */
@@ -808,6 +819,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, NULL, NULL, ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate all without key parse");
@@ -818,6 +830,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, NULL, NULL, ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate specify host 1");
@@ -829,6 +842,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "prometheus.example.com", NULL, ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate specify host 2");
@@ -840,6 +854,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "sisyphus.example.com", NULL, ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate match host 1");
@@ -851,6 +866,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "prometheus.example.com", NULL, ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate match host 2");
@@ -862,6 +878,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "sisyphus.example.com", NULL, ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate specify host missing");
@@ -872,6 +889,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "actaeon.example.org", NULL, ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate match host missing");
@@ -882,6 +900,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "actaeon.example.org", NULL, ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate specify IPv4");
@@ -893,6 +912,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "tiresias.example.org", "192.0.2.1", ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate specify IPv6");
@@ -904,6 +924,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "tiresias.example.org", "2001:db8::1", ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate match IPv4");
@@ -915,6 +936,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "tiresias.example.org", "192.0.2.1", ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate match IPv6");
@@ -926,6 +948,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "tiresias.example.org", "2001:db8::1", ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate specify addr missing");
@@ -936,6 +959,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "tiresias.example.org", "192.168.0.1", ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate match addr missing");
@@ -946,6 +970,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "tiresias.example.org", "::1", ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate specify host 2 and IPv4");
@@ -958,6 +983,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "sisyphus.example.com", "192.0.2.1", ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate match host 1 and IPv6");
@@ -969,7 +995,9 @@ test_iterate(void)
        ctx.match_ipv6 = 1;
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
-           check, &ctx, "prometheus.example.com", "2001:db8::1", ctx.flags), 0);
+           check, &ctx, "prometheus.example.com",
+           "2001:db8::1", ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate specify host 2 and IPv4 w/ key parse");
@@ -982,6 +1010,7 @@ test_iterate(void)
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
            check, &ctx, "sisyphus.example.com", "192.0.2.1", ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 
        TEST_START("hostkeys_iterate match host 1 and IPv6 w/ key parse");
@@ -993,7 +1022,9 @@ test_iterate(void)
        ctx.match_ipv6 = 1;
        prepare_expected(expected_full, ctx.nexpected);
        ASSERT_INT_EQ(hostkeys_foreach(test_data_file("known_hosts"),
-           check, &ctx, "prometheus.example.com", "2001:db8::1", ctx.flags), 0);
+           check, &ctx, "prometheus.example.com",
+           "2001:db8::1", ctx.flags), 0);
+       cleanup_expected(expected_full, ctx.nexpected);
        TEST_DONE();
 }