Skip to content

Commit

Permalink
Merge Maarten Bosmans's 'memleak-fixes' into stable.
Browse files Browse the repository at this point in the history
  • Loading branch information
jralls committed Apr 29, 2023
2 parents 168765c + 71802b5 commit df878c6
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 117 deletions.
7 changes: 4 additions & 3 deletions libgnucash/app-utils/gnc-quotes.cpp
Expand Up @@ -123,7 +123,7 @@ class GncQuotesImpl
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;
Expand All @@ -146,11 +146,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)
{
Expand Down
23 changes: 16 additions & 7 deletions libgnucash/app-utils/test/gtest-gnc-quotes.cpp
Expand Up @@ -90,19 +90,23 @@ class GncQuotesTest : public ::testing::Test
m_book{qof_session_get_book(gnc_get_current_session())}
{
qof_init();
gnc_commodity_table_register();
gnc_pricedb_register();

/* By setting an empty commodity table on the book before registering
* the commodity_table type we avoid adding the default commodities */
auto comm_table{gnc_commodity_table_new()};
qof_book_set_data(m_book, GNC_COMMODITY_TABLE, comm_table);

gnc_commodity_table_register();
gnc_pricedb_register();

auto eur = gnc_commodity_new(m_book, "Euro", "ISO4217", "EUR", NULL, 100);
auto source{gnc_quote_source_lookup_by_internal("currency")};
gnc_commodity_begin_edit(eur);
gnc_commodity_set_quote_flag(eur, TRUE);
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);
Expand All @@ -111,8 +115,7 @@ class GncQuotesTest : public ::testing::Test
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);
Expand All @@ -124,10 +127,13 @@ class GncQuotesTest : public ::testing::Test
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();
qof_close();
}

QofSession* m_session;
Expand Down Expand Up @@ -269,6 +275,7 @@ TEST_F(GncQuotesTest, fetch_one_commodity)
gnc_price_get_value(price)));
EXPECT_STREQ("Finance::Quote", gnc_price_get_source_string(price));
EXPECT_STREQ("last", gnc_price_get_typestr(price));
gnc_price_unref(price);
}

TEST_F(GncQuotesTest, fetch_one_currency)
Expand Down Expand Up @@ -299,6 +306,7 @@ TEST_F(GncQuotesTest, fetch_one_currency)
gnc_price_get_value(price)));
EXPECT_STREQ("Finance::Quote", gnc_price_get_source_string(price));
EXPECT_STREQ("last", gnc_price_get_typestr(price));
gnc_price_unref(price);
}

TEST_F(GncQuotesTest, no_currency)
Expand Down Expand Up @@ -365,6 +373,7 @@ TEST_F(GncQuotesTest, no_date)
gnc_price_get_value(price)));
EXPECT_STREQ("Finance::Quote", gnc_price_get_source_string(price));
EXPECT_STREQ("last", gnc_price_get_typestr(price));
gnc_price_unref(price);
}

TEST_F(GncQuotesTest, test_version)
Expand Down
14 changes: 8 additions & 6 deletions libgnucash/backend/sql/gnc-sql-column-table-entry.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand Down
4 changes: 0 additions & 4 deletions libgnucash/backend/xml/sixtp-dom-generators.cpp
Expand Up @@ -205,12 +205,8 @@ double_to_string (double value)
static void
add_text_to_node (xmlNodePtr node, const gchar* type, gchar* val)
{
gchar* newtype = g_strdup (type);
gchar* newval = g_strdup (val);
xmlSetProp (node, BAD_CAST "type", BAD_CAST type);
xmlNodeSetContent (node, checked_char_cast (val));
g_free (newtype);
g_free (newval);
}

static void add_kvp_slot (const char* key, KvpValue* value, void* data);
Expand Down
39 changes: 24 additions & 15 deletions libgnucash/core-utils/test/gtest-path-utilities.cpp
Expand Up @@ -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} {}
Expand All @@ -26,21 +32,24 @@ struct PathTest : public testing::Test
{
if (m_prefix)
g_free(m_prefix);
/* Clear the statically allocated exe string */
gnc_gbr_set_exe(NULL);
}
char *m_prefix;
};


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
}

Expand All @@ -50,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
}

Expand All @@ -69,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
}

Expand All @@ -88,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
}

Expand All @@ -107,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
}
Expand Down
20 changes: 0 additions & 20 deletions libgnucash/engine/kvp-value.cpp
Expand Up @@ -99,26 +99,6 @@ KvpValueImpl::get_type() const noexcept
return KvpValue::Type::INVALID;
}

KvpFrame *
KvpValueImpl::replace_frame_nc (KvpFrame * new_value) noexcept
{
if (datastore.type() != type_id<KvpFrame *>())
return {};
auto ret = boost::get<KvpFrame *>(datastore);
datastore = new_value;
return ret;
}

GList *
KvpValueImpl::replace_glist_nc (GList * new_value) noexcept
{
if (datastore.type() != type_id<GList *>())
return {};
auto ret = boost::get<GList *>(datastore);
datastore = new_value;
return ret;
}

struct to_string_visitor : boost::static_visitor<void>
{
std::ostringstream & output;
Expand Down
16 changes: 0 additions & 16 deletions libgnucash/engine/kvp-value.hpp
Expand Up @@ -100,22 +100,6 @@ struct KvpValueImpl
*/
~KvpValueImpl() noexcept;

/**
* Replaces the frame within this KvpValueImpl.
*
* If this KvpValueImpl doesn't contain a KvpFrame, nullptr
* is returned. Otherwise, the old KvpFrame * is returned.
*/
KvpFrame * replace_frame_nc (KvpFrame *) noexcept;

/**
* Replaces the glist within this KvpValueImpl.
*
* If this KvpValueImpl doesn't contain a GList, nullptr
* is returned. Otherwise, the old GList * is returned.
*/
GList * replace_glist_nc (GList *) noexcept;

/**
* Adds another value to this KvpValueImpl.
*
Expand Down

0 comments on commit df878c6

Please sign in to comment.