From 0da925505783b929c0a1f9e15f0726b9a8670dc4 Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Sat, 23 Mar 2024 16:25:45 +0200 Subject: [PATCH] treewide: check for JSON parse errors Check for JSON parse errors, and log error messages as appropriate. It's mostly enough to do this where the input is parsed for the first time, e.g. via pw_properties_new_string, as that already validates the JSON syntax. --- spa/plugins/bluez5/quirks.c | 4 ++++ src/pipewire/conf.c | 15 +++++++++++++-- src/tools/pw-dot.c | 7 +++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/spa/plugins/bluez5/quirks.c b/spa/plugins/bluez5/quirks.c index c386c8a6e..2ca4210a1 100644 --- a/spa/plugins/bluez5/quirks.c +++ b/spa/plugins/bluez5/quirks.c @@ -159,6 +159,7 @@ static void load_quirks(struct spa_bt_quirks *this, const char *str, size_t len) struct spa_json data = SPA_JSON_INIT(str, len); struct spa_json rules; char key[1024]; + int line, col; if (spa_json_enter_object(&data, &rules) <= 0) spa_json_init(&rules, str, len); @@ -182,6 +183,9 @@ static void load_quirks(struct spa_bt_quirks *this, const char *str, size_t len) else if (spa_streq(key, "bluez5.features.device") && !this->device_rules) this->device_rules = strndup(value, sz); } + + if (spa_json_get_error(&rules, str, &line, &col)) + spa_log_error(this->log, "spa.bluez5 quirks syntax error, line:%d col:%d", line, col); } static int load_conf(struct spa_bt_quirks *this, const char *path) diff --git a/src/pipewire/conf.c b/src/pipewire/conf.c index 3f678c4ad..e1f4e20ee 100644 --- a/src/pipewire/conf.c +++ b/src/pipewire/conf.c @@ -387,6 +387,8 @@ static int conf_load(const char *path, struct pw_properties *conf) char *data; struct stat sbuf; int count; + int line = -1, col = -1; + int res; spa_autoclose int fd = open(path, O_CLOEXEC | O_RDONLY); if (fd < 0) @@ -399,6 +401,11 @@ static int conf_load(const char *path, struct pw_properties *conf) if ((data = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) goto error; + if (!pw_properties_check_string(data, sbuf.st_size, &line, &col)) { + errno = EINVAL; + goto error; + } + count = pw_properties_update_string(conf, data, sbuf.st_size); munmap(data, sbuf.st_size); } else { @@ -410,8 +417,12 @@ static int conf_load(const char *path, struct pw_properties *conf) return 0; error: - pw_log_warn("%p: error loading config '%s': %m", conf, path); - return -errno; + res = -errno; + if (line != -1) + pw_log_warn("%p: syntax error in config '%s': line:%d col:%d", conf, path, line, col); + else + pw_log_warn("%p: error loading config '%s': %m", conf, path); + return res; } static bool check_override(struct pw_properties *conf, const char *name, int level) diff --git a/src/tools/pw-dot.c b/src/tools/pw-dot.c index aa1d766c6..db9367640 100644 --- a/src/tools/pw-dot.c +++ b/src/tools/pw-dot.c @@ -1277,6 +1277,7 @@ static int get_data_from_json(struct data *data, const char *json_path) struct stat sbuf; struct spa_json it[2]; const char *value; + int line, col; if ((fd = open(json_path, O_CLOEXEC | O_RDONLY)) < 0) { fprintf(stderr, "error opening file '%s': %m\n", json_path); @@ -1312,6 +1313,12 @@ static int get_data_from_json(struct data *data, const char *json_path) } munmap(json, sbuf.st_size); + + if (spa_json_get_error(&it[0], json, &line, &col)) { + fprintf(stderr, "JSON syntax error on line:%d col:%d\n", line, col); + return -1; + } + return 0; }