]> sigrok.org Git - libsigrok.git/commitdiff
rigol-ds: improve robustness in samplerate getting code path
authorGerhard Sittig <redacted>
Wed, 22 Dec 2021 13:01:42 +0000 (14:01 +0100)
committerGerhard Sittig <redacted>
Sat, 8 Jan 2022 09:13:21 +0000 (10:13 +0100)
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

src/hardware/rigol-ds/api.c

index 95de8f6a0d7b30339a7fd6756ba3d4bfe556d0f6..632c95963cb75e517e6d7abe3cbff783e6cbee70 100644 (file)
@@ -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;