tools: check properties given by user for syntax errors

Check JSON passed in by user for syntax errors before processing it.
This commit is contained in:
Pauli Virtanen 2024-03-24 16:11:26 +02:00
parent 0da9255057
commit 026d55df62
5 changed files with 54 additions and 11 deletions

View file

@ -1402,6 +1402,23 @@ static bool do_info(struct data *data, const char *cmd, char *args, char **error
return true;
}
static struct pw_properties *properties_new_checked(const char *str, char **error)
{
struct pw_properties *props;
int line, col;
if (!pw_properties_check_string(str, strlen(str), &line, &col)) {
*error = spa_aprintf("syntax error in properties, line:%d col:%d", line, col);
return NULL;
}
props = pw_properties_new_string(str);
if (!props)
*error = spa_aprintf("failed to allocate properties");
return props;
}
static bool do_create_device(struct data *data, const char *cmd, char *args, char **error)
{
struct remote_data *rd = data->current;
@ -1417,8 +1434,10 @@ static bool do_create_device(struct data *data, const char *cmd, char *args, cha
*error = spa_aprintf("%s <factory-name> [<properties>]", cmd);
return false;
}
if (n == 2)
props = pw_properties_new_string(a[1]);
if (n == 2) {
if ((props = properties_new_checked(a[1], error)) == NULL)
return false;
}
proxy = pw_core_create_object(rd->core, a[0],
PW_TYPE_INTERFACE_Device,
@ -1457,8 +1476,10 @@ static bool do_create_node(struct data *data, const char *cmd, char *args, char
*error = spa_aprintf("%s <factory-name> [<properties>]", cmd);
return false;
}
if (n == 2)
props = pw_properties_new_string(a[1]);
if (n == 2) {
if ((props = properties_new_checked(a[1], error)) == NULL)
return false;
}
proxy = pw_core_create_object(rd->core, a[0],
PW_TYPE_INTERFACE_Node,
@ -1574,10 +1595,12 @@ static bool do_create_link(struct data *data, const char *cmd, char *args, char
*error = spa_aprintf("%s <node-id> <port> <node-id> <port> [<properties>]", cmd);
return false;
}
if (n == 5)
props = pw_properties_new_string(a[4]);
else
if (n == 5) {
if ((props = properties_new_checked(a[4], error)) == NULL)
return false;
} else {
props = pw_properties_new(NULL, NULL);
}
if (!spa_streq(a[0], "-"))
pw_properties_set(props, PW_KEY_LINK_OUTPUT_NODE, a[0]);