From 799bc13c858f6cfc170bf570a3f3e311c0dd5aff Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 31 Dec 2020 17:07:03 +0100 Subject: [PATCH] json: set state on final bare string When we run out of chars and are inside a bare string, set our state so that the next round will continue instead of looping forever. --- spa/include/spa/utils/json.h | 12 +++++++----- src/tests/test-properties.c | 10 ++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/spa/include/spa/utils/json.h b/spa/include/spa/utils/json.h index 9e612b687..c1c324e03 100644 --- a/spa/include/spa/utils/json.h +++ b/spa/include/spa/utils/json.h @@ -164,11 +164,13 @@ static inline int spa_json_next(struct spa_json * iter, const char **value) } } - return (iter->depth == 0 ? - (iter->state == __BARE && iter->cur < iter->end ? - iter->cur - *value : - 0) : - -1); + if (iter->depth != 0) + return -1; + if (iter->state == __BARE) { + iter->state = __STRUCT; + return iter->cur - *value; + } + return 0; } static inline int spa_json_enter_container(struct spa_json *iter, struct spa_json *sub, char type) diff --git a/src/tests/test-properties.c b/src/tests/test-properties.c index 6fc52c248..2ad026166 100644 --- a/src/tests/test-properties.c +++ b/src/tests/test-properties.c @@ -222,6 +222,16 @@ static void test_new_string(void) spa_assert(!strcmp(pw_properties_get(props, "empty"), "")); pw_properties_free(props); + + props = pw_properties_new_string("foo=bar bar=baz"); + spa_assert(props != NULL); + spa_assert(props->flags == 0); + spa_assert(props->dict.n_items == 2); + + spa_assert(!strcmp(pw_properties_get(props, "foo"), "bar")); + spa_assert(!strcmp(pw_properties_get(props, "bar"), "baz")); + + pw_properties_free(props); } static void test_update(void)