json-pod: add error checking version of json to pod

Add an error checking version of the json to pod converter and use that
in pw-cli to report about json parsing errors.
This commit is contained in:
Wim Taymans 2024-09-23 10:12:56 +02:00
parent 8a8843ba20
commit 4b9db9492e
2 changed files with 30 additions and 7 deletions

View file

@ -135,16 +135,32 @@ static inline int spa_json_to_pod_part(struct spa_pod_builder *b, uint32_t flags
return 0;
}
static inline int spa_json_to_pod(struct spa_pod_builder *b, uint32_t flags,
const struct spa_type_info *info, const char *value, int len)
static inline int spa_json_to_pod_checked(struct spa_pod_builder *b, uint32_t flags,
const struct spa_type_info *info, const char *value, int len,
struct spa_error_location *loc)
{
struct spa_json iter;
const char *val;
int res;
if ((len = spa_json_begin(&iter, value, len, &val)) <= 0)
return -EINVAL;
if (loc)
spa_zero(*loc);
return spa_json_to_pod_part(b, flags, info->type, info, &iter, val, len);
if ((res = spa_json_begin(&iter, value, len, &val)) <= 0)
goto error;
res = spa_json_to_pod_part(b, flags, info->type, info, &iter, val, len);
error:
if (res < 0 && loc)
spa_json_get_error(&iter, value, loc);
return res;
}
static inline int spa_json_to_pod(struct spa_pod_builder *b, uint32_t flags,
const struct spa_type_info *info, const char *value, int len)
{
return spa_json_to_pod_checked(b, flags, info, value, len, NULL);
}
/**

View file

@ -30,6 +30,7 @@
#include <spa/utils/result.h>
#include <spa/utils/string.h>
#include <spa/debug/pod.h>
#include <spa/debug/file.h>
#include <spa/utils/keys.h>
#include <spa/utils/json-pod.h>
#include <spa/pod/dynamic.h>
@ -1780,6 +1781,7 @@ static bool do_set_param(struct data *data, const char *cmd, char *args, char **
spa_auto(spa_pod_dynamic_builder) b = { 0 };
const struct spa_type_info *ti;
struct spa_pod *pod;
struct spa_error_location loc;
spa_pod_dynamic_builder_init(&b, buffer, sizeof(buffer), 4096);
@ -1804,8 +1806,13 @@ static bool do_set_param(struct data *data, const char *cmd, char *args, char **
*error = spa_aprintf("%s: unknown param type: %s", cmd, a[1]);
return false;
}
if ((res = spa_json_to_pod(&b.b, 0, ti, a[2], strlen(a[2]))) < 0) {
*error = spa_aprintf("%s: can't make pod: %s", cmd, spa_strerror(res));
if ((res = spa_json_to_pod_checked(&b.b, 0, ti, a[2], strlen(a[2]), &loc)) < 0) {
if (loc.line != 0) {
spa_debug_file_error_location(stderr, &loc,
"syntax error in json '%s': %s",
a[2], loc.reason);
}
*error = spa_aprintf("%s: invalid pod: %s", cmd, loc.reason);
return false;
}
if ((pod = spa_pod_builder_deref(&b.b, 0)) == NULL) {