]> sigrok.org Git - libsigrok.git/commitdiff
hameg-hmo: fix potential NULL dereference
authorGerhard Sittig <redacted>
Sat, 10 Feb 2018 11:20:05 +0000 (12:20 +0100)
committerGerhard Sittig <redacted>
Sat, 10 Feb 2018 14:34:20 +0000 (15:34 +0100)
Check for successful allocation before accessing struct members. Return
with an error from device initialization when allocation fails.

This was reported by clang's scan-build.

src/hardware/hameg-hmo/protocol.c

index e79901caaf37ee447f53119ae87d759005b14543..059ae4399cc612f241b6b5d0dff7e3e1f9ec6994 100644 (file)
@@ -664,6 +664,7 @@ SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
        unsigned int i, j, group;
        struct sr_channel *ch;
        struct dev_context *devc;
+       int ret;
 
        devc = sdi->priv;
        model_index = -1;
@@ -711,12 +712,19 @@ SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
        }
 
        /* Add digital channel groups. */
+       ret = SR_OK;
        for (i = 0; i < scope_models[model_index].digital_pods; i++) {
                devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
+               if (!devc->digital_groups[i]) {
+                       ret = SR_ERR_MALLOC;
+                       break;
+               }
                devc->digital_groups[i]->name = g_strdup_printf("POD%d", i);
                sdi->channel_groups = g_slist_append(sdi->channel_groups,
                                   devc->digital_groups[i]);
        }
+       if (ret != SR_OK)
+               return ret;
 
        /* Add digital channels. */
        for (i = 0; i < scope_models[model_index].digital_channels; i++) {