mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Store allocated temporaries in a variable so they can be freed
If a function that returns an allocated pointer is passed directly into something that does not take ownership of the pointer, the allocation is leaked. This can be fixed by assigning the pointer to a new variable and freeing it after operation on the memory.
This commit is contained in:
committed by
John Ralls
parent
0d86be6d2a
commit
6be682b645
@@ -8,6 +8,12 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
|
||||
/* Variant of EXPECT_STREQ that calls g_free()
|
||||
* on its first argument after the check */
|
||||
#define EXPECT_STREQ_GFREE(a, b) do { char *p_; EXPECT_STREQ(p_ = (a), (b)); g_free(p_); } while (0)
|
||||
|
||||
|
||||
struct PathTest : public testing::Test
|
||||
{
|
||||
PathTest() : m_prefix{nullptr} {}
|
||||
@@ -36,14 +42,14 @@ struct PathTest : public testing::Test
|
||||
TEST_F(PathTest, gnc_path_get_prefix)
|
||||
{
|
||||
#ifdef ENABLE_BINRELOC
|
||||
EXPECT_STREQ(gnc_path_get_prefix(), m_prefix);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_prefix(), m_prefix);
|
||||
#else
|
||||
g_setenv("GNC_UNINSTALLED", "1", TRUE);
|
||||
g_setenv("GNC_BUILDDIR", m_prefix, 1);
|
||||
EXPECT_STREQ(gnc_path_get_prefix(), m_prefix);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_prefix(), m_prefix);
|
||||
g_unsetenv("GNC_UNINSTALLED");
|
||||
g_unsetenv("GNC_BUILDDIR");
|
||||
EXPECT_STREQ(gnc_path_get_prefix(), PREFIX);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_prefix(), PREFIX);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -53,16 +59,16 @@ TEST_F(PathTest, gnc_path_get_bindir)
|
||||
gchar *binpath = g_build_filename(m_prefix, dirname, NULL);
|
||||
g_free(dirname);
|
||||
#ifdef ENABLE_BINRELOC
|
||||
EXPECT_STREQ(gnc_path_get_bindir(), binpath);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_bindir(), binpath);
|
||||
g_free(binpath);
|
||||
#else
|
||||
g_setenv("GNC_UNINSTALLED", "1", TRUE);
|
||||
g_setenv("GNC_BUILDDIR", m_prefix, 1);
|
||||
EXPECT_STREQ(gnc_path_get_bindir(), binpath);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_bindir(), binpath);
|
||||
g_free(binpath);
|
||||
g_unsetenv("GNC_UNINSTALLED");
|
||||
g_unsetenv("GNC_BUILDDIR");
|
||||
EXPECT_STREQ(gnc_path_get_bindir(), BINDIR);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_bindir(), BINDIR);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -72,16 +78,16 @@ TEST_F(PathTest, gnc_path_get_libdir)
|
||||
gchar *libpath = g_build_filename(m_prefix, dirname, NULL);
|
||||
g_free(dirname);
|
||||
#ifdef ENABLE_BINRELOC
|
||||
EXPECT_STREQ(gnc_path_get_libdir(), libpath);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_libdir(), libpath);
|
||||
g_free(libpath);
|
||||
#else
|
||||
g_setenv("GNC_UNINSTALLED", "1", TRUE);
|
||||
g_setenv("GNC_BUILDDIR", m_prefix, 1);
|
||||
EXPECT_STREQ(gnc_path_get_libdir(), libpath);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_libdir(), libpath);
|
||||
g_free(libpath);
|
||||
g_unsetenv("GNC_UNINSTALLED");
|
||||
g_unsetenv("GNC_BUILDDIR");
|
||||
EXPECT_STREQ(gnc_path_get_libdir(), LIBDIR);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_libdir(), LIBDIR);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -91,16 +97,16 @@ TEST_F(PathTest, gnc_path_get_datadir)
|
||||
gchar *datapath = g_build_filename(m_prefix, dirname, NULL);
|
||||
g_free(dirname);
|
||||
#ifdef ENABLE_BINRELOC
|
||||
EXPECT_STREQ(gnc_path_get_datadir(), datapath);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_datadir(), datapath);
|
||||
g_free(datapath);
|
||||
#else
|
||||
g_setenv("GNC_UNINSTALLED", "1", TRUE);
|
||||
g_setenv("GNC_BUILDDIR", m_prefix, 1);
|
||||
EXPECT_STREQ(gnc_path_get_datadir(), datapath);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_datadir(), datapath);
|
||||
g_free(datapath);
|
||||
g_unsetenv("GNC_UNINSTALLED");
|
||||
g_unsetenv("GNC_BUILDDIR");
|
||||
EXPECT_STREQ(gnc_path_get_datadir(), DATADIR);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_datadir(), DATADIR);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -110,17 +116,17 @@ TEST_F(PathTest, gnc_path_get_sysconfdir)
|
||||
gchar *sysconfpath = g_build_filename(m_prefix, dirname, PROJECT_NAME, NULL);
|
||||
g_free(dirname);
|
||||
#ifdef ENABLE_BINRELOC
|
||||
EXPECT_STREQ(gnc_path_get_pkgsysconfdir(), sysconfpath);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_pkgsysconfdir(), sysconfpath);
|
||||
g_free(sysconfpath);
|
||||
#else
|
||||
g_setenv("GNC_UNINSTALLED", "1", TRUE);
|
||||
g_setenv("GNC_BUILDDIR", m_prefix, 1);
|
||||
EXPECT_STREQ(gnc_path_get_pkgsysconfdir(), sysconfpath);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_pkgsysconfdir(), sysconfpath);
|
||||
g_free(sysconfpath);
|
||||
g_unsetenv("GNC_UNINSTALLED");
|
||||
g_unsetenv("GNC_BUILDDIR");
|
||||
sysconfpath = g_build_filename(SYSCONFDIR, PROJECT_NAME, NULL);
|
||||
EXPECT_STREQ(gnc_path_get_pkgsysconfdir(), sysconfpath);
|
||||
EXPECT_STREQ_GFREE(gnc_path_get_pkgsysconfdir(), sysconfpath);
|
||||
g_free(sysconfpath);
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user