Fix dereference of expired temporaries.

Reported by clang 15.0.7, which will be in Ubuntu 23.04, -Wdangling-gsl
This commit is contained in:
Richard Cohen 2023-03-28 13:53:57 +01:00 committed by John Ralls
parent 17e08d0f43
commit 6ba5bb326a
6 changed files with 21 additions and 14 deletions

View File

@ -261,13 +261,10 @@ equals_node_val_vs_kvp_frame (xmlNodePtr node, const KvpFrame* frm)
} }
else else
{ {
auto frm1str = g_strdup (frm->to_string ().c_str ()); auto frmstr = frm->to_string ();
auto frm2str = g_strdup (cmpfrm->to_string ().c_str ()); auto cmpfrmstr = cmpfrm->to_string ();
printf ("%s vs %s\n", frm1str, frm2str); printf ("%s vs %s\n", frmstr.c_str(), cmpfrmstr.c_str());
g_free (frm1str);
g_free (frm2str);
delete cmpfrm; delete cmpfrm;
return FALSE; return FALSE;

View File

@ -211,7 +211,10 @@ gchar *gnc_file_path_relative_part (const gchar *prefix, const gchar *path)
{ {
std::string p{path}; std::string p{path};
if (p.find(prefix) == 0) if (p.find(prefix) == 0)
return g_strdup(p.substr(strlen(prefix)).c_str()); {
auto str = p.substr(strlen(prefix));
return g_strdup(str.c_str());
}
return g_strdup(path); return g_strdup(path);
} }

View File

@ -1101,7 +1101,8 @@ qof_strftime(gchar *buf, gsize max, const gchar *format, const struct tm *tm)
gchar * gchar *
gnc_date_timestamp (void) gnc_date_timestamp (void)
{ {
return g_strdup(GncDateTime::timestamp().c_str()); auto timestamp = GncDateTime::timestamp();
return g_strdup(timestamp.c_str());
} }
/********************************************************************\ /********************************************************************\

View File

@ -65,10 +65,13 @@ header = N_("This Dataset contains features not supported "
gchar *gnc_features_test_unknown (QofBook *book) gchar *gnc_features_test_unknown (QofBook *book)
{ {
auto unknowns {qof_book_get_unknown_features (book, features_table)}; auto unknowns {qof_book_get_unknown_features (book, features_table)};
if (unknowns.empty())
return nullptr;
auto accum = [](const auto& a, const auto& b){ return a + "\n* " + b; }; auto accum = [](const auto& a, const auto& b){ return a + "\n* " + b; };
return unknowns.empty() ? nullptr : auto msg {std::accumulate (unknowns.begin(), unknowns.end(),
g_strdup (std::accumulate (unknowns.begin(), unknowns.end(), std::string (_(header)), accum)};
std::string (_(header)), accum).c_str()); return g_strdup (msg.c_str());
} }
void gnc_features_set_used (QofBook *book, const gchar *feature) void gnc_features_set_used (QofBook *book, const gchar *feature)

View File

@ -459,7 +459,10 @@ GncOptionDB::save_to_kvp(QofBook* book, bool clear_options) const noexcept
* have to store. */ * have to store. */
kvp = new KvpValue(option.template get_value<double>()); kvp = new KvpValue(option.template get_value<double>());
else else
kvp = new KvpValue{g_strdup(option.template get_value<std::string>().c_str())}; {
auto str{option.template get_value<std::string>()};
kvp = new KvpValue{g_strdup(str.c_str())};
}
qof_book_set_option(book, kvp, &list_head); qof_book_set_option(book, kvp, &list_head);
} }
}); });

View File

@ -1102,8 +1102,8 @@ qof_instance_compare_kvp (const QofInstance *a, const QofInstance *b)
char* char*
qof_instance_kvp_as_string (const QofInstance *inst) qof_instance_kvp_as_string (const QofInstance *inst)
{ {
//The std::string is a local temporary and doesn't survive this function. auto str{inst->kvp_data->to_string()};
return g_strdup(inst->kvp_data->to_string().c_str()); return g_strdup(str.c_str());
} }
void void