Skip to content

Commit dc888f7

Browse files
tests/tr1: Fix sporadic failures caused by tmpnam/_tempnam (#2210)
1 parent 5ebbfd4 commit dc888f7

File tree

5 files changed

+47
-38
lines changed

5 files changed

+47
-38
lines changed

tests/tr1/include/temp_file_name.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
#pragma once
5+
6+
#include <random>
7+
#include <string>
8+
9+
[[nodiscard]] inline std::string temp_file_name() {
10+
std::string ret{"temp_file_"};
11+
std::uniform_int_distribution<int> dist{0, 15};
12+
std::random_device rd;
13+
14+
for (int i = 0; i < 64; ++i) { // 64 hexits = 256 bits of entropy
15+
ret.push_back("0123456789ABCDEF"[dist(rd)]);
16+
}
17+
18+
ret += ".tmp";
19+
20+
return ret;
21+
}

tests/tr1/tests/cstdio/test.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define TEST_NAMEX "<cstdio>"
66

77
#include "tdefs.h"
8+
#include "temp_file_name.h"
89
#include <assert.h>
910
#include <cstdio>
1011
#include <errno.h>
@@ -91,7 +92,8 @@ void test_cpp() { // test C++ header
9192
int in1;
9293
long off;
9394

94-
assert((tn = STDx tmpnam((char*) nullptr)) != nullptr);
95+
const auto temp_name = temp_file_name();
96+
tn = temp_name.c_str();
9597
assert((pf = STDx fopen(tn, "w+")) != nullptr);
9698

9799
STDx setbuf(pf, (char*) nullptr);
@@ -124,18 +126,17 @@ void test_cpp() { // test C++ header
124126
}
125127

126128
{ // test character I/O
127-
const char* tmpbuff;
128129
char tname[L_tmpnam];
129-
char tmpbuf[L_tmpnam];
130+
char tn[100];
130131
STDx FILE* pf;
131-
char tn[100] = {0};
132132

133-
assert(STDx tmpnam(tmpbuf) == tmpbuf);
134-
CHECK(CSTD strlen(tmpbuf) < L_tmpnam);
135-
assert((tmpbuff = STDx tmpnam((char*) nullptr)) != nullptr);
133+
const auto temp_name1 = temp_file_name();
134+
CHECK(temp_name1.size() < sizeof(tname));
135+
CSTD strcpy_s(tname, sizeof(tname), temp_name1.c_str());
136136

137-
CSTD strcpy_s(tn, sizeof(tn), tmpbuff);
138-
CSTD strcpy_s(tname, sizeof(tname), tmpbuf);
137+
const auto temp_name2 = temp_file_name();
138+
CHECK(temp_name2.size() < sizeof(tn));
139+
CSTD strcpy_s(tn, sizeof(tn), temp_name2.c_str());
139140

140141
CHECK(CSTD strcmp(tn, tname) != 0);
141142
assert((pf = STDx fopen(tname, "w")) != nullptr);

tests/tr1/tests/cwchar1/test.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define TEST_NAMEX "<cwchar>, part 1"
66

77
#include "tdefs.h"
8+
#include "temp_file_name.h"
89
#include <assert.h>
910
#include <cwchar>
1011
#include <stdarg.h>
@@ -13,9 +14,6 @@
1314

1415
#pragma warning(disable : 4793) // function compiled as native
1516

16-
#define NO_TMPNAM_TESTS 1
17-
#undef tmpnam
18-
#define tmpnam(x) _tempnam(".", "")
1917

2018
#undef clearerr // tested in stdio2.c
2119
#undef feof
@@ -101,7 +99,8 @@ void test_cpp() { // test C++ header
10199
int in1;
102100
long off;
103101

104-
assert((tn = CSTD tmpnam((char*) nullptr)) != nullptr);
102+
const auto temp_name = temp_file_name();
103+
tn = temp_name.c_str();
105104
assert((pf = CSTD fopen(tn, "w+")) != nullptr);
106105
CHECK_INT(STDx fwide(pf, 0), 0);
107106
CHECK_INT(STDx fwprintf(pf, L"123\n"), 4);
@@ -141,19 +140,15 @@ void test_cpp() { // test C++ header
141140

142141
CHECK(wmacs[1] < wmacs[0]);
143142

144-
#if NO_TMPNAM_TESTS
145-
char *tname, *tn;
146-
assert((tn = CSTD tmpnam((char*) nullptr)) != nullptr);
147-
tname = (char*) CSTD malloc(CSTD strlen(tn) + 1);
143+
char* tname;
144+
const char* tn;
145+
const auto temp_name1 = temp_file_name();
146+
tn = temp_name1.c_str();
147+
tname = (char*) CSTD malloc(CSTD strlen(tn) + 1);
148148
CSTD strcpy(tname, tn);
149149

150-
#else // NO_TMPNAM_TESTS
151-
char tname[L_tmpnam], *tn;
152-
CHECK_PTR(CSTD tmpnam(tname), tname);
153-
assert(CSTD strlen(tname) < L_tmpnam);
154-
#endif // NO_TMPNAM_TESTS
155-
156-
assert((tn = CSTD tmpnam((char*) nullptr)) != nullptr);
150+
const auto temp_name2 = temp_file_name();
151+
tn = temp_name2.c_str();
157152
CHECK(CSTD strcmp(tn, tname) != 0);
158153
assert((pf = CSTD fopen(tname, "w")) != nullptr);
159154
CHECK_INT(STDx fgetwc(pf), wintval);

tests/tr1/tests/fstream1/test.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,14 @@
55
#define TEST_NAME "<fstream>, part 1"
66

77
#include "tdefs.h"
8+
#include "temp_file_name.h"
89
#include <assert.h>
910
#include <fstream>
1011
#include <string>
1112

12-
#undef tmpnam
13-
#define tmpnam(x) _tempnam(".", "")
14-
1513
void test_main() { // test basic workings of char fstream definitions
16-
const char* tn = CSTD tmpnam(0);
17-
18-
assert(tn != nullptr);
19-
20-
STD string tn_str(tn);
14+
STD string tn_str = temp_file_name();
15+
const char* tn = tn_str.c_str();
2116

2217
// test output file opening
2318
STD ofstream ofs(tn);

tests/tr1/tests/fstream2/test.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55
#define TEST_NAME "<fstream>, part 2"
66

77
#include "tdefs.h"
8+
#include "temp_file_name.h"
89
#include <assert.h>
910
#include <fstream>
1011
#include <wchar.h>
1112

12-
#undef tmpnam
13-
#define tmpnam(x) _tempnam(".", "")
14-
1513
void test_main() { // test basic workings of wide fstream definitions
16-
const char* tn = CSTD tmpnam(0);
17-
18-
assert(tn != nullptr);
14+
const auto temp_name = temp_file_name();
15+
const char* tn = temp_name.c_str();
1916

2017
// test output file opening
2118
STD wofstream ofs(tn);

0 commit comments

Comments
 (0)