From: Gerhard Sittig Date: Wed, 22 Dec 2021 13:01:42 +0000 (+0100) Subject: rigol-ds: improve robustness in samplerate getting code path X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=713a3f352496f928b9911bfdc7e87a67134113df rigol-ds: improve robustness in samplerate getting code path The rigol-ds driver's acquisition start routine tries to determine the samplerate, which depends on devices' protocol version and communication of SCPI requests. The logic potentially used an uninitialized variable (for protocol versions below V3). Rephrase the samplerate getting code path, apply stricter checks and extend diagnostics. Make sure the XINC retrieval did yield a non-zero value, to also avoid a division by zero. Address style issues while we are here. Use braces for all branches when one of them is complex. Eliminate trailing whitespace at ends of lines. src/hardware/rigol-ds/api.c:1029:7: warning: variable 'xinc' is used uninitialized whenever '&&' condition is false [-Wsometimes-uninitialized] if (devc->model->series->protocol >= PROTOCOL_V3 && ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src/hardware/rigol-ds/api.c:1034:28: note: uninitialized use occurs here devc->sample_rate = 1. / xinc; ^~~~ src/hardware/rigol-ds/api.c:1029:7: note: remove the '&&' if its condition is always true if (devc->model->series->protocol >= PROTOCOL_V3 && ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src/hardware/rigol-ds/api.c:1028:13: note: initialize the variable 'xinc' to silence this warning float xinc; ^ = 0.0 --- diff --git a/src/hardware/rigol-ds/api.c b/src/hardware/rigol-ds/api.c index 95de8f6a..632c9596 100644 --- a/src/hardware/rigol-ds/api.c +++ b/src/hardware/rigol-ds/api.c @@ -883,6 +883,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) GSList *l; char *cmd; int protocol; + int ret; scpi = sdi->conn; devc = sdi->priv; @@ -1021,20 +1022,27 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) devc->channel_entry = devc->enabled_channels; - if (devc->data_source == DATA_SOURCE_LIVE) - devc->sample_rate = analog_frame_size(sdi) / + if (devc->data_source == DATA_SOURCE_LIVE) { + devc->sample_rate = analog_frame_size(sdi) / (devc->timebase * devc->model->series->num_horizontal_divs); - else { + } else { float xinc; - if (devc->model->series->protocol >= PROTOCOL_V3 && - sr_scpi_get_float(sdi->conn, "WAV:XINC?", &xinc) != SR_OK) { - sr_err("Couldn't get sampling rate"); + if (devc->model->series->protocol < PROTOCOL_V3) { + sr_err("Cannot get samplerate (below V3)."); + return SR_ERR; + } + ret = sr_scpi_get_float(sdi->conn, "WAV:XINC?", &xinc); + if (ret != SR_OK) { + sr_err("Cannot get samplerate (WAV:XINC? failed)."); + return SR_ERR; + } + if (!xinc) { + sr_err("Cannot get samplerate (zero XINC value)."); return SR_ERR; } devc->sample_rate = 1. / xinc; } - if (rigol_ds_capture_start(sdi) != SR_OK) return SR_ERR;