From ac13b70a89a16490ff69e0d92bffa818a59e3ed5 Mon Sep 17 00:00:00 2001 From: Marc Hoersken Date: Mon, 22 Jun 2020 08:21:26 +0200 Subject: [PATCH] tests: avoid use of banned function strncat (#489) --- tests/openssh_fixture.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/openssh_fixture.c b/tests/openssh_fixture.c index 62719cd..7112903 100644 --- a/tests/openssh_fixture.c +++ b/tests/openssh_fixture.c @@ -62,10 +62,12 @@ static int run_command_varg(char **output, const char *command, va_list args) { FILE *pipe; + char redirect_stderr[] = "%s 2>&1"; char command_buf[BUFSIZ]; char buf[BUFSIZ]; char *p; int ret; + if(output) { *output = NULL; } @@ -78,18 +80,22 @@ static int run_command_varg(char **output, const char *command, va_list args) } /* Rewrite the command to redirect stderr to stdout to we can output it */ - if(strlen(command_buf) + 6 >= sizeof(command_buf)) { + if(strlen(command_buf) + strlen(redirect_stderr) >= sizeof(buf)) { fprintf(stderr, "Unable to rewrite command (%s)\n", command); return -1; } - strncat(command_buf, " 2>&1", 6); + ret = snprintf(buf, sizeof(buf), redirect_stderr, command_buf); + if(ret < 0 || ret >= BUFSIZ) { + fprintf(stderr, "Unable to rewrite command (%s)\n", command); + return -1; + } fprintf(stdout, "Command: %s\n", command); #ifdef WIN32 - pipe = _popen(command_buf, "r"); + pipe = _popen(buf, "r"); #else - pipe = popen(command_buf, "r"); + pipe = popen(buf, "r"); #endif if(!pipe) { fprintf(stderr, "Unable to execute command '%s'\n", command);