From: Gerhard Sittig Date: Sun, 16 May 2021 11:48:24 +0000 (+0200) Subject: scpi: make sure to either return valid SCPI response data or NULL X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=4c7296644461e39cd06f5f5fa0898a4c189f43e1;p=libsigrok.git scpi: make sure to either return valid SCPI response data or NULL Assign NULL early in those SCPI get routines which return allocated memory to their callers. So that the return value is either a valid pointer to memory or NULL regardless of the routine's exit code. This simplifies call sites and increases robustness. --- diff --git a/src/scpi/scpi.c b/src/scpi/scpi.c index 9fa4ac03..c01791ef 100644 --- a/src/scpi/scpi.c +++ b/src/scpi/scpi.c @@ -620,8 +620,10 @@ SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi, const char *command, char **scpi_response) { GString *response; - response = g_string_sized_new(1024); + *scpi_response = NULL; + + response = g_string_sized_new(1024); if (sr_scpi_get_data(scpi, command, &response) != SR_OK) { if (response) g_string_free(response, TRUE); @@ -857,6 +859,7 @@ SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi, gchar **ptr, **tokens; GArray *response_array; + *scpi_response = NULL; response = NULL; tokens = NULL; @@ -883,7 +886,6 @@ SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi, if (ret != SR_OK && response_array->len == 0) { g_array_free(response_array, TRUE); - *scpi_response = NULL; return SR_ERR_DATA; } @@ -913,6 +915,7 @@ SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi, gchar **ptr, **tokens; GArray *response_array; + *scpi_response = NULL; response = NULL; tokens = NULL; @@ -939,7 +942,6 @@ SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi, if (response_array->len == 0) { g_array_free(response_array, TRUE); - *scpi_response = NULL; return SR_ERR_DATA; } @@ -972,6 +974,8 @@ SR_PRIV int sr_scpi_get_block(struct sr_scpi_dev_inst *scpi, long datalen; gint64 timeout; + *scpi_response = NULL; + g_mutex_lock(&scpi->scpi_mutex); if (command) @@ -993,8 +997,6 @@ SR_PRIV int sr_scpi_get_block(struct sr_scpi_dev_inst *scpi, timeout = g_get_monotonic_time() + scpi->read_timeout_us; - *scpi_response = NULL; - /* Get (the first chunk of) the response. */ do { ret = scpi_read_response(scpi, response, timeout); @@ -1108,6 +1110,7 @@ SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi, struct sr_scpi_hw_info *hw_info; gchar *idn_substr; + *scpi_response = NULL; response = NULL; tokens = NULL;