From: Gerhard Sittig Date: Sun, 16 May 2021 17:51:52 +0000 (+0200) Subject: scpi: readability nits in vector getters, style nit in malloc call X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=191af3d9ca2f347810a12a3f9d87bdc8c8db15aa scpi: readability nits in vector getters, style nit in malloc call Improve readability of SCPI uint8 and float vector get routines. Move assignment and use of variables closer together to simplify review. Allocate the glib array based on the text split result's length. Move data processing to the "straight" path and handle failed conversion as an exceptional condition in an error path. Eliminate a redundant data type reference in a malloc call. --- diff --git a/src/scpi/scpi.c b/src/scpi/scpi.c index 0f767f2a..ec0ddc55 100644 --- a/src/scpi/scpi.c +++ b/src/scpi/scpi.c @@ -863,28 +863,30 @@ SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi, float tmp; char *response; gchar **ptr, **tokens; + size_t token_count; GArray *response_array; *scpi_response = NULL; - response = NULL; - tokens = NULL; + response = NULL; ret = sr_scpi_get_string(scpi, command, &response); if (ret != SR_OK && !response) return ret; tokens = g_strsplit(response, ",", 0); - ptr = tokens; + token_count = g_strv_length(tokens); - response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256); + response_array = g_array_sized_new(TRUE, FALSE, + sizeof(float), token_count + 1); + ptr = tokens; while (*ptr) { - if (sr_atof_ascii(*ptr, &tmp) == SR_OK) - response_array = g_array_append_val(response_array, - tmp); - else + ret = sr_atof_ascii(*ptr, &tmp); + if (ret != SR_OK) { ret = SR_ERR_DATA; - + break; + } + response_array = g_array_append_val(response_array, tmp); ptr++; } g_strfreev(tokens); @@ -920,28 +922,30 @@ SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi, int tmp, ret; char *response; gchar **ptr, **tokens; + size_t token_count; GArray *response_array; *scpi_response = NULL; - response = NULL; - tokens = NULL; + response = NULL; ret = sr_scpi_get_string(scpi, command, &response); if (ret != SR_OK && !response) return ret; tokens = g_strsplit(response, ",", 0); - ptr = tokens; + token_count = g_strv_length(tokens); - response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256); + response_array = g_array_sized_new(TRUE, FALSE, + sizeof(uint8_t), token_count + 1); + ptr = tokens; while (*ptr) { - if (sr_atoi(*ptr, &tmp) == SR_OK) - response_array = g_array_append_val(response_array, - tmp); - else + ret = sr_atoi(*ptr, &tmp); + if (ret != SR_OK) { ret = SR_ERR_DATA; - + break; + } + response_array = g_array_append_val(response_array, tmp); ptr++; } g_strfreev(tokens); @@ -1151,7 +1155,7 @@ SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi, } g_free(response); - hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info)); + hw_info = g_malloc0(sizeof(*hw_info)); idn_substr = g_strstr_len(tokens[0], -1, "IDN "); if (idn_substr == NULL)