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.
This commit is contained in:
Pauli Virtanen 2024-03-23 16:25:45 +02:00
parent 7ee8df39e8
commit 0da9255057
3 changed files with 24 additions and 2 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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;
}