From: Gerhard Sittig Date: Fri, 9 Feb 2018 18:09:27 +0000 (+0100) Subject: lascar-el-usb: fix potential NULL dereference and memory leak X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=b65adcedb89f69c7f52a6e98e73a3513c2648e95;p=libsigrok.git lascar-el-usb: fix potential NULL dereference and memory leak Check for successful allocation of multiple memory ranges, and release partial allocations in the error path when one of them failed. This fixes a potential memory leak, as well as avoids NULL dereferences. This was reported by clang's scan-build. --- diff --git a/src/hardware/lascar-el-usb/protocol.c b/src/hardware/lascar-el-usb/protocol.c index 44f4be82..dcd83e8e 100644 --- a/src/hardware/lascar-el-usb/protocol.c +++ b/src/hardware/lascar-el-usb/protocol.c @@ -412,10 +412,13 @@ static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf, packet.type = SR_DF_ANALOG; packet.payload = &analog; analog.meaning->mqflags = 0; - if (!(temp = g_try_malloc(sizeof(float) * samples))) - break; - if (!(rh = g_try_malloc(sizeof(float) * samples))) + temp = g_try_malloc(sizeof(float) * samples); + rh = g_try_malloc(sizeof(float) * samples); + if (!temp || !rh) { + g_free(temp); + g_free(rh); break; + } for (i = 0, j = 0; i < samples; i++) { /* Both Celsius and Fahrenheit stored at base -40. */ if (devc->temp_unit == 0)