spa-json: only add the json data represented by its size

The data pointer of a WpSpaJson object can be bigger than the actual WpSpaJson
size. For example, this happens when iterating an array: instead of
re-allocating the nested data into a new region of memory, the iterator uses
the same region of memory as the parent, and just sets the pointer data and size
to the correct location when creating the nested WpSpaJson object.

This patch makes sure only the data represented by the size is added into the
builder when adding a json object, fixing issues that were happening before when
adding nested objects directly from a second WpSpaJson object.
This commit is contained in:
Julian Bouzas 2022-06-21 11:52:53 -04:00 committed by George Kiagiadakis
parent 8ade19ac59
commit a19c7f3d2f

View file

@ -953,6 +953,14 @@ builder_add_formatted (WpSpaJsonBuilder *self, const gchar *fmt, ...)
self->size += s;
}
static void
builder_add_json (WpSpaJsonBuilder *self, WpSpaJson *json)
{
g_return_if_fail (self->max_size - self->size >= json->size + 1);
snprintf (self->data + self->size, json->size + 1, "%s", json->data);
self->size += json->size;
}
/*!
* \brief Adds a property into the builder
*
@ -1053,14 +1061,14 @@ wp_spa_json_builder_add_string (WpSpaJsonBuilder *self, const gchar *value)
*
* \ingroup wpspajson
* \param self the spa json builder object
* \param json the json value
* \param json (transfer none): the json value
*/
void
wp_spa_json_builder_add_json (WpSpaJsonBuilder *self, WpSpaJson *json)
{
ensure_separator (self, FALSE);
ensure_allocated_max_size (self, json->size);
builder_add_formatted (self, "%s", json->data);
builder_add_json (self, json);
}
/*!