Skip to content

Commit e6569be

Browse files
committed
tests: fix memory leaks in c tests
1 parent 5a92d1c commit e6569be

5 files changed

Lines changed: 48 additions & 16 deletions

File tree

tests/xbps/libxbps/find_pkg_orphans/main.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ ATF_TC_BODY(find_pkg_orphans_test, tc)
7171
xbps_strlcpy(xh.metadir, tcsdir, sizeof(xh.metadir));
7272
ATF_REQUIRE_EQ(xbps_init(&xh), 0);
7373

74-
a = xbps_array_create();
75-
xbps_array_add_cstring_nocopy(a, "xbps-git");
74+
ATF_REQUIRE((a = xbps_array_create()));
75+
ATF_REQUIRE(xbps_array_add_cstring_nocopy(a, "xbps-git"));
7676

7777
pstr = xbps_string_create();
7878
res = xbps_find_pkg_orphans(&xh, a);
@@ -82,7 +82,11 @@ ATF_TC_BODY(find_pkg_orphans_test, tc)
8282
xbps_string_append_cstring(pstr, pkgver);
8383
xbps_string_append_cstring(pstr, "\n");
8484
}
85+
xbps_object_release(a);
86+
xbps_object_release(res);
8587
ATF_REQUIRE_STREQ(xbps_string_cstring_nocopy(pstr), expected_output);
88+
xbps_object_release(pstr);
89+
xbps_end(&xh);
8690
}
8791

8892
ATF_TC(find_all_orphans_test);
@@ -112,13 +116,16 @@ ATF_TC_BODY(find_all_orphans_test, tc)
112116
res = xbps_find_pkg_orphans(&xh, NULL);
113117
for (i = 0; i < xbps_array_count(res); i++) {
114118
pkgd = xbps_array_get(res, i);
115-
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
116-
xbps_string_append_cstring(pstr, pkgver);
117-
xbps_string_append_cstring(pstr, "\n");
119+
ATF_REQUIRE(xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver));
120+
ATF_REQUIRE(xbps_string_append_cstring(pstr, pkgver));
121+
ATF_REQUIRE(xbps_string_append_cstring(pstr, "\n"));
118122
}
119123
printf("%s", xbps_string_cstring_nocopy(pstr));
124+
xbps_object_release(res);
120125

121126
ATF_REQUIRE_STREQ(xbps_string_cstring_nocopy(pstr), expected_output_all);
127+
xbps_object_release(pstr);
128+
xbps_end(&xh);
122129
}
123130

124131
ATF_TP_ADD_TCS(tp)

tests/xbps/libxbps/pkgdb/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ ATF_TC_BODY(pkgdb_get_pkg_test, tc)
6666
ATF_REQUIRE_EQ(xbps_object_type(pkgd), XBPS_TYPE_DICTIONARY);
6767
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
6868
ATF_REQUIRE_STREQ(pkgver, "mixed-0.1_1");
69+
xbps_end(&xh);
6970
}
7071

7172
ATF_TC(pkgdb_get_virtualpkg_test);
@@ -108,6 +109,7 @@ ATF_TC_BODY(pkgdb_get_virtualpkg_test, tc)
108109
ATF_REQUIRE_EQ(xbps_object_type(pkgd), XBPS_TYPE_DICTIONARY);
109110
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
110111
ATF_REQUIRE_STREQ(pkgver, "virtual-mixed-0.1_1");
112+
xbps_end(&xh);
111113
}
112114

113115
ATF_TC(pkgdb_get_pkg_revdeps_test);
@@ -144,6 +146,8 @@ ATF_TC_BODY(pkgdb_get_pkg_revdeps_test, tc)
144146
xbps_string_append_cstring(pstr, "\n");
145147
}
146148
ATF_REQUIRE_STREQ(xbps_string_cstring_nocopy(pstr), eout);
149+
xbps_object_release(pstr);
150+
xbps_end(&xh);
147151
}
148152

