mirror of
				https://gitlab.freedesktop.org/pipewire/pipewire.git
				synced 2025-11-03 09:01:54 -05:00 
			
		
		
		
	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:
		
							parent
							
								
									0da9255057
								
							
						
					
					
						commit
						026d55df62
					
				
					 5 changed files with 54 additions and 11 deletions
				
			
		| 
						 | 
					@ -1606,7 +1606,7 @@ int main(int argc, char *argv[])
 | 
				
			||||||
	uint8_t buffer[1024];
 | 
						uint8_t buffer[1024];
 | 
				
			||||||
	struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
 | 
						struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
 | 
				
			||||||
	const char *prog;
 | 
						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;
 | 
						enum pw_stream_flags flags = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	setlocale(LC_ALL, "");
 | 
						setlocale(LC_ALL, "");
 | 
				
			||||||
| 
						 | 
					@ -1729,6 +1729,10 @@ int main(int argc, char *argv[])
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		case 'P':
 | 
							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));
 | 
								pw_properties_update_string(data.props, optarg, strlen(optarg));
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1402,6 +1402,23 @@ static bool do_info(struct data *data, const char *cmd, char *args, char **error
 | 
				
			||||||
	return true;
 | 
						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)
 | 
					static bool do_create_device(struct data *data, const char *cmd, char *args, char **error)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct remote_data *rd = data->current;
 | 
						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);
 | 
							*error = spa_aprintf("%s <factory-name> [<properties>]", cmd);
 | 
				
			||||||
		return false;
 | 
							return false;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if (n == 2)
 | 
						if (n == 2) {
 | 
				
			||||||
		props = pw_properties_new_string(a[1]);
 | 
							if ((props = properties_new_checked(a[1], error)) == NULL)
 | 
				
			||||||
 | 
								return false;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	proxy = pw_core_create_object(rd->core, a[0],
 | 
						proxy = pw_core_create_object(rd->core, a[0],
 | 
				
			||||||
					    PW_TYPE_INTERFACE_Device,
 | 
										    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);
 | 
							*error = spa_aprintf("%s <factory-name> [<properties>]", cmd);
 | 
				
			||||||
		return false;
 | 
							return false;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if (n == 2)
 | 
						if (n == 2) {
 | 
				
			||||||
		props = pw_properties_new_string(a[1]);
 | 
							if ((props = properties_new_checked(a[1], error)) == NULL)
 | 
				
			||||||
 | 
								return false;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	proxy = pw_core_create_object(rd->core, a[0],
 | 
						proxy = pw_core_create_object(rd->core, a[0],
 | 
				
			||||||
					    PW_TYPE_INTERFACE_Node,
 | 
										    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);
 | 
							*error = spa_aprintf("%s <node-id> <port> <node-id> <port> [<properties>]", cmd);
 | 
				
			||||||
		return false;
 | 
							return false;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if (n == 5)
 | 
						if (n == 5) {
 | 
				
			||||||
		props = pw_properties_new_string(a[4]);
 | 
							if ((props = properties_new_checked(a[4], error)) == NULL)
 | 
				
			||||||
	else
 | 
								return false;
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
		props = pw_properties_new(NULL, NULL);
 | 
							props = pw_properties_new(NULL, NULL);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!spa_streq(a[0], "-"))
 | 
						if (!spa_streq(a[0], "-"))
 | 
				
			||||||
		pw_properties_set(props, PW_KEY_LINK_OUTPUT_NODE, a[0]);
 | 
							pw_properties_set(props, PW_KEY_LINK_OUTPUT_NODE, a[0]);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -147,7 +147,7 @@ int main(int argc, char *argv[])
 | 
				
			||||||
		{ "properties",		required_argument,	NULL, 'P' },
 | 
							{ "properties",		required_argument,	NULL, 'P' },
 | 
				
			||||||
		{ NULL,	0, NULL, 0}
 | 
							{ 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";
 | 
						char temp[PATH_MAX] = "/tmp/pipewire-XXXXXX";
 | 
				
			||||||
	struct sockaddr_un sockaddr = {0};
 | 
						struct sockaddr_un sockaddr = {0};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -176,6 +176,10 @@ int main(int argc, char *argv[])
 | 
				
			||||||
			opt_remote = optarg;
 | 
								opt_remote = optarg;
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		case 'P':
 | 
							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));
 | 
								pw_properties_update_string(data.props, optarg, strlen(optarg));
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -873,7 +873,7 @@ static int run(int argc, char *argv[])
 | 
				
			||||||
		.objects = SPA_LIST_INIT(&data.objects),
 | 
							.objects = SPA_LIST_INIT(&data.objects),
 | 
				
			||||||
		.target_links = SPA_LIST_INIT(&data.target_links),
 | 
							.target_links = SPA_LIST_INIT(&data.target_links),
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
	int res = 0, c;
 | 
						int res = 0, c, line, col;
 | 
				
			||||||
	static const struct option long_options[] = {
 | 
						static const struct option long_options[] = {
 | 
				
			||||||
		{ "help",	no_argument,		NULL, 'h' },
 | 
							{ "help",	no_argument,		NULL, 'h' },
 | 
				
			||||||
		{ "version",	no_argument,		NULL, 'V' },
 | 
							{ "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");
 | 
								pw_properties_set(data.props, PW_KEY_LINK_PASSIVE, "true");
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		case 'p':
 | 
							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));
 | 
								pw_properties_update_string(data.props, optarg, strlen(optarg));
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		case 'd':
 | 
							case 'd':
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -110,7 +110,7 @@ int main(int argc, char *argv[])
 | 
				
			||||||
		{ "playback-props",	required_argument,	NULL, 'o' },
 | 
							{ "playback-props",	required_argument,	NULL, 'o' },
 | 
				
			||||||
		{ NULL, 0, NULL, 0}
 | 
							{ NULL, 0, NULL, 0}
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
	int c, res = -1;
 | 
						int c, res = -1, line, col;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	setlocale(LC_ALL, "");
 | 
						setlocale(LC_ALL, "");
 | 
				
			||||||
	pw_init(&argc, &argv);
 | 
						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);
 | 
								pw_properties_set(data.playback_props, PW_KEY_TARGET_OBJECT, optarg);
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		case 'i':
 | 
							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));
 | 
								pw_properties_update_string(data.capture_props, optarg, strlen(optarg));
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		case 'o':
 | 
							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));
 | 
								pw_properties_update_string(data.playback_props, optarg, strlen(optarg));
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue