spa-json: always advance to the end of the nested array/object

Fixes size issue with nested objects when they are the last property value of
the parent object.
This commit is contained in:
Julian Bouzas 2022-08-28 10:12:00 -04:00
parent b1b5bf2f5f
commit 55691a0d81
2 changed files with 40 additions and 6 deletions

View file

@ -1246,7 +1246,7 @@ static int
check_nested_size (struct spa_json *parent, const gchar *data, int size)
{
const gchar *nested_data;
int nested_size, last_sub_size = 0;
int nested_size;
struct spa_json nested[2];
/* only arrays and objects are considered nested data */
@ -1259,16 +1259,15 @@ check_nested_size (struct spa_json *parent, const gchar *data, int size)
/* recursively advance */
while ((nested_size = spa_json_next (&nested[1], &nested_data)) > 0) {
last_sub_size = check_nested_size (&nested[1], nested_data, nested_size);
if (last_sub_size < 0)
if (check_nested_size (&nested[1], nested_data, nested_size) < 0)
return -1;
}
if (nested_size < 0)
return -1;
/* if last sub size, advance one more time to reach end of nested data */
if (last_sub_size > 0 && spa_json_next (&nested[1], &nested_data) < 0)
return -1;
/* advance one more time to reach end of nested data */
if (spa_json_next (&nested[1], &nested_data) < 0)
return -1;
return nested_data - data;
}

View file

@ -909,6 +909,40 @@ test_spa_json_nested2 (void)
}
}
static void
test_spa_json_nested3 (void)
{
const gchar json_str[] =
"{ test-setting-json3: { key1: \"value\", key2: 2, key3: true } }";
g_autoptr (WpSpaJson) json = wp_spa_json_new_from_string (json_str);
g_assert_nonnull (json);
g_assert_true (wp_spa_json_is_object (json));
g_autoptr (WpIterator) it = wp_spa_json_new_iterator (json);
g_assert_nonnull (it);
{
GValue next = G_VALUE_INIT;
g_assert_true (wp_iterator_next (it, &next));
WpSpaJson *j = g_value_get_boxed (&next);
g_assert_nonnull (j);
g_autofree gchar *v = wp_spa_json_parse_string (j);
g_assert_cmpstr (v, ==, "test-setting-json3");
g_value_unset (&next);
}
{
GValue next = G_VALUE_INIT;
g_assert_true (wp_iterator_next (it, &next));
WpSpaJson *j = g_value_get_boxed (&next);
g_assert_nonnull (j);
g_assert_true (wp_spa_json_is_object (j));
g_autofree gchar *v = wp_spa_json_to_string (j);
g_assert_cmpstr (v, ==, "{ key1: \"value\", key2: 2, key3: true }");
g_value_unset (&next);
}
}
static void
test_spa_json_ownership (void)
{
@ -1161,6 +1195,7 @@ main (int argc, char *argv[])
test_spa_json_object_builder_parser_iterator);
g_test_add_func ("/wp/spa-json/nested", test_spa_json_nested);
g_test_add_func ("/wp/spa-json/nested2", test_spa_json_nested2);
g_test_add_func ("/wp/spa-json/nested3", test_spa_json_nested3);
g_test_add_func ("/wp/spa-json/ownership", test_spa_json_ownership);
g_test_add_func ("/wp/spa-json/spa-format", test_spa_json_spa_format);
g_test_add_func ("/wp/spa-json/to-string", test_spa_json_to_string);