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.
This commit is contained in:
Gerhard Sittig 2021-05-16 13:48:24 +02:00
parent ac1866b923
commit 4c72966444
1 changed files with 8 additions and 5 deletions

View File

@ -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;