149153
ATF_TC(pkgdb_pkg_reverts_test);
@@ -174,6 +178,7 @@ ATF_TC_BODY(pkgdb_pkg_reverts_test, tc)
174178
ATF_REQUIRE_EQ(xbps_pkg_reverts(pkgd, "reverts-0.3_1"), 1);
175179
ATF_REQUIRE_EQ(xbps_pkg_reverts(pkgd, "reverts-0.4_1"), 1);
176180
ATF_REQUIRE_EQ(xbps_pkg_reverts(pkgd, "reverts-0.5_1"), 0);
181+
xbps_end(&xh);
177182
}
178183

179184
ATF_TP_ADD_TCS(tp)

tests/xbps/libxbps/plist_match/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ ATF_TC_BODY(match_string_test, tc)
5050
xbps_array_t a = array_init();
5151
ATF_REQUIRE_EQ(xbps_match_string_in_array(a, "foo-2.0_1"), true);
5252
ATF_REQUIRE_EQ(xbps_match_string_in_array(a, "foo-2.1_1"), false);
53+
xbps_object_release(a);
5354
}
5455

5556
ATF_TC(match_pkgname_test);
@@ -63,6 +64,7 @@ ATF_TC_BODY(match_pkgname_test, tc)
6364
xbps_array_t a = array_init();
6465
ATF_REQUIRE_EQ(xbps_match_pkgname_in_array(a, "foo"), true);
6566
ATF_REQUIRE_EQ(xbps_match_pkgname_in_array(a, "baz"), false);
67+
xbps_object_release(a);
6668
}
6769

6870
ATF_TC(match_pkgpattern_test);
@@ -76,6 +78,7 @@ ATF_TC_BODY(match_pkgpattern_test, tc)
7678
xbps_array_t a = array_init();
7779
ATF_REQUIRE_EQ(xbps_match_pkgpattern_in_array(a, "foo>=1.0"), true);
7880
ATF_REQUIRE_EQ(xbps_match_pkgpattern_in_array(a, "blah<1.0"), false);
81+
xbps_object_release(a);
7982
}
8083

8184
ATF_TC(match_pkgdep_test);
@@ -88,6 +91,7 @@ ATF_TC_BODY(match_pkgdep_test, tc)
8891
{
8992
xbps_array_t a = array_init();
9093
ATF_REQUIRE_EQ(xbps_match_pkgdep_in_array(a, "foo-2.0_1"), true);
94+
xbps_object_release(a);
9195
}
9296

9397
ATF_TP_ADD_TCS(tp)

tests/xbps/libxbps/plist_match_virtual/main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pkgdict_init(void)
6464
ATF_REQUIRE(d != NULL);
6565

6666
a = provides_init();
67-
ATF_REQUIRE_EQ(xbps_dictionary_set(d, "provides", a), true);
67+
ATF_REQUIRE_EQ(xbps_dictionary_set_and_rel(d, "provides", a), true);
6868

6969
return d;
7070
}
@@ -83,6 +83,7 @@ ATF_TC_BODY(match_virtual_pkg_dict_test, tc)
8383
ATF_REQUIRE_EQ(xbps_match_virtual_pkg_in_dict(d, "cron-daemon"), true);
8484
ATF_REQUIRE_EQ(xbps_match_virtual_pkg_in_dict(d, "cron-daemon>=0"), true);
8585
ATF_REQUIRE_EQ(xbps_match_virtual_pkg_in_dict(d, "cron-daemon>2"), false);
86+
xbps_object_release(d);
8687
}
8788

8889
ATF_TC(match_any_virtualpkg_rundeps_test);
@@ -100,6 +101,9 @@ ATF_TC_BODY(match_any_virtualpkg_rundeps_test, tc)
100101

101102
ATF_REQUIRE_EQ(
102103
xbps_match_any_virtualpkg_in_rundeps(rundeps, provides), true);
104+
105+
xbps_object_release(provides);
106+
xbps_object_release(rundeps);
103107
}
104108

105109
ATF_TP_ADD_TCS(tp)

