From: Gerhard Sittig Date: Fri, 9 Feb 2018 18:17:13 +0000 (+0100) Subject: motech-lps-30x: fix several compiler warnings X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=e4924d752b3a2e08d0ab90b0eb872c569c4c68c9;p=libsigrok.git motech-lps-30x: fix several compiler warnings Check pointers' validity before dereferencing them. Explicitly assign a default value to variables, before conversion routines conditionally assign the "real" value (and don't in case of conversion errors). This avoids processing "garbage" data. Strictly speaking I cannot see how the conversion routine returns OK and has _not_ assigned a result. But the explicit assignment won't harm either, and matches the fallback when the conversion fails (detectibly). Which means that runtime behaviour won't change. This was reported by clang's scan-build. --- diff --git a/src/hardware/motech-lps-30x/api.c b/src/hardware/motech-lps-30x/api.c index 0c581789..af3ee4f0 100644 --- a/src/hardware/motech-lps-30x/api.c +++ b/src/hardware/motech-lps-30x/api.c @@ -650,6 +650,8 @@ static int config_list(uint32_t key, GVariant **data, case SR_CONF_DEVICE_OPTIONS: return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); case SR_CONF_CHANNEL_CONFIG: + if (!devc || !devc->model) + return SR_ERR_ARG; if (devc->model->modelid <= LPS_303) { /* The 1-channel models. */ *data = g_variant_new_strv(channel_modes, 1); @@ -675,9 +677,13 @@ static int config_list(uint32_t key, GVariant **data, *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_ch3)); break; case SR_CONF_VOLTAGE_TARGET: + if (!devc || !devc->model) + return SR_ERR_ARG; *data = std_gvar_min_max_step_array(devc->model->channels[ch_idx].voltage); break; case SR_CONF_CURRENT_LIMIT: + if (!devc || !devc->model) + return SR_ERR_ARG; *data = std_gvar_min_max_step_array(devc->model->channels[ch_idx].current); break; default: diff --git a/src/hardware/motech-lps-30x/protocol.c b/src/hardware/motech-lps-30x/protocol.c index ce097274..b31f2ee1 100644 --- a/src/hardware/motech-lps-30x/protocol.c +++ b/src/hardware/motech-lps-30x/protocol.c @@ -76,6 +76,8 @@ static void process_line(struct sr_dev_inst *sdi) int auxint; devc = sdi->priv; + if (!devc) + return; switch (devc->acq_req_pending) { case 0: /* Should not happen... */ @@ -87,6 +89,7 @@ static void process_line(struct sr_dev_inst *sdi) case AQ_U2: case AQ_I1: case AQ_I2: + dbl = 0.0; if (sr_atod_ascii(devc->buf, &dbl) != SR_OK) { sr_err("Failed to convert '%s' to double, errno=%d %s", devc->buf, errno, g_strerror(errno)); @@ -94,6 +97,7 @@ static void process_line(struct sr_dev_inst *sdi) } break; case AQ_STATUS: + auxint = 0; if (sr_atoi(devc->buf, &auxint) != SR_OK) { sr_err("Failed to convert '%s' to int, errno=%d %s", devc->buf, errno, g_strerror(errno));