From 6d548acf3247ba8e5ff04ef1ab2a6ac5f58dffc3 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Mon, 30 Oct 2023 17:23:30 +0100 Subject: [PATCH] ug_input: refuse non-numeric opt Currently only port number is accepted - refuse any option that doesn't match this. \+ created a separate function for parsing --- src/video_capture/ug_input.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/video_capture/ug_input.cpp b/src/video_capture/ug_input.cpp index a7223e3f2..e73298cda 100644 --- a/src/video_capture/ug_input.cpp +++ b/src/video_capture/ug_input.cpp @@ -100,6 +100,20 @@ void ug_input_state::frame_arrived(struct video_frame *f, struct audio_frame *a) } } +static bool +parse_fmt(const char *fmt, uint16_t *port) +{ + if (fmt[0] == '\0') { + return true; + } + if (!isdigit(fmt[0])) { + MSG(ERROR, "Invalid option: %s\n", fmt); + return false; + } + *port = stoi(fmt); + return true; +} + static int vidcap_ug_input_init(struct vidcap_params *cap_params, void **state) { uint16_t port = 5004; @@ -109,12 +123,12 @@ static int vidcap_ug_input_init(struct vidcap_params *cap_params, void **state) printf("\t-t ug_input[:] [-s embedded]\n"); return VIDCAP_INIT_NOERR; } - ug_input_state *s = new ug_input_state(); - - if (isdigit(vidcap_params_get_fmt(cap_params)[0])) { - port = atoi(vidcap_params_get_fmt(cap_params)); + if (!parse_fmt(vidcap_params_get_fmt(cap_params), &port)) { + return VIDCAP_INIT_FAIL; } + auto *s = new ug_input_state(); + char cfg[128] = ""; snprintf(cfg, sizeof cfg, "%p", s); int ret = initialize_video_display(vidcap_params_get_parent(cap_params), "pipe", cfg, 0, NULL, &s->display);