tests/xbps/libxbps/util/main.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2424
*-
2525
*/
26+
#include <stdlib.h>
2627
#include <string.h>
2728
#include <atf-c.h>
2829
#include <xbps.h>
@@ -37,6 +38,7 @@ ATF_TC_HEAD(util_test, tc)
3738
ATF_TC_BODY(util_test, tc)
3839
{
3940
char name[XBPS_NAME_SIZE];
41+
char *s;
4042

4143
ATF_CHECK_EQ(xbps_pkg_name(name, sizeof(name), "font-adobe-a"), false);
4244
ATF_CHECK_EQ(xbps_pkg_name(name, sizeof(name), "font-adobe-1"), false);
@@ -81,18 +83,28 @@ ATF_TC_BODY(util_test, tc)
8183
ATF_REQUIRE_STREQ(name, "systemd");
8284
ATF_CHECK_EQ(xbps_pkgpattern_name(name, sizeof(name), "*nslookup"), false);
8385

84-
ATF_REQUIRE_STREQ(xbps_binpkg_arch("/path/to/foo-1.0_1.x86_64.xbps"), "x86_64");
85-
ATF_REQUIRE_STREQ(xbps_binpkg_arch("/path/to/foo-1.0_1.x86_64-musl.xbps"), "x86_64-musl");
86-
ATF_REQUIRE_STREQ(xbps_binpkg_arch("foo-1.0_1.x86_64-musl.xbps"), "x86_64-musl");
87-
ATF_REQUIRE_STREQ(xbps_binpkg_arch("foo-1.0_1.x86_64.xbps"), "x86_64");
86+
ATF_REQUIRE_STREQ((s = xbps_binpkg_arch("/path/to/foo-1.0_1.x86_64.xbps")), "x86_64");
87+
free(s);
88+
ATF_REQUIRE_STREQ((s = xbps_binpkg_arch("/path/to/foo-1.0_1.x86_64-musl.xbps")), "x86_64-musl");
89+
free(s);
90+
ATF_REQUIRE_STREQ((s = xbps_binpkg_arch("foo-1.0_1.x86_64-musl.xbps")), "x86_64-musl");
91+
free(s);
92+
ATF_REQUIRE_STREQ((s = xbps_binpkg_arch("foo-1.0_1.x86_64.xbps")), "x86_64");
93+
free(s);
8894

89-
ATF_REQUIRE_STREQ(xbps_binpkg_pkgver("foo-1.0_1.x86_64.xbps"), "foo-1.0_1");
90-
ATF_REQUIRE_STREQ(xbps_binpkg_pkgver("foo-1.0_1.x86_64-musl.xbps"), "foo-1.0_1");
91-
ATF_REQUIRE_STREQ(xbps_binpkg_pkgver("/path/to/foo-1.0_1.x86_64.xbps"), "foo-1.0_1");
92-
ATF_REQUIRE_STREQ(xbps_binpkg_pkgver("/path/to/foo-1.0_1.x86_64-musl.xbps"), "foo-1.0_1");
95+
ATF_REQUIRE_STREQ((s = xbps_binpkg_pkgver("foo-1.0_1.x86_64.xbps")), "foo-1.0_1");
96+
free(s);
97+
ATF_REQUIRE_STREQ((s = xbps_binpkg_pkgver("foo-1.0_1.x86_64-musl.xbps")), "foo-1.0_1");
98+
free(s);
99+
ATF_REQUIRE_STREQ((s = xbps_binpkg_pkgver("/path/to/foo-1.0_1.x86_64.xbps")), "foo-1.0_1");
100+
free(s);
101+
ATF_REQUIRE_STREQ((s = xbps_binpkg_pkgver("/path/to/foo-1.0_1.x86_64-musl.xbps")), "foo-1.0_1");
102+
free(s);
93103

94-
ATF_CHECK_EQ(xbps_binpkg_pkgver("foo-1.0.x86_64.xbps"), NULL);
95-
ATF_CHECK_EQ(xbps_binpkg_pkgver("foo-1.0.x86_64"), NULL);
104+
ATF_CHECK_EQ((s = xbps_binpkg_pkgver("foo-1.0.x86_64.xbps")), NULL);
105+
free(s);
106+
ATF_CHECK_EQ((s = xbps_binpkg_pkgver("foo-1.0.x86_64")), NULL);
107+
free(s);
96108
}
97109

98110
ATF_TP_ADD_TCS(tp)

0 commit comments

Comments
 (0)