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

@ -1606,7 +1606,7 @@ int main(int argc, char *argv[])
uint8_t buffer[1024];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
const char *prog;
int exit_code = EXIT_FAILURE, c, ret;
int exit_code = EXIT_FAILURE, c, ret, line, col;
enum pw_stream_flags flags = 0;
setlocale(LC_ALL, "");
@ -1729,6 +1729,10 @@ int main(int argc, char *argv[])
break;
case 'P':
if (!pw_properties_check_string(optarg, strlen(optarg), &line, &col)) {
fprintf(stderr, "error: syntax error in --properties at line:%d col:%d\n", line, col);
goto error_usage;
}
pw_properties_update_string(data.props, optarg, strlen(optarg));
break;

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]);

View File

@ -147,7 +147,7 @@ int main(int argc, char *argv[])
{ "properties", required_argument, NULL, 'P' },
{ NULL, 0, NULL, 0}
};
int c, res, listen_fd, close_fd[2];
int c, res, listen_fd, close_fd[2], line, col;
char temp[PATH_MAX] = "/tmp/pipewire-XXXXXX";
struct sockaddr_un sockaddr = {0};
@ -176,6 +176,10 @@ int main(int argc, char *argv[])
opt_remote = optarg;
break;
case 'P':
if (!pw_properties_check_string(optarg, strlen(optarg), &line, &col)) {
fprintf(stderr, "error: syntax error in --properties at line:%d col:%d\n", line, col);
return -1;
}
pw_properties_update_string(data.props, optarg, strlen(optarg));
break;
default:

View File

@ -873,7 +873,7 @@ static int run(int argc, char *argv[])
.objects = SPA_LIST_INIT(&data.objects),
.target_links = SPA_LIST_INIT(&data.target_links),
};
int res = 0, c;
int res = 0, c, line, col;
static const struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
@ -942,6 +942,10 @@ static int run(int argc, char *argv[])
pw_properties_set(data.props, PW_KEY_LINK_PASSIVE, "true");
break;
case 'p':
if (!pw_properties_check_string(optarg, strlen(optarg), &line, &col)) {
fprintf(stderr, "error: syntax error in --props at line:%d col:%d\n", line, col);
return -1;
}
pw_properties_update_string(data.props, optarg, strlen(optarg));
break;
case 'd':

View File

@ -110,7 +110,7 @@ int main(int argc, char *argv[])
{ "playback-props", required_argument, NULL, 'o' },
{ NULL, 0, NULL, 0}
};
int c, res = -1;
int c, res = -1, line, col;
setlocale(LC_ALL, "");
pw_init(&argc, &argv);
@ -170,9 +170,17 @@ int main(int argc, char *argv[])
pw_properties_set(data.playback_props, PW_KEY_TARGET_OBJECT, optarg);
break;
case 'i':
if (!pw_properties_check_string(optarg, strlen(optarg), &line, &col)) {
fprintf(stderr, "error: syntax error in --capture-props at line:%d col:%d\n", line, col);
return -1;
}
pw_properties_update_string(data.capture_props, optarg, strlen(optarg));
break;
case 'o':
if (!pw_properties_check_string(optarg, strlen(optarg), &line, &col)) {
fprintf(stderr, "error: syntax error in --playback-props at line:%d col:%d\n", line, col);
return -1;
}
pw_properties_update_string(data.playback_props, optarg, strlen(optarg));
break;
default: