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.
This commit is contained in:
Gerhard Sittig 2018-02-09 19:09:27 +01:00 committed by Uwe Hermann
parent fc69eecca0
commit b65adcedb8
1 changed files with 6 additions and 3 deletions

View 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)