scpi: make sure to either return valid SCPI response data or NULL
authorGerhard Sittig <gerhard.sittig@gmx.net>
Sun, 16 May 2021 11:48:24 +0000 (13:48 +0200)
committerGerhard Sittig <gerhard.sittig@gmx.net>
Sat, 22 May 2021 06:06:58 +0000 (08:06 +0200)
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.

src/scpi/scpi.c

index 9fa4ac03605392245ca0077f468cd006bb99f235..c01791ef5ecebc8ac43300db1248a36081951f92 100644 (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;