aboutsummaryrefslogtreecommitdiff
path: root/test_utils/test_main.c
diff options
context:
space:
mode:
Diffstat (limited to 'test_utils/test_main.c')
-rw-r--r--test_utils/test_main.c87
1 files changed, 58 insertions, 29 deletions
diff --git a/test_utils/test_main.c b/test_utils/test_main.c
index 116da2315439..74250ad748c2 100644
--- a/test_utils/test_main.c
+++ b/test_utils/test_main.c
@@ -426,7 +426,7 @@ failure(const char *fmt, ...)
nextmsg = NULL;
} else {
va_start(ap, fmt);
- vsprintf(msgbuff, fmt, ap);
+ vsnprintf(msgbuff, sizeof(msgbuff), fmt, ap);
va_end(ap);
nextmsg = msgbuff;
}
@@ -551,7 +551,7 @@ test_skipping(const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
- vsprintf(buff, fmt, ap);
+ vsnprintf(buff, sizeof(buff), fmt, ap);
va_end(ap);
/* Use failure() message if set. */
msg = nextmsg;
@@ -1970,7 +1970,12 @@ assertion_make_file(const char *file, int line,
failure_finish(NULL);
return (0);
}
- if (0 != chmod(path, mode)) {
+#ifdef HAVE_FCHMOD
+ if (0 != fchmod(fd, mode))
+#else
+ if (0 != chmod(path, mode))
+#endif
+ {
failure_start(file, line, "Could not chmod %s", path);
failure_finish(NULL);
close(fd);
@@ -3065,7 +3070,7 @@ systemf(const char *fmt, ...)
int r;
va_start(ap, fmt);
- vsprintf(buff, fmt, ap);
+ vsnprintf(buff, sizeof(buff), fmt, ap);
if (verbosity > VERBOSITY_FULL)
logprintf("Cmd: %s\n", buff);
r = system(buff);
@@ -3090,7 +3095,7 @@ slurpfile(size_t * sizep, const char *fmt, ...)
int r;
va_start(ap, fmt);
- vsprintf(filename, fmt, ap);
+ vsnprintf(filename, sizeof(filename), fmt, ap);
va_end(ap);
f = fopen(filename, "rb");
@@ -3157,7 +3162,7 @@ extract_reference_file(const char *name)
char buff[1024];
FILE *in, *out;
- sprintf(buff, "%s/%s.uu", refdir, name);
+ snprintf(buff, sizeof(buff), "%s/%s.uu", refdir, name);
in = fopen(buff, "r");
failure("Couldn't open reference file %s", buff);
assert(in != NULL);
@@ -3185,14 +3190,12 @@ extract_reference_file(const char *name)
while (bytes > 0) {
int n = 0;
/* Write out 1-3 bytes from that. */
- if (bytes > 0) {
- assert(VALID_UUDECODE(p[0]));
- assert(VALID_UUDECODE(p[1]));
- n = UUDECODE(*p++) << 18;
- n |= UUDECODE(*p++) << 12;
- fputc(n >> 16, out);
- --bytes;
- }
+ assert(VALID_UUDECODE(p[0]));
+ assert(VALID_UUDECODE(p[1]));
+ n = UUDECODE(*p++) << 18;
+ n |= UUDECODE(*p++) << 12;
+ fputc(n >> 16, out);
+ --bytes;
if (bytes > 0) {
assert(VALID_UUDECODE(p[0]));
n |= UUDECODE(*p++) << 6;
@@ -3218,7 +3221,7 @@ copy_reference_file(const char *name)
FILE *in, *out;
size_t rbytes;
- sprintf(buff, "%s/%s", refdir, name);
+ snprintf(buff, sizeof(buff), "%s/%s", refdir, name);
in = fopen(buff, "rb");
failure("Couldn't open reference file %s", buff);
assert(in != NULL);
@@ -3545,7 +3548,7 @@ test_run(int i, const char *tmpdir)
exit(1);
}
/* Create a log file for this test. */
- sprintf(logfilename, "%s.log", tests[i].name);
+ snprintf(logfilename, sizeof(logfilename), "%s.log", tests[i].name);
logfile = fopen(logfilename, "w");
fprintf(logfile, "%s\n\n", tests[i].name);
/* Chdir() to a work dir for this specific test. */
@@ -3859,7 +3862,19 @@ main(int argc, char **argv)
static const int limit = sizeof(tests) / sizeof(tests[0]);
int test_set[sizeof(tests) / sizeof(tests[0])];
int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
+ int testprogdir_len;
+#ifdef PROGRAM
+ int tmp2_len;
+#endif
time_t now;
+ struct tm *tmptr;
+#if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
+ struct tm tmbuf;
+#endif
+#if defined(HAVE__LOCALTIME64_S)
+ errno_t terr;
+ __time64_t tmptime;
+#endif
char *refdir_alloc = NULL;
const char *progname;
char **saved_argv;
@@ -3895,12 +3910,13 @@ main(int argc, char **argv)
* tree.
*/
progname = p = argv[0];
- if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
+ testprogdir_len = strlen(progname) + 1;
+ if ((testprogdir = (char *)malloc(testprogdir_len)) == NULL)
{
fprintf(stderr, "ERROR: Out of memory.");
exit(1);
}
- strcpy(testprogdir, progname);
+ strncpy(testprogdir, progname, testprogdir_len);
while (*p != '\0') {
/* Support \ or / dir separators for Windows compat. */
if (*p == '/' || *p == '\\')
@@ -4042,20 +4058,21 @@ main(int argc, char **argv)
#ifdef PROGRAM
if (testprogfile == NULL)
{
- if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
- strlen(PROGRAM) + 1)) == NULL)
+ tmp2_len = strlen(testprogdir) + 1 + strlen(PROGRAM) + 1;
+ if ((tmp2 = (char *)malloc(tmp2_len)) == NULL)
{
fprintf(stderr, "ERROR: Out of memory.");
exit(1);
}
- strcpy(tmp2, testprogdir);
- strcat(tmp2, "/");
- strcat(tmp2, PROGRAM);
+ strncpy(tmp2, testprogdir, tmp2_len);
+ strncat(tmp2, "/", tmp2_len);
+ strncat(tmp2, PROGRAM, tmp2_len);
testprogfile = tmp2;
}
{
char *testprg;
+ int testprg_len;
#if defined(_WIN32) && !defined(__CYGWIN__)
/* Command.com sometimes rejects '/' separators. */
testprg = strdup(testprogfile);
@@ -4066,10 +4083,11 @@ main(int argc, char **argv)
testprogfile = testprg;
#endif
/* Quote the name that gets put into shell command lines. */
- testprg = malloc(strlen(testprogfile) + 3);
- strcpy(testprg, "\"");
- strcat(testprg, testprogfile);
- strcat(testprg, "\"");
+ testprg_len = strlen(testprogfile) + 3;
+ testprg = malloc(testprg_len);
+ strncpy(testprg, "\"", testprg_len);
+ strncat(testprg, testprogfile, testprg_len);
+ strncat(testprg, "\"", testprg_len);
testprog = testprg;
}
#endif
@@ -4091,9 +4109,20 @@ main(int argc, char **argv)
*/
now = time(NULL);
for (i = 0; ; i++) {
+#if defined(HAVE_LOCALTIME_R)
+ tmptr = localtime_r(&now, &tmbuf);
+#elif defined(HAVE__LOCALTIME64_S)
+ tmptime = now;
+ terr = _localtime64_s(&tmbuf, &tmptime);
+ if (terr)
+ tmptr = NULL;
+ else
+ tmptr = &tmbuf;
+#else
+ tmptr = localtime(&now);
+#endif
strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
- "%Y-%m-%dT%H.%M.%S",
- localtime(&now));
+ "%Y-%m-%dT%H.%M.%S", tmptr);
if ((strlen(tmp) + 1 + strlen(progname) + 1 +
strlen(tmpdir_timestamp) + 1 + 3) >
(sizeof(tmpdir) / sizeof(char))) {