Refactor: Remove some unnecessary uses of memcpy

- Also, remove unnecessary "static" in gnucash-style.c

The second one in guid.cpp is UB:

libgnucash/engine/guid.cpp:137:5: warning: undefined behavior, source object type 'const gnc::GUID' is not TriviallyCopyable [bugprone-undefined-memory-manipulation]
        memcpy (&target, &source, sizeof (GncGUID));
        ^
This commit is contained in:
Richard Cohen
2023-02-07 16:48:17 +00:00
parent 4a60e01fcd
commit 3abc9a5558
10 changed files with 18 additions and 24 deletions

View File

@@ -731,7 +731,6 @@ if (UNIX OR MINGW)
set (HAVE_GETTIMEOFDAY 1) set (HAVE_GETTIMEOFDAY 1)
set (HAVE_GUILE 1) set (HAVE_GUILE 1)
set (HAVE_LIBM 1) set (HAVE_LIBM 1)
set (HAVE_MEMCPY 1)
set (STDC_HEADERS 1) set (STDC_HEADERS 1)
set (_ALL_SOURCE 1) set (_ALL_SOURCE 1)
set (_GNU_SOURCE 1) set (_GNU_SOURCE 1)

View File

@@ -132,9 +132,6 @@
/* Define to 1 if you have the <mcheck.h> header file. */ /* Define to 1 if you have the <mcheck.h> header file. */
#cmakedefine HAVE_MCHECK_H 1 #cmakedefine HAVE_MCHECK_H 1
/* Define to 1 if you have the `memcpy' function. */
#cmakedefine HAVE_MEMCPY 1
/* Define to 1 if you have the <memory.h> header file. */ /* Define to 1 if you have the <memory.h> header file. */
#cmakedefine HAVE_MEMORY_H 1 #cmakedefine HAVE_MEMORY_H 1

View File

@@ -61,9 +61,9 @@ static void sack_foreach_func(gpointer key, gpointer value, gpointer user_data)
sack_foreach_data_t data = (sack_foreach_data_t) user_data; sack_foreach_data_t data = (sack_foreach_data_t) user_data;
gnc_numeric thisvalue = *(gnc_numeric *) key; gnc_numeric thisvalue = *(gnc_numeric *) key;
gnc_numeric reachable_value = gnc_numeric_add_fixed (thisvalue, data->split_value); gnc_numeric reachable_value = gnc_numeric_add_fixed (thisvalue, data->split_value);
gpointer new_value = g_malloc(sizeof(gnc_numeric)); gnc_numeric* new_value = g_new(gnc_numeric, 1);
memcpy(new_value, &reachable_value, sizeof(gnc_numeric)); *new_value = reachable_value;
data->reachable_list = g_list_prepend(data->reachable_list, new_value); data->reachable_list = g_list_prepend(data->reachable_list, new_value);
} }
@@ -108,7 +108,7 @@ gnc_account_get_autoclear_splits (Account *account, gnc_numeric toclear_value,
{ {
Split *split = (Split *)node->data; Split *split = (Split *)node->data;
gnc_numeric split_value = xaccSplitGetAmount (split); gnc_numeric split_value = xaccSplitGetAmount (split);
gpointer new_value = g_malloc(sizeof(gnc_numeric)); gnc_numeric *new_value = g_new(gnc_numeric, 1);
struct _sack_foreach_data_t s_data[1]; struct _sack_foreach_data_t s_data[1];
s_data->split_value = split_value; s_data->split_value = split_value;
@@ -118,7 +118,7 @@ gnc_account_get_autoclear_splits (Account *account, gnc_numeric toclear_value,
g_hash_table_foreach (sack, sack_foreach_func, s_data); g_hash_table_foreach (sack, sack_foreach_func, s_data);
/* Add the value of the split itself to the reachable_list */ /* Add the value of the split itself to the reachable_list */
memcpy(new_value, &split_value, sizeof(gnc_numeric)); *new_value = split_value;
s_data->reachable_list = g_list_prepend s_data->reachable_list = g_list_prepend
(s_data->reachable_list, new_value); (s_data->reachable_list, new_value);

View File

@@ -56,12 +56,10 @@ style_get_key (SheetBlockStyle *style)
static gpointer static gpointer
style_create_key (SheetBlockStyle *style) style_create_key (SheetBlockStyle *style)
{ {
static gint key; gint key = style->cursor->num_rows;
gpointer new_key;
key = style->cursor->num_rows; gint *new_key = g_new(gint, 1);
new_key = g_malloc(sizeof(key)); *new_key = key;
new_key = memcpy(new_key, &key, sizeof(key));
return new_key; return new_key;
} }

View File

@@ -160,7 +160,7 @@ _retrieve_guid_ (gpointer pObject, gpointer pValue)
g_return_if_fail (pObject != NULL); g_return_if_fail (pObject != NULL);
g_return_if_fail (pValue != NULL); g_return_if_fail (pValue != NULL);
memcpy (pGuid, guid, sizeof (GncGUID)); *pGuid = *guid;
} }
/* Special column table because we need to be able to access the table by /* Special column table because we need to be able to access the table by

View File

@@ -649,7 +649,7 @@ _retrieve_guid_ (gpointer pObject, gpointer pValue)
g_return_if_fail (pObject != NULL); g_return_if_fail (pObject != NULL);
g_return_if_fail (pValue != NULL); g_return_if_fail (pValue != NULL);
memcpy (pGuid, guid, sizeof (GncGUID)); *pGuid = *guid;
} }
// Table to retrieve just the guid // Table to retrieve just the guid

View File

@@ -1118,8 +1118,8 @@ gnc_sx_clone_temporal_state (SXTmpStateData *tsd)
if(tsd) if(tsd)
{ {
toRet = g_malloc(sizeof(SXTmpStateData)); toRet = g_new(SXTmpStateData, 1);
toRet = memcpy (toRet, tsd, sizeof (SXTmpStateData)); *toRet = *tsd;
} }
return toRet; return toRet;

View File

@@ -33,7 +33,6 @@
#include <glib.h> #include <glib.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <string.h> /* for memcpy() */
#include <qofinstance-p.h> #include <qofinstance-p.h>
#include "gncCustomerP.h" #include "gncCustomerP.h"
@@ -399,7 +398,7 @@ void gncOwnerCopy (const GncOwner *src, GncOwner *dest)
{ {
if (!src || !dest) return; if (!src || !dest) return;
if (src == dest) return; if (src == dest) return;
memcpy (dest, src, sizeof (*dest)); *dest = *src;
} }
gboolean gncOwnerEqual (const GncOwner *a, const GncOwner *b) gboolean gncOwnerEqual (const GncOwner *a, const GncOwner *b)

View File

@@ -56,6 +56,7 @@
#include <boost/uuid/uuid_io.hpp> #include <boost/uuid/uuid_io.hpp>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <algorithm>
/* This static indicates the debugging module that this .o belongs to. */ /* This static indicates the debugging module that this .o belongs to. */
static QofLogModule log_module = QOF_MOD_ENGINE; static QofLogModule log_module = QOF_MOD_ENGINE;
@@ -120,7 +121,7 @@ guid_copy (const GncGUID *guid)
{ {
if (!guid) return nullptr; if (!guid) return nullptr;
auto ret = guid_malloc (); auto ret = guid_malloc ();
memcpy (ret, guid, sizeof (GncGUID)); *ret = *guid;
return ret; return ret;
} }
@@ -134,7 +135,7 @@ guid_null (void)
static void static void
guid_assign (GncGUID & target, gnc::GUID const & source) guid_assign (GncGUID & target, gnc::GUID const & source)
{ {
memcpy (&target, &source, sizeof (GncGUID)); std::copy (source.begin(), source.end(), target.reserved);
} }
/*Takes an allocated guid pointer and constructs it in place*/ /*Takes an allocated guid pointer and constructs it in place*/

View File

@@ -188,7 +188,7 @@ static QofQueryTerm * copy_query_term (const QofQueryTerm *qt)
if (!qt) return NULL; if (!qt) return NULL;
new_qt = g_new0 (QofQueryTerm, 1); new_qt = g_new0 (QofQueryTerm, 1);
memcpy (new_qt, qt, sizeof(QofQueryTerm)); *new_qt = *qt;
new_qt->param_list = g_slist_copy (qt->param_list); new_qt->param_list = g_slist_copy (qt->param_list);
new_qt->param_fcns = g_slist_copy (qt->param_fcns); new_qt->param_fcns = g_slist_copy (qt->param_fcns);
new_qt->pdata = qof_query_core_predicate_copy (qt->pdata); new_qt->pdata = qof_query_core_predicate_copy (qt->pdata);
@@ -224,7 +224,7 @@ copy_or_terms(const GList * or_terms)
static void copy_sort (QofQuerySort *dst, const QofQuerySort *src) static void copy_sort (QofQuerySort *dst, const QofQuerySort *src)
{ {
memcpy (dst, src, sizeof (*dst)); *dst = *src;
dst->param_list = g_slist_copy (src->param_list); dst->param_list = g_slist_copy (src->param_list);
dst->param_fcns = g_slist_copy (src->param_fcns); dst->param_fcns = g_slist_copy (src->param_fcns);
} }
@@ -1006,7 +1006,7 @@ QofQuery * qof_query_copy (QofQuery *q)
ht = copy->be_compiled; ht = copy->be_compiled;
free_members (copy); free_members (copy);
memcpy (copy, q, sizeof (QofQuery)); *copy = *q;
copy->be_compiled = ht; copy->be_compiled = ht;
copy->terms = copy_or_terms (q->terms); copy->terms = copy_or_terms (q->terms);