mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-11 13:30:07 -05:00
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:
parent
7ee8df39e8
commit
0da9255057
3 changed files with 24 additions and 2 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue