From 5ebaa3876a5312e72016ea7b0a8c8f108e0326ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Thu, 1 Aug 2024 17:26:54 +0200 Subject: [PATCH] Avoid duplicating the path separator. --- test_utils/test_main.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/test_utils/test_main.c b/test_utils/test_main.c index 848e58a4e..c6589fa31 100644 --- a/test_utils/test_main.c +++ b/test_utils/test_main.c @@ -3888,6 +3888,7 @@ main(int argc, char **argv) int test_set[limit]; int i = 0, j = 0, tests_run = 0, tests_failed = 0, option; size_t testprogdir_len; + size_t tmplen; #ifdef PROGRAM size_t tmp2_len; #endif @@ -3987,6 +3988,9 @@ main(int argc, char **argv) tmp = getenv("TEMPDIR"); else tmp = "/tmp"; + tmplen = strlen(tmp); + while (tmplen > 0 && tmp[tmplen - 1] == '/') + tmplen--; /* Allow -d to be controlled through the environment. */ if (getenv(ENVBASE "_DEBUG") != NULL) @@ -4139,16 +4143,16 @@ main(int argc, char **argv) #endif strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp), "%Y-%m-%dT%H.%M.%S", tmptr); - if ((strlen(tmp) + 1 + strlen(progname) + 1 + - strlen(tmpdir_timestamp) + 1 + 3) > - sizeof(tmpdir)) { + if (tmplen + 1 + strlen(progname) + 1 + + strlen(tmpdir_timestamp) + 1 + 3 >= + ARRAY_SIZE(tmpdir)) { fprintf(stderr, "ERROR: Temp directory pathname too long\n"); exit(1); } - snprintf(tmpdir, sizeof(tmpdir), "%s/%s.%s-%03d", tmp, - progname, tmpdir_timestamp, i); - if (assertMakeDir(tmpdir,0755)) + snprintf(tmpdir, sizeof(tmpdir), "%.*s/%s.%s-%03d", + (int)tmplen, tmp, progname, tmpdir_timestamp, i); + if (assertMakeDir(tmpdir, 0755)) break; if (i >= 999) { fprintf(stderr,