rigol-ds: free memory that was allocated by SCPI get routines

The SCPI get routines may allocate memory for response data which
callers have to free after use.

The approach to release allocated memory is simlar or identical to the
corresponding parts of pull request 95, in an attempt to get the reported
leak fixes in, yet reduce the potential for conflicts where this commit
deviates from the PR. Most work was done by Ralf. Thank you!

This addresses part of bug #1683.

Submitted-By: Reported-By: Ralf <jr-oss@gmx.net>
Tested-By: Reported-By: Ralf <jr-oss@gmx.net>
This commit is contained in:
Gerhard Sittig 2021-05-16 14:48:42 +02:00
parent 83d38ed90a
commit 4fad41a8a4
1 changed files with 21 additions and 7 deletions

View File

@ -106,7 +106,7 @@ static void rigol_ds_set_wait_event(struct dev_context *devc, enum wait_events e
*/ */
static int rigol_ds_event_wait(const struct sr_dev_inst *sdi, char status1, char status2) static int rigol_ds_event_wait(const struct sr_dev_inst *sdi, char status1, char status2)
{ {
char *buf; char *buf, c;
struct dev_context *devc; struct dev_context *devc;
time_t start; time_t start;
@ -133,7 +133,9 @@ static int rigol_ds_event_wait(const struct sr_dev_inst *sdi, char status1, char
if (sr_scpi_get_string(sdi->conn, ":TRIG:STAT?", &buf) != SR_OK) if (sr_scpi_get_string(sdi->conn, ":TRIG:STAT?", &buf) != SR_OK)
return SR_ERR; return SR_ERR;
} while (buf[0] == status1 || buf[0] == status2); c = buf[0];
g_free(buf);
} while (c == status1 || c == status2);
devc->wait_status = 2; devc->wait_status = 2;
} }
@ -146,7 +148,9 @@ static int rigol_ds_event_wait(const struct sr_dev_inst *sdi, char status1, char
if (sr_scpi_get_string(sdi->conn, ":TRIG:STAT?", &buf) != SR_OK) if (sr_scpi_get_string(sdi->conn, ":TRIG:STAT?", &buf) != SR_OK)
return SR_ERR; return SR_ERR;
} while (buf[0] != status1 && buf[0] != status2); c = buf[0];
g_free(buf);
} while (c != status1 && c != status2);
rigol_ds_set_wait_event(devc, WAIT_NONE); rigol_ds_set_wait_event(devc, WAIT_NONE);
} }
@ -266,7 +270,7 @@ static int rigol_ds_block_wait(const struct sr_dev_inst *sdi)
char *buf; char *buf;
struct dev_context *devc; struct dev_context *devc;
time_t start; time_t start;
int len; int len, ret;
if (!(devc = sdi->priv)) if (!(devc = sdi->priv))
return SR_ERR; return SR_ERR;
@ -292,8 +296,9 @@ static int rigol_ds_block_wait(const struct sr_dev_inst *sdi)
/* "READ,nnnn" (still working) or "IDLE,nnnn" (finished) */ /* "READ,nnnn" (still working) or "IDLE,nnnn" (finished) */
if (sr_scpi_get_string(sdi->conn, ":WAV:STAT?", &buf) != SR_OK) if (sr_scpi_get_string(sdi->conn, ":WAV:STAT?", &buf) != SR_OK)
return SR_ERR; return SR_ERR;
ret = parse_int(buf + 5, &len);
if (parse_int(buf + 5, &len) != SR_OK) g_free(buf);
if (ret != SR_OK)
return SR_ERR; return SR_ERR;
} while (buf[0] == 'R' && len < (1000 * 1000)); } while (buf[0] == 'R' && len < (1000 * 1000));
} }
@ -334,6 +339,7 @@ SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi)
gchar *trig_mode; gchar *trig_mode;
unsigned int num_channels, i, j; unsigned int num_channels, i, j;
int buffer_samples; int buffer_samples;
int ret;
if (!(devc = sdi->priv)) if (!(devc = sdi->priv))
return SR_ERR; return SR_ERR;
@ -366,7 +372,9 @@ SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi)
return SR_ERR; return SR_ERR;
if (sr_scpi_get_string(sdi->conn, ":TRIG:MODE?", &trig_mode) != SR_OK) if (sr_scpi_get_string(sdi->conn, ":TRIG:MODE?", &trig_mode) != SR_OK)
return SR_ERR; return SR_ERR;
if (rigol_ds_config_set(sdi, ":TRIG:%s:SWE SING", trig_mode) != SR_OK) ret = rigol_ds_config_set(sdi, ":TRIG:%s:SWE SING", trig_mode);
g_free(trig_mode);
if (ret != SR_OK)
return SR_ERR; return SR_ERR;
if (rigol_ds_config_set(sdi, ":RUN") != SR_OK) if (rigol_ds_config_set(sdi, ":RUN") != SR_OK)
return SR_ERR; return SR_ERR;
@ -935,6 +943,8 @@ SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi)
/* Coupling. */ /* Coupling. */
for (i = 0; i < devc->model->analog_channels; i++) { for (i = 0; i < devc->model->analog_channels; i++) {
cmd = g_strdup_printf(":CHAN%d:COUP?", i + 1); cmd = g_strdup_printf(":CHAN%d:COUP?", i + 1);
g_free(devc->coupling[i]);
devc->coupling[i] = NULL;
res = sr_scpi_get_string(sdi->conn, cmd, &devc->coupling[i]); res = sr_scpi_get_string(sdi->conn, cmd, &devc->coupling[i]);
g_free(cmd); g_free(cmd);
if (res != SR_OK) if (res != SR_OK)
@ -945,6 +955,8 @@ SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi)
sr_dbg("CH%d %s", i + 1, devc->coupling[i]); sr_dbg("CH%d %s", i + 1, devc->coupling[i]);
/* Trigger source. */ /* Trigger source. */
g_free(devc->trigger_source);
devc->trigger_source = NULL;
if (sr_scpi_get_string(sdi->conn, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK) if (sr_scpi_get_string(sdi->conn, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK)
return SR_ERR; return SR_ERR;
sr_dbg("Current trigger source %s", devc->trigger_source); sr_dbg("Current trigger source %s", devc->trigger_source);
@ -956,6 +968,8 @@ SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi)
sr_dbg("Current horizontal trigger position %g", devc->horiz_triggerpos); sr_dbg("Current horizontal trigger position %g", devc->horiz_triggerpos);
/* Trigger slope. */ /* Trigger slope. */
g_free(devc->trigger_slope);
devc->trigger_slope = NULL;
if (sr_scpi_get_string(sdi->conn, ":TRIG:EDGE:SLOP?", &devc->trigger_slope) != SR_OK) if (sr_scpi_get_string(sdi->conn, ":TRIG:EDGE:SLOP?", &devc->trigger_slope) != SR_OK)
return SR_ERR; return SR_ERR;
sr_dbg("Current trigger slope %s", devc->trigger_slope); sr_dbg("Current trigger slope %s", devc->trigger_slope);