From f45d89b75b6839c0435b88c52f103368d7b8f756 Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Wed, 20 Mar 2024 20:40:05 +0200 Subject: [PATCH] spa: json: propagate parse error in spa_json_container_len Successful return is always >= 2 since it includes {} or [], so use 0 to indicate error. Since there's existing code that doesn't check the return value, it's better to use 0 for errors as it'll likely to just lead to producing an empty string if the value is not checked. --- spa/include/spa/utils/json.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/spa/include/spa/utils/json.h b/spa/include/spa/utils/json.h index 1a0e4887a..6fa8c7806 100644 --- a/spa/include/spa/utils/json.h +++ b/spa/include/spa/utils/json.h @@ -281,12 +281,20 @@ static inline int spa_json_is_container(const char *val, int len) return len > 0 && (*val == '{' || *val == '['); } +/** + * Return length of container at current position, starting at \a value. + * + * \return Length of container including {} or [], or 0 on error. + */ static inline int spa_json_container_len(struct spa_json *iter, const char *value, int len SPA_UNUSED) { const char *val; struct spa_json sub; + int res; spa_json_enter(iter, &sub); - while (spa_json_next(&sub, &val) > 0); + while ((res = spa_json_next(&sub, &val)) > 0); + if (res < 0) + return 0; return sub.cur + 1 - value; }