summaryrefslogtreecommitdiff
path: root/test/libapt
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2015-06-18 17:33:15 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2015-08-10 17:25:25 +0200
commit3d8232bf97ce11818fb07813a71136484ea1a44a (patch)
treec7e1e3885e952f7ab8171e1cf4b425ddccb5606f /test/libapt
parentc3392a9fccc04129816057b1184c651171034376 (diff)
fix memory leaks reported by -fsanitize
Various small leaks here and there. Nothing particularily big, but still good to fix. Found by the sanitizers while running our testcases. Reported-By: gcc -fsanitize Git-Dch: Ignore
Diffstat (limited to 'test/libapt')
-rw-r--r--test/libapt/cdrom_test.cc7
-rw-r--r--test/libapt/file-helpers.cc7
-rw-r--r--test/libapt/file-helpers.h2
-rw-r--r--test/libapt/sourcelist_test.cc5
4 files changed, 10 insertions, 11 deletions
diff --git a/test/libapt/cdrom_test.cc b/test/libapt/cdrom_test.cc
index 7257eaf1b..b4c51cdb0 100644
--- a/test/libapt/cdrom_test.cc
+++ b/test/libapt/cdrom_test.cc
@@ -91,7 +91,7 @@ TEST(CDROMTest,ReduceSourcelist)
}
TEST(CDROMTest, FindMountPointForDevice)
{
- char * tempfile = NULL;
+ std::string tempfile;
FileFd fd;
createTemporaryFile("mountpoints", fd, &tempfile,
"rootfs / rootfs rw 0 0\n"
@@ -109,7 +109,6 @@ TEST(CDROMTest, FindMountPointForDevice)
EXPECT_EQ("/boot/efi", FindMountPointForDevice("/dev/sda1"));
EXPECT_EQ("/tmp", FindMountPointForDevice("tmpfs"));
- if (tempfile != NULL)
- unlink(tempfile);
- free(tempfile);
+ if (tempfile.empty() == false)
+ unlink(tempfile.c_str());
}
diff --git a/test/libapt/file-helpers.cc b/test/libapt/file-helpers.cc
index 5edb9a9fe..387192868 100644
--- a/test/libapt/file-helpers.cc
+++ b/test/libapt/file-helpers.cc
@@ -53,20 +53,19 @@ void helperCreateLink(std::string const &dir, std::string const &targetname, std
link.append(linkname);
ASSERT_EQ(0, symlink(target.c_str(), link.c_str()));
}
-void helperCreateTemporaryFile(std::string const &id, FileFd &fd, char * * const filename, char const * const content)
+void helperCreateTemporaryFile(std::string const &id, FileFd &fd, std::string * const filename, char const * const content)
{
std::string name("apt-test-");
name.append(id).append(".XXXXXXXX");
char * tempfile = strdup(name.c_str());
+ ASSERT_STRNE(NULL, tempfile);
int tempfile_fd = mkstemp(tempfile);
ASSERT_NE(-1, tempfile_fd);
if (filename != NULL)
*filename = tempfile;
else
- {
unlink(tempfile);
- free(tempfile);
- }
+ free(tempfile);
EXPECT_TRUE(fd.OpenDescriptor(tempfile_fd, FileFd::ReadWrite));
if (content != NULL)
diff --git a/test/libapt/file-helpers.h b/test/libapt/file-helpers.h
index e8472d503..f639c1cbc 100644
--- a/test/libapt/file-helpers.h
+++ b/test/libapt/file-helpers.h
@@ -24,6 +24,6 @@ void helperCreateDirectory(std::string const &dir, std::string const &name);
void helperCreateLink(std::string const &dir, std::string const &targetname, std::string const &linkname);
#define createTemporaryFile(id, fd, filename, content) \
ASSERT_NO_FATAL_FAILURE(helperCreateTemporaryFile(id, fd, filename, content))
-void helperCreateTemporaryFile(std::string const &id, FileFd &fd, char * * const filename, char const * const content);
+void helperCreateTemporaryFile(std::string const &id, FileFd &fd, std::string * const filename, char const * const content);
#endif
diff --git a/test/libapt/sourcelist_test.cc b/test/libapt/sourcelist_test.cc
index 747ab4957..9e6f82213 100644
--- a/test/libapt/sourcelist_test.cc
+++ b/test/libapt/sourcelist_test.cc
@@ -20,7 +20,7 @@ class SourceList : public pkgSourceList {
TEST(SourceListTest,ParseFileDeb822)
{
FileFd fd;
- char * tempfile = NULL;
+ std::string tempfile;
createTemporaryFile("parsefiledeb822", fd, &tempfile,
"Types: deb\n"
"URIs: http://ftp.debian.org/debian\n"
@@ -39,5 +39,6 @@ TEST(SourceListTest,ParseFileDeb822)
EXPECT_EQ(2, sources.ParseFileDeb822(tempfile));
EXPECT_EQ(2, sources.size());
- unlink(tempfile);
+ if (tempfile.empty() == false)
+ unlink(tempfile.c_str());
}