From: Aurelien Jacobs Date: Thu, 12 Feb 2015 10:14:37 +0000 (+0100) Subject: Correctly copy sr_datafeed_meta in sr_packet_copy(). X-Git-Tag: libsigrok-0.4.0~647 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=ee29d92e140a7b516b4ad24b2cd3047bfa5497cd Correctly copy sr_datafeed_meta in sr_packet_copy(). Commit 5801d558 replaced g_slist_copy_deep() by some incorrect code that actually leaks the newly allocated memory, instead of doing a deep copy. This new version should be more correct, more concise, and it fixes the following warning: src/session.c: In function 'sr_packet_copy': src/session.c:1025:38: warning: passing argument 2 of 'g_slist_foreach' from incompatible pointer type [-Wincompatible-pointer-types] g_slist_foreach(meta_copy->config, (GCopyFunc)copy_src, NULL); ^ In file included from /usr/include/glib-2.0/glib/gmain.h:26:0, from /usr/include/glib-2.0/glib/giochannel.h:33, from /usr/include/glib-2.0/glib.h:54, from src/session.c:24: /usr/include/glib-2.0/glib/gslist.h:125:10: note: expected 'GFunc {aka void (*)(void *, void *)}' but argument is of type 'void * (*)(const void *, void *)' void g_slist_foreach (GSList *list, ^ --- diff --git a/src/session.c b/src/session.c index 4ef3bd43..fd24c26c 100644 --- a/src/session.c +++ b/src/session.c @@ -983,15 +983,11 @@ SR_API int sr_session_source_remove_channel(struct sr_session *session, return _sr_session_source_remove(session, (gintptr)channel); } -static void *copy_src(struct sr_config *src) +static void copy_src(struct sr_config *src, struct sr_datafeed_meta *meta_copy) { - struct sr_config *new_src; - - new_src = g_malloc(sizeof(struct sr_config)); - memcpy(new_src, src, sizeof(struct sr_config)); g_variant_ref(src->data); - - return new_src; + meta_copy->config = g_slist_append(meta_copy->config, + g_memdup(src, sizeof(struct sr_config))); } SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet, @@ -1020,9 +1016,8 @@ SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet, break; case SR_DF_META: meta = packet->payload; - meta_copy = g_malloc(sizeof(struct sr_datafeed_meta)); - meta_copy->config = g_slist_copy(meta->config); - g_slist_foreach(meta_copy->config, (GCopyFunc)copy_src, NULL); + meta_copy = g_malloc0(sizeof(struct sr_datafeed_meta)); + g_slist_foreach(meta->config, (GFunc)copy_src, meta_copy->config); (*copy)->payload = meta_copy; break; case SR_DF_LOGIC: