From: Wolfram Sang Date: Sun, 21 Feb 2016 09:06:10 +0000 (+0100) Subject: session: allocate correct amount of memory in sr_packet_copy X-Git-Tag: libsigrok-0.5.0~559 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=b84f91ff2ea924c5662d38034b3754d9a37265d4;p=libsigrok.git session: allocate correct amount of memory in sr_packet_copy We want the size of the struct, not of a pointer to the struct. And to be absolutely future proof, dereference the pointer we are assigning the memory to (not the one we are copying the data from). Found by Coverity, CID 50858. Signed-off-by: Wolfram Sang --- diff --git a/src/session.c b/src/session.c index 4aeddcc2..5a68c8df 100644 --- a/src/session.c +++ b/src/session.c @@ -1525,7 +1525,7 @@ SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet, break; case SR_DF_LOGIC: logic = packet->payload; - logic_copy = g_malloc(sizeof(logic)); + logic_copy = g_malloc(sizeof(*logic_copy)); logic_copy->length = logic->length; logic_copy->unitsize = logic->unitsize; memcpy(logic_copy->data, logic->data, logic->length * logic->unitsize); @@ -1533,7 +1533,7 @@ SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet, break; case SR_DF_ANALOG_OLD: analog_old = packet->payload; - analog_old_copy = g_malloc(sizeof(analog_old)); + analog_old_copy = g_malloc(sizeof(*analog_old_copy)); analog_old_copy->channels = g_slist_copy(analog_old->channels); analog_old_copy->num_samples = analog_old->num_samples; analog_old_copy->mq = analog_old->mq; @@ -1546,7 +1546,7 @@ SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet, break; case SR_DF_ANALOG: analog = packet->payload; - analog_copy = g_malloc(sizeof(analog)); + analog_copy = g_malloc(sizeof(*analog_copy)); analog_copy->data = g_malloc( analog->encoding->unitsize * analog->num_samples); memcpy(analog_copy->data, analog->data,