]> granicus.if.org Git - p11-kit/commitdiff
Fix various memory leaks exposed by 'make leakcheck'
authorStef Walter <stef@thewalter.net>
Tue, 23 Jul 2013 14:45:50 +0000 (16:45 +0200)
committerStef Walter <stef@thewalter.net>
Tue, 23 Jul 2013 21:06:30 +0000 (23:06 +0200)
common/tests/test-path.c
p11-kit/modules.c
trust/asn1.c
trust/builder.c
trust/extract-openssl.c
trust/index.c
trust/parser.c
trust/tests/Makefile.am
trust/tests/test-asn1.c
trust/tests/test-builder.c
trust/token.c

index a6ba54d1b95ae585795c50cd9456275da7d0ea80..57619c801fe4575b5d5bdd0bce5c91914e09a932 100644 (file)
@@ -75,34 +75,40 @@ test_base (void)
        }
 }
 
-#define check_equals_and_free(ex, ac) \
-       do { assert_str_eq (ex, ac); free (ac); } while (0)
+#define assert_str_eq_free(ex, ac) \
+       do { const char *__s1 = (ex); \
+            char *__s2 = (ac); \
+            if (__s1 && __s2 && strcmp (__s1, __s2) == 0) ; else \
+                p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s == %s): (%s == %s)", \
+                               #ex, #ac, __s1 ? __s1 : "(null)", __s2 ? __s2 : "(null)"); \
+            free (__s2); \
+       } while (0)
 
 static void
 test_build (void)
 {
 #ifdef OS_UNIX
-       check_equals_and_free ("/root/second",
-                              p11_path_build ("/root", "second", NULL));
-       check_equals_and_free ("/root/second",
-                              p11_path_build ("/root", "/second", NULL));
-       check_equals_and_free ("/root/second",
-                              p11_path_build ("/root/", "second", NULL));
-       check_equals_and_free ("/root/second/third",
-                              p11_path_build ("/root", "second", "third", NULL));
-       check_equals_and_free ("/root/second/third",
-                              p11_path_build ("/root", "/second/third", NULL));
+       assert_str_eq_free ("/root/second",
+                           p11_path_build ("/root", "second", NULL));
+       assert_str_eq_free ("/root/second",
+                           p11_path_build ("/root", "/second", NULL));
+       assert_str_eq_free ("/root/second",
+                           p11_path_build ("/root/", "second", NULL));
+       assert_str_eq_free ("/root/second/third",
+                           p11_path_build ("/root", "second", "third", NULL));
+       assert_str_eq_free ("/root/second/third",
+                           p11_path_build ("/root", "/second/third", NULL));
 #else /* OS_WIN32 */
-       check_equals_and_free ("C:\\root\\second",
-                              p11_path_build ("C:\\root", "second", NULL));
-       check_equals_and_free ("C:\\root\\second",
-                              p11_path_build ("C:\\root", "\\second", NULL));
-       check_equals_and_free ("C:\\root\\second",
-                              p11_path_build ("C:\\root\\", "second", NULL));
-       check_equals_and_free ("C:\\root\\second\\third",
-                              p11_path_build ("C:\\root", "second", "third", NULL));
-       check_equals_and_free ("C:\\root\\second/third",
-                              p11_path_build ("C:\\root", "second/third", NULL));
+       assert_str_eq_free ("C:\\root\\second",
+                           p11_path_build ("C:\\root", "second", NULL));
+       assert_str_eq_free ("C:\\root\\second",
+                           p11_path_build ("C:\\root", "\\second", NULL));
+       assert_str_eq_free ("C:\\root\\second",
+                           p11_path_build ("C:\\root\\", "second", NULL));
+       assert_str_eq_free ("C:\\root\\second\\third",
+                           p11_path_build ("C:\\root", "second", "third", NULL));
+       assert_str_eq_free ("C:\\root\\second/third",
+                           p11_path_build ("C:\\root", "second/third", NULL));
 #endif
 }
 
@@ -113,22 +119,22 @@ test_expand (void)
 
 #ifdef OS_UNIX
        putenv ("HOME=/home/blah");
-       check_equals_and_free ("/home/blah/my/path",
-                              p11_path_expand ("~/my/path"));
-       check_equals_and_free ("/home/blah",
-                              p11_path_expand ("~"));
+       assert_str_eq_free ("/home/blah/my/path",
+                           p11_path_expand ("~/my/path"));
+       assert_str_eq_free ("/home/blah",
+                           p11_path_expand ("~"));
        putenv ("XDG_CONFIG_HOME=/my");
-       check_equals_and_free ("/my/path",
-                              p11_path_expand ("~/.config/path"));
+       assert_str_eq_free ("/my/path",
+                           p11_path_expand ("~/.config/path"));
        putenv ("XDG_CONFIG_HOME=");
-       check_equals_and_free ("/home/blah/.config/path",
-                              p11_path_expand ("~/.config/path"));
+       assert_str_eq_free ("/home/blah/.config/path",
+                           p11_path_expand ("~/.config/path"));
 #else /* OS_WIN32 */
        putenv ("HOME=C:\\Users\\blah");
-       check_equals_and_free ("C:\\Users\\blah\\path",
-                              p11_path_expand ("~/my/path"));
-       check_equals_and_free ("C:\\Users\\blah\\path",
-                              p11_path_expand ("~\\path"));
+       assert_str_eq_free ("C:\\Users\\blah\\path",
+                           p11_path_expand ("~/my/path"));
+       assert_str_eq_free ("C:\\Users\\blah\\path",
+                           p11_path_expand ("~\\path"));
 #endif
 
        putenv("HOME=");
@@ -153,14 +159,14 @@ test_absolute (void)
 static void
 test_parent (void)
 {
-       check_equals_and_free ("/", p11_path_parent ("/root"));
-       check_equals_and_free ("/", p11_path_parent ("/root/"));
-       check_equals_and_free ("/", p11_path_parent ("/root//"));
-       check_equals_and_free ("/root", p11_path_parent ("/root/second"));
-       check_equals_and_free ("/root", p11_path_parent ("/root//second"));
-       check_equals_and_free ("/root", p11_path_parent ("/root//second//"));
-       check_equals_and_free ("/root", p11_path_parent ("/root///second"));
-       check_equals_and_free ("/root/second", p11_path_parent ("/root/second/test.file"));
+       assert_str_eq_free ("/", p11_path_parent ("/root"));
+       assert_str_eq_free ("/", p11_path_parent ("/root/"));
+       assert_str_eq_free ("/", p11_path_parent ("/root//"));
+       assert_str_eq_free ("/root", p11_path_parent ("/root/second"));
+       assert_str_eq_free ("/root", p11_path_parent ("/root//second"));
+       assert_str_eq_free ("/root", p11_path_parent ("/root//second//"));
+       assert_str_eq_free ("/root", p11_path_parent ("/root///second"));
+       assert_str_eq_free ("/root/second", p11_path_parent ("/root/second/test.file"));
        assert_ptr_eq (NULL, p11_path_parent ("/"));
        assert_ptr_eq (NULL, p11_path_parent ("//"));
        assert_ptr_eq (NULL, p11_path_parent (""));
index 43ace182794afeb116c3447c79b7bf1935de22c6..b373544296854ed383c41abef69e1b290ca5aacf 100644 (file)
@@ -467,8 +467,10 @@ take_config_and_load_module_inlock (char **name,
                return CKR_OK;
 
        /* Take ownership of thes evariables */
+       p11_dict_free (mod->config);
        mod->config = *config;
        *config = NULL;
+       free (mod->name);
        mod->name = *name;
        *name = NULL;
        mod->critical = critical;
index 7ed3b0188439c213670ca7debc0582bcd8627470..dd1812dbb98eb56d41f866f75554d53df46e93ff 100644 (file)
@@ -327,8 +327,10 @@ p11_asn1_cache_take (p11_asn1_cache *cache,
 {
        asn1_item *item;
 
-       if (cache == NULL)
+       if (cache == NULL) {
+               asn1_delete_structure (&node);
                return;
+       }
 
        return_if_fail (struct_name != NULL);
        return_if_fail (der != NULL);
index 2daadb3ec2ebc8a124287b0486bdc5c3c00f7c9f..18c09ade1797958ef06d3d5f318f3f3c457eb8ed 100644 (file)
@@ -1018,9 +1018,6 @@ build_for_schema (p11_builder *builder,
                }
        }
 
-       if (populate && schema->populate)
-               *extra = schema->populate (builder, index, merge);
-
        /* Validate the result, before committing to the change. */
        if (!loading && schema->validate) {
                rv = (schema->validate) (builder, attrs, merge);
@@ -1028,6 +1025,9 @@ build_for_schema (p11_builder *builder,
                        return rv;
        }
 
+       if (populate && schema->populate)
+               *extra = schema->populate (builder, index, merge);
+
        return CKR_OK;
 }
 
index c263ba1b040f35d0545aa36d254877dec39edc3e..1f12f11aa18f2325ee84d942d7f9634b15a36869 100644 (file)
@@ -672,6 +672,7 @@ p11_extract_openssl_directory (P11KitIter *iter,
 
                        free (filename);
                        free (path);
+                       free (name);
                }
 
                if (!ret)
index 83f0dc03eabd163df222d9b51a93c7171a861e26..83a4503439c2f2db0952c4996b8a16cef46d0ec2 100644 (file)
@@ -352,6 +352,7 @@ index_build (p11_index *index,
 
                count = nmerge;
                memcpy (built, merge, sizeof (CK_ATTRIBUTE) * nmerge);
+               p11_array_push (stack, merge);
                merge_attrs (built, &count, *attrs, nattrs, stack);
                merge_attrs (built, &count, extra, nextra, stack);
 
@@ -367,7 +368,6 @@ index_build (p11_index *index,
                        free (stack->elem[i]);
                *attrs = built;
        } else {
-               p11_attrs_free (merge);
                p11_attrs_free (extra);
                free (built);
        }
index 4129cc00f7047eef49eeecbc869f61bb587ba36d..54d9c15418b6d360cc3c841b6f819192101e866a 100644 (file)
@@ -676,6 +676,7 @@ p11_parser_free (p11_parser *parser)
        return_if_fail (parser != NULL);
        p11_persist_free (parser->persist);
        p11_array_free (parser->parsed);
+       p11_array_free (parser->formats);
        if (parser->asn1_owned)
                p11_dict_free (parser->asn1_defs);
        free (parser);
index 5a6c9ec91f87b0061bbda1187b201d57869e125d..53775df21755a1841120e980158964f5e9874ac5 100644 (file)
@@ -37,6 +37,12 @@ LDADD = \
 
 CHECK_PROGS = \
        test-digest \
+       test-asn1 \
+       test-base64 \
+       test-pem \
+       test-oid \
+       test-utf8 \
+       test-x509 \
        test-persist \
        test-index \
        test-parser \
@@ -48,12 +54,6 @@ CHECK_PROGS = \
        test-cer \
        test-bundle \
        test-openssl \
-       test-asn1 \
-       test-base64 \
-       test-pem \
-       test-oid \
-       test-utf8 \
-       test-x509 \
        $(NULL)
 
 noinst_PROGRAMS = \
index d5ec131b7cd2f4de4feb73d464672df90704438c..df75dfd0e7ca9113315b85526137fe9570addbd5 100644 (file)
@@ -142,10 +142,11 @@ test_asn1_free (void)
        asn = p11_asn1_decode (defs, "PKIX1.ExtKeyUsageSyntax",
                               test_eku_server_and_client,
                               sizeof (test_eku_server_and_client), NULL);
-       assert_ptr_not_null (defs);
+       assert_ptr_not_null (asn);
 
        p11_asn1_free (asn);
        p11_asn1_free (NULL);
+       p11_dict_free (defs);
 }
 
 int
index 1d37924e71b11c854738b08c04e810ad3d9b99b9..6e061aa0a91b4af6d2a753cf727be0114ae2b4ec 100644 (file)
@@ -766,6 +766,7 @@ test_valid_dates (void)
        rv = p11_builder_build (test.builder, test.index, attrs, input, &extra);
        assert_num_eq (CKR_OK, rv);
 
+       p11_attrs_free (extra);
        p11_attrs_free (attrs);
        attrs = NULL;
 
@@ -773,6 +774,7 @@ test_valid_dates (void)
        rv = p11_builder_build (test.builder, test.index, attrs, input, &extra);
        assert_num_eq (CKR_OK, rv);
 
+       p11_attrs_free (extra);
        p11_attrs_free (attrs);
 }
 
@@ -831,6 +833,7 @@ test_valid_name (void)
        rv = p11_builder_build (test.builder, test.index, attrs, input, &extra);
        assert_num_eq (CKR_OK, rv);
 
+       p11_attrs_free (extra);
        p11_attrs_free (attrs);
        attrs = NULL;
 
@@ -839,6 +842,7 @@ test_valid_name (void)
        rv = p11_builder_build (test.builder, test.index, attrs, input, &extra);
        assert_num_eq (CKR_OK, rv);
 
+       p11_attrs_free (extra);
        p11_attrs_free (attrs);
 }
 
index 97b1fc04f4f7f33343efb62e21c47352407b78d1..1336443e891733ddf4e7c22a4c99eedbea9ccdda 100644 (file)
@@ -396,6 +396,7 @@ p11_token_reload (p11_token *token,
        CK_ATTRIBUTE *attr;
        struct stat sb;
        char *origin;
+       bool ret;
 
        attr = p11_attrs_find (attrs, CKA_X_ORIGIN);
        if (attr == NULL)
@@ -410,10 +411,14 @@ p11_token_reload (p11_token *token,
                } else {
                        p11_message_err (errno, "cannot access trust file: %s", origin);
                }
-               return false;
+               ret = false;
+
+       } else {
+               ret = loader_load_file (token, origin, &sb) > 0;
        }
 
-       return loader_load_file (token, origin, &sb) > 0;
+       free (origin);
+       return ret;
 }
 
 static bool
@@ -676,6 +681,7 @@ on_index_store (void *data,
 
        p11_buffer_uninit (&buffer);
        p11_persist_free (persist);
+       free (other);
 
        if (rv == CKR_OK) {
                if (!p11_save_finish_file (file, &path, true))