From 8d4fe81ebd7e77ae7df256f2a1348fc24a4bcaf8 Mon Sep 17 00:00:00 2001 From: George Kiagiadakis Date: Wed, 16 Mar 2022 21:11:09 +0200 Subject: [PATCH] spa-json: allocate enough space for parsing strings without looping The string size is always less or equal to the size of the token (len), so by allocating len+1 bytes we have enough space to write the whole token and the null byte, if needed. --- lib/wp/spa-json.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/wp/spa-json.c b/lib/wp/spa-json.c index fa5c4850..22aa3339 100644 --- a/lib/wp/spa-json.c +++ b/lib/wp/spa-json.c @@ -624,16 +624,9 @@ wp_spa_json_parse_float (WpSpaJson *self, float *value) static gchar * wp_spa_json_parse_string_internal (const gchar *data, int len) { - size_t size = WP_SPA_JSON_STRING_INIT_SIZE; - gchar *res = NULL; - - res = g_new0(gchar, size); - while (spa_json_parse_stringn(data, len, res, size) < 0) - { - size *= 2; - res = g_realloc(res, size); - } - + gchar *res = g_new0 (gchar, len+1); + if (res) + spa_json_parse_string (data, len, res); return res; }