]> sigrok.org Git - libsigrok.git/commitdiff
lascar-el-usb: fix potential NULL dereference and memory leak
authorGerhard Sittig <redacted>
Fri, 9 Feb 2018 18:09:27 +0000 (19:09 +0100)
committerUwe Hermann <redacted>
Fri, 9 Feb 2018 20:37:39 +0000 (21:37 +0100)
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.

src/hardware/lascar-el-usb/protocol.c

index 44f4be82f350cca5d1e33b4d03c5c5fef8cb3a20..dcd83e8e035421c1fc34e0d0afd24d76d17ecc8b 100644 (file)
@@ -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)