From c9cfcd25917c4ee5767fa097d4fa8fb8a4888a6d Mon Sep 17 00:00:00 2001 From: Gerhard Sittig Date: Sun, 16 May 2021 14:40:00 +0200 Subject: [PATCH] fluke-45: 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. This implementation is incomplete. The fluke-45 driver's context holds a "global" copy of the most recently received response. While the data is freed in the next receive call, one item remains allocated for the driver's remaining life time. Which is considered non-critical. Also moves an operator in a complex multi-line expression to a different location to follow the project's style. This addresses part of bug #1683. --- src/hardware/fluke-45/api.c | 4 ++++ src/hardware/fluke-45/protocol.c | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/hardware/fluke-45/api.c b/src/hardware/fluke-45/api.c index 16c7dc03..2e80b6bf 100644 --- a/src/hardware/fluke-45/api.c +++ b/src/hardware/fluke-45/api.c @@ -78,16 +78,20 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) sr_scpi_get_string(scpi, "ECHO-TEST", &response); if (response && strcmp(response, "ECHO-TEST") == 0) { sr_err("Serial port ECHO is ON. Please turn it OFF!"); + g_free(response); return NULL; } + g_free(response); #endif /* Get device IDN. */ if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { + sr_scpi_hw_info_free(hw_info); sr_info("Couldn't get IDN response, retrying."); sr_scpi_close(scpi); sr_scpi_open(scpi); if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { + sr_scpi_hw_info_free(hw_info); sr_info("Couldn't get IDN response."); return NULL; } diff --git a/src/hardware/fluke-45/protocol.c b/src/hardware/fluke-45/protocol.c index 5d9c3793..16489007 100644 --- a/src/hardware/fluke-45/protocol.c +++ b/src/hardware/fluke-45/protocol.c @@ -332,6 +332,7 @@ SR_PRIV int fl45_scpi_get_response(const struct sr_dev_inst *sdi, char *cmd) * If the response is a prompt then ignore and read the next * response in the buffer. */ + g_free(devc->response); devc->response = NULL; /* Now attempt to read again. */ if (sr_scpi_get_string(sdi->conn, NULL, &devc->response) != SR_OK) @@ -339,9 +340,10 @@ SR_PRIV int fl45_scpi_get_response(const struct sr_dev_inst *sdi, char *cmd) } /* NULL RS232 error prompts. */ - if (strcmp(devc->response, "!>") == 0 - || (strcmp(devc->response, "?>") == 0)) { + if (strcmp(devc->response, "!>") == 0 || + (strcmp(devc->response, "?>") == 0)) { /* Unable to execute CMD. */ + g_free(devc->response); devc->response = NULL; } -- 2.30.2