mirror of
https://github.com/Gnucash/gnucash.git
synced 2024-11-25 10:20:18 -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:
parent
0d86be6d2a
commit
6be682b645
@ -122,7 +122,7 @@ private:
|
||||
class GncFQQuoteSource final : public GncQuoteSource
|
||||
{
|
||||
const bfs::path c_cmd;
|
||||
const std::string c_fq_wrapper;
|
||||
std::string c_fq_wrapper;
|
||||
std::string m_version;
|
||||
StrVec m_sources;
|
||||
std::string m_api_key;
|
||||
@ -145,11 +145,12 @@ static const std::string empty_string{};
|
||||
|
||||
GncFQQuoteSource::GncFQQuoteSource() :
|
||||
c_cmd{bp::search_path("perl")},
|
||||
c_fq_wrapper{std::string(gnc_path_get_bindir()) + "/finance-quote-wrapper"},
|
||||
m_version{}, m_sources{}, m_api_key{}
|
||||
{
|
||||
char *bindir = gnc_path_get_bindir();
|
||||
c_fq_wrapper = std::string(bindir) + "/finance-quote-wrapper";
|
||||
g_free(bindir);
|
||||
StrVec args{"-w", c_fq_wrapper, "-v"};
|
||||
const std::string empty_string;
|
||||
auto [rv, sources, errors] = run_cmd(args, empty_string);
|
||||
if (rv)
|
||||
{
|
||||
|
@ -101,8 +101,7 @@ protected:
|
||||
gnc_commodity_set_quote_source(eur, source);
|
||||
gnc_commodity_commit_edit(eur);
|
||||
gnc_commodity_table_insert(comm_table, eur);
|
||||
auto usd = gnc_commodity_new(m_book, "United States Dollar", "CURRENCY",
|
||||
"USD", NULL, 100);
|
||||
auto usd = gnc_commodity_new(m_book, "United States Dollar", "CURRENCY", "USD", NULL, 100);
|
||||
gnc_commodity_table_insert(comm_table, usd);
|
||||
source = gnc_quote_source_lookup_by_internal("yahoo_json");
|
||||
auto aapl = gnc_commodity_new(m_book, "Apple", "NASDAQ", "AAPL", NULL, 1);
|
||||
@ -111,8 +110,7 @@ protected:
|
||||
gnc_commodity_set_quote_source(aapl, source);
|
||||
gnc_commodity_commit_edit(aapl);
|
||||
gnc_commodity_table_insert(comm_table, aapl);
|
||||
auto hpe = gnc_commodity_new(m_book, "Hewlett Packard", "NYSE", "HPE",
|
||||
NULL, 1);
|
||||
auto hpe = gnc_commodity_new(m_book, "Hewlett Packard", "NYSE", "HPE", NULL, 1);
|
||||
gnc_commodity_begin_edit(hpe);
|
||||
gnc_commodity_set_quote_flag(hpe, TRUE);
|
||||
gnc_commodity_set_quote_source(hpe, source);
|
||||
@ -124,7 +122,9 @@ protected:
|
||||
gnc_commodity_set_quote_source(fkcm, source);
|
||||
gnc_commodity_commit_edit(fkcm);
|
||||
gnc_commodity_table_insert(comm_table, fkcm);
|
||||
gnc_quote_source_set_fq_installed("TestSuite", g_list_prepend(nullptr, (void*)"yahoo_json"));
|
||||
GList *sources = g_list_prepend(nullptr, (void*)"yahoo_json");
|
||||
gnc_quote_source_set_fq_installed("TestSuite", sources);
|
||||
g_list_free(sources);
|
||||
}
|
||||
~GncQuotesTest() {
|
||||
gnc_clear_current_session();
|
||||
|
@ -99,9 +99,11 @@ GncSqlColumnTableEntry::add_objectref_guid_to_query (QofIdTypeConst obj_name,
|
||||
auto inst = get_row_value_from_object<QofInstance*>(obj_name, pObject);
|
||||
if (inst == nullptr) return;
|
||||
auto guid = qof_instance_get_guid (inst);
|
||||
if (guid != nullptr)
|
||||
vec.emplace_back (std::make_pair (std::string{m_col_name},
|
||||
quote_string(guid_to_string(guid))));
|
||||
if (guid != nullptr) {
|
||||
gchar *guid_s = guid_to_string(guid);
|
||||
vec.emplace_back (std::make_pair (std::string{m_col_name}, quote_string(guid_s)));
|
||||
g_free(guid_s);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
@ -356,9 +358,9 @@ GncSqlColumnTableEntryImpl<CT_GUID>::add_to_query(QofIdTypeConst obj_name,
|
||||
|
||||
if (s != nullptr)
|
||||
{
|
||||
|
||||
vec.emplace_back (std::make_pair (std::string{m_col_name},
|
||||
quote_string(guid_to_string(s))));
|
||||
gchar *guid_s = guid_to_string(s);
|
||||
vec.emplace_back (std::make_pair (std::string{m_col_name}, quote_string(guid_s)));
|
||||
g_free(guid_s);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -28,9 +28,13 @@
|
||||
#include <guid.hpp>
|
||||
#include <glib.h>
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include <config.h>
|
||||
#include <ctype.h>
|
||||
#include "cashobjects.h"
|
||||
#include "qofid.h"
|
||||
#include "test-stuff.h"
|
||||
#include "test-engine-stuff.h"
|
||||
#include "qof.h"
|
||||
@ -50,13 +54,17 @@ static void test_null_guid(void)
|
||||
guid_free(gp);
|
||||
}
|
||||
|
||||
static void
|
||||
free_entry (QofInstance* inst, void* data)
|
||||
{
|
||||
g_object_unref (G_OBJECT(inst));
|
||||
}
|
||||
|
||||
static void
|
||||
run_test (void)
|
||||
{
|
||||
int i;
|
||||
QofSession *sess;
|
||||
QofBook *book;
|
||||
QofInstance *ent;
|
||||
QofCollection *col;
|
||||
QofIdType type;
|
||||
GncGUID guid;
|
||||
@ -68,12 +76,10 @@ run_test (void)
|
||||
col = qof_book_get_collection (book, "asdf");
|
||||
type = qof_collection_get_type (col);
|
||||
|
||||
for (i = 0; i < NENT; i++)
|
||||
for (int i = 0; i < NENT; i++)
|
||||
{
|
||||
ent = static_cast<QofInstance*>(g_object_new(QOF_TYPE_INSTANCE, NULL));
|
||||
guid_replace(&guid);
|
||||
ent = static_cast<QofInstance*>(g_object_new(QOF_TYPE_INSTANCE,
|
||||
"guid", &guid, NULL));
|
||||
auto ent = QOF_INSTANCE(g_object_new(QOF_TYPE_INSTANCE, "guid", &guid, NULL));
|
||||
do_test ((NULL == qof_collection_lookup_entity (col, &guid)),
|
||||
"duplicate guid");
|
||||
ent->e_type = type;
|
||||
@ -83,6 +89,7 @@ run_test (void)
|
||||
}
|
||||
|
||||
/* Make valgrind happy -- destroy the session. */
|
||||
qof_collection_foreach(col, free_entry, nullptr);
|
||||
qof_session_destroy(sess);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user