From c1cc4868e21f3c4f7d0f2416a5f20b9433ae7e2d Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Fri, 17 Sep 2021 15:33:12 -0700 Subject: [PATCH 1/9] Eliminate `NO_TMPNAM_TESTS` logic. --- tests/tr1/tests/cwchar1/test.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/tr1/tests/cwchar1/test.cpp b/tests/tr1/tests/cwchar1/test.cpp index 3d9b7b8ab15..d0b42f49ce4 100644 --- a/tests/tr1/tests/cwchar1/test.cpp +++ b/tests/tr1/tests/cwchar1/test.cpp @@ -13,7 +13,6 @@ #pragma warning(disable : 4793) // function compiled as native -#define NO_TMPNAM_TESTS 1 #undef tmpnam #define tmpnam(x) _tempnam(".", "") @@ -141,18 +140,11 @@ void test_cpp() { // test C++ header CHECK(wmacs[1] < wmacs[0]); -#if NO_TMPNAM_TESTS char *tname, *tn; assert((tn = CSTD tmpnam((char*) nullptr)) != nullptr); tname = (char*) CSTD malloc(CSTD strlen(tn) + 1); CSTD strcpy(tname, tn); -#else // NO_TMPNAM_TESTS - char tname[L_tmpnam], *tn; - CHECK_PTR(CSTD tmpnam(tname), tname); - assert(CSTD strlen(tname) < L_tmpnam); -#endif // NO_TMPNAM_TESTS - assert((tn = CSTD tmpnam((char*) nullptr)) != nullptr); CHECK(CSTD strcmp(tn, tname) != 0); assert((pf = CSTD fopen(tname, "w")) != nullptr); From db165a1ea8f102dadc946fe59638c9bdb1520d5c Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Fri, 17 Sep 2021 15:38:01 -0700 Subject: [PATCH 2/9] Eliminate `tmpnam` macroization. --- tests/tr1/tests/cwchar1/test.cpp | 8 +++----- tests/tr1/tests/fstream1/test.cpp | 5 +---- tests/tr1/tests/fstream2/test.cpp | 5 +---- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/tests/tr1/tests/cwchar1/test.cpp b/tests/tr1/tests/cwchar1/test.cpp index d0b42f49ce4..b6ef9cc53f8 100644 --- a/tests/tr1/tests/cwchar1/test.cpp +++ b/tests/tr1/tests/cwchar1/test.cpp @@ -13,8 +13,6 @@ #pragma warning(disable : 4793) // function compiled as native -#undef tmpnam -#define tmpnam(x) _tempnam(".", "") #undef clearerr // tested in stdio2.c #undef feof @@ -100,7 +98,7 @@ void test_cpp() { // test C++ header int in1; long off; - assert((tn = CSTD tmpnam((char*) nullptr)) != nullptr); + assert((tn = CSTD _tempnam(".", "")) != nullptr); assert((pf = CSTD fopen(tn, "w+")) != nullptr); CHECK_INT(STDx fwide(pf, 0), 0); CHECK_INT(STDx fwprintf(pf, L"123\n"), 4); @@ -141,11 +139,11 @@ void test_cpp() { // test C++ header CHECK(wmacs[1] < wmacs[0]); char *tname, *tn; - assert((tn = CSTD tmpnam((char*) nullptr)) != nullptr); + assert((tn = CSTD _tempnam(".", "")) != nullptr); tname = (char*) CSTD malloc(CSTD strlen(tn) + 1); CSTD strcpy(tname, tn); - assert((tn = CSTD tmpnam((char*) nullptr)) != nullptr); + assert((tn = CSTD _tempnam(".", "")) != nullptr); CHECK(CSTD strcmp(tn, tname) != 0); assert((pf = CSTD fopen(tname, "w")) != nullptr); CHECK_INT(STDx fgetwc(pf), wintval); diff --git a/tests/tr1/tests/fstream1/test.cpp b/tests/tr1/tests/fstream1/test.cpp index 2a91e822c18..2a15947f6c3 100644 --- a/tests/tr1/tests/fstream1/test.cpp +++ b/tests/tr1/tests/fstream1/test.cpp @@ -9,11 +9,8 @@ #include #include -#undef tmpnam -#define tmpnam(x) _tempnam(".", "") - void test_main() { // test basic workings of char fstream definitions - const char* tn = CSTD tmpnam(0); + const char* tn = CSTD _tempnam(".", ""); assert(tn != nullptr); diff --git a/tests/tr1/tests/fstream2/test.cpp b/tests/tr1/tests/fstream2/test.cpp index bb45daaef33..03925e53bb7 100644 --- a/tests/tr1/tests/fstream2/test.cpp +++ b/tests/tr1/tests/fstream2/test.cpp @@ -9,11 +9,8 @@ #include #include -#undef tmpnam -#define tmpnam(x) _tempnam(".", "") - void test_main() { // test basic workings of wide fstream definitions - const char* tn = CSTD tmpnam(0); + const char* tn = CSTD _tempnam(".", ""); assert(tn != nullptr); From c875d492b9d9bdccbe6b9fae69f6e63fcd9664c6 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Fri, 17 Sep 2021 17:21:45 -0700 Subject: [PATCH 3/9] Add temp_file_name.h. --- tests/tr1/include/temp_file_name.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/tr1/include/temp_file_name.h diff --git a/tests/tr1/include/temp_file_name.h b/tests/tr1/include/temp_file_name.h new file mode 100644 index 00000000000..938fe24669c --- /dev/null +++ b/tests/tr1/include/temp_file_name.h @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#pragma once + +#include +#include + +[[nodiscard]] inline std::string temp_file_name() { + std::string ret{"temp_file_"}; + + std::random_device rd; + + for (int i = 0; i < 64; ++i) { // 64 hexits = 256 bits of entropy + ret.push_back("0123456789ABCDEF"[rd() % 16]); + } + + ret += ".tmp"; + + return ret; +} From 1fc25646f0c2a6db8f587aa4e1606380adb10619 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Fri, 17 Sep 2021 17:55:45 -0700 Subject: [PATCH 4/9] Convert tmpnam() calls in cstdio/test.cpp. --- tests/tr1/tests/cstdio/test.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/tr1/tests/cstdio/test.cpp b/tests/tr1/tests/cstdio/test.cpp index 7a6b0585a7d..38ab0183438 100644 --- a/tests/tr1/tests/cstdio/test.cpp +++ b/tests/tr1/tests/cstdio/test.cpp @@ -5,6 +5,7 @@ #define TEST_NAMEX "" #include "tdefs.h" +#include "temp_file_name.h" #include #include #include @@ -91,7 +92,8 @@ void test_cpp() { // test C++ header int in1; long off; - assert((tn = STDx tmpnam((char*) nullptr)) != nullptr); + const auto temp_name = temp_file_name(); + assert((tn = temp_name.c_str()) != nullptr); assert((pf = STDx fopen(tn, "w+")) != nullptr); STDx setbuf(pf, (char*) nullptr); @@ -126,13 +128,15 @@ void test_cpp() { // test C++ header { // test character I/O const char* tmpbuff; char tname[L_tmpnam]; - char tmpbuf[L_tmpnam]; + const char* tmpbuf; STDx FILE* pf; char tn[100] = {0}; - assert(STDx tmpnam(tmpbuf) == tmpbuf); + const auto temp_name1 = temp_file_name(); + assert((tmpbuf = temp_name1.c_str()) != nullptr); CHECK(CSTD strlen(tmpbuf) < L_tmpnam); - assert((tmpbuff = STDx tmpnam((char*) nullptr)) != nullptr); + const auto temp_name2 = temp_file_name(); + assert((tmpbuff = temp_name2.c_str()) != nullptr); CSTD strcpy_s(tn, sizeof(tn), tmpbuff); CSTD strcpy_s(tname, sizeof(tname), tmpbuf); From d64ce4f1658fa9968efbca534110f008a51dda87 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Fri, 17 Sep 2021 18:07:05 -0700 Subject: [PATCH 5/9] Convert _tempnam() calls in cwchar1, fstream1, fstream2. --- tests/tr1/tests/cwchar1/test.cpp | 13 +++++++++---- tests/tr1/tests/fstream1/test.cpp | 4 +++- tests/tr1/tests/fstream2/test.cpp | 4 +++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/tests/tr1/tests/cwchar1/test.cpp b/tests/tr1/tests/cwchar1/test.cpp index b6ef9cc53f8..6ba8836d623 100644 --- a/tests/tr1/tests/cwchar1/test.cpp +++ b/tests/tr1/tests/cwchar1/test.cpp @@ -5,6 +5,7 @@ #define TEST_NAMEX ", part 1" #include "tdefs.h" +#include "temp_file_name.h" #include #include #include @@ -98,7 +99,8 @@ void test_cpp() { // test C++ header int in1; long off; - assert((tn = CSTD _tempnam(".", "")) != nullptr); + const auto temp_name = temp_file_name(); + assert((tn = temp_name.c_str()) != nullptr); assert((pf = CSTD fopen(tn, "w+")) != nullptr); CHECK_INT(STDx fwide(pf, 0), 0); CHECK_INT(STDx fwprintf(pf, L"123\n"), 4); @@ -138,12 +140,15 @@ void test_cpp() { // test C++ header CHECK(wmacs[1] < wmacs[0]); - char *tname, *tn; - assert((tn = CSTD _tempnam(".", "")) != nullptr); + char* tname; + const char* tn; + const auto temp_name1 = temp_file_name(); + assert((tn = temp_name1.c_str()) != nullptr); tname = (char*) CSTD malloc(CSTD strlen(tn) + 1); CSTD strcpy(tname, tn); - assert((tn = CSTD _tempnam(".", "")) != nullptr); + const auto temp_name2 = temp_file_name(); + assert((tn = temp_name2.c_str()) != nullptr); CHECK(CSTD strcmp(tn, tname) != 0); assert((pf = CSTD fopen(tname, "w")) != nullptr); CHECK_INT(STDx fgetwc(pf), wintval); diff --git a/tests/tr1/tests/fstream1/test.cpp b/tests/tr1/tests/fstream1/test.cpp index 2a15947f6c3..9380c2b34fd 100644 --- a/tests/tr1/tests/fstream1/test.cpp +++ b/tests/tr1/tests/fstream1/test.cpp @@ -5,12 +5,14 @@ #define TEST_NAME ", part 1" #include "tdefs.h" +#include "temp_file_name.h" #include #include #include void test_main() { // test basic workings of char fstream definitions - const char* tn = CSTD _tempnam(".", ""); + const auto temp_name = temp_file_name(); + const char* tn = temp_name.c_str(); assert(tn != nullptr); diff --git a/tests/tr1/tests/fstream2/test.cpp b/tests/tr1/tests/fstream2/test.cpp index 03925e53bb7..c8eef3026f6 100644 --- a/tests/tr1/tests/fstream2/test.cpp +++ b/tests/tr1/tests/fstream2/test.cpp @@ -5,12 +5,14 @@ #define TEST_NAME ", part 2" #include "tdefs.h" +#include "temp_file_name.h" #include #include #include void test_main() { // test basic workings of wide fstream definitions - const char* tn = CSTD _tempnam(".", ""); + const auto temp_name = temp_file_name(); + const char* tn = temp_name.c_str(); assert(tn != nullptr); From aa0782eac36e216c3bc6e87aef9c1ab0abe06f1b Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Mon, 20 Sep 2021 15:03:06 -0700 Subject: [PATCH 6/9] Use uniform_int_distribution. --- tests/tr1/include/temp_file_name.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tr1/include/temp_file_name.h b/tests/tr1/include/temp_file_name.h index 938fe24669c..79991f56f20 100644 --- a/tests/tr1/include/temp_file_name.h +++ b/tests/tr1/include/temp_file_name.h @@ -8,11 +8,11 @@ [[nodiscard]] inline std::string temp_file_name() { std::string ret{"temp_file_"}; - + std::uniform_int_distribution dist{0, 15}; std::random_device rd; for (int i = 0; i < 64; ++i) { // 64 hexits = 256 bits of entropy - ret.push_back("0123456789ABCDEF"[rd() % 16]); + ret.push_back("0123456789ABCDEF"[dist(rd)]); } ret += ".tmp"; From 1c1a509f4dbc1f20c460767c35d21ce5ed9a821c Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Mon, 20 Sep 2021 15:30:05 -0700 Subject: [PATCH 7/9] Don't bother asserting `c_str() != nullptr`. --- tests/tr1/tests/cstdio/test.cpp | 6 +++--- tests/tr1/tests/cwchar1/test.cpp | 8 ++++---- tests/tr1/tests/fstream1/test.cpp | 2 -- tests/tr1/tests/fstream2/test.cpp | 2 -- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/tests/tr1/tests/cstdio/test.cpp b/tests/tr1/tests/cstdio/test.cpp index 38ab0183438..a18cd82c080 100644 --- a/tests/tr1/tests/cstdio/test.cpp +++ b/tests/tr1/tests/cstdio/test.cpp @@ -93,7 +93,7 @@ void test_cpp() { // test C++ header long off; const auto temp_name = temp_file_name(); - assert((tn = temp_name.c_str()) != nullptr); + tn = temp_name.c_str(); assert((pf = STDx fopen(tn, "w+")) != nullptr); STDx setbuf(pf, (char*) nullptr); @@ -133,10 +133,10 @@ void test_cpp() { // test C++ header char tn[100] = {0}; const auto temp_name1 = temp_file_name(); - assert((tmpbuf = temp_name1.c_str()) != nullptr); + tmpbuf = temp_name1.c_str(); CHECK(CSTD strlen(tmpbuf) < L_tmpnam); const auto temp_name2 = temp_file_name(); - assert((tmpbuff = temp_name2.c_str()) != nullptr); + tmpbuff = temp_name2.c_str(); CSTD strcpy_s(tn, sizeof(tn), tmpbuff); CSTD strcpy_s(tname, sizeof(tname), tmpbuf); diff --git a/tests/tr1/tests/cwchar1/test.cpp b/tests/tr1/tests/cwchar1/test.cpp index 6ba8836d623..d773b7b00a8 100644 --- a/tests/tr1/tests/cwchar1/test.cpp +++ b/tests/tr1/tests/cwchar1/test.cpp @@ -100,7 +100,7 @@ void test_cpp() { // test C++ header long off; const auto temp_name = temp_file_name(); - assert((tn = temp_name.c_str()) != nullptr); + tn = temp_name.c_str(); assert((pf = CSTD fopen(tn, "w+")) != nullptr); CHECK_INT(STDx fwide(pf, 0), 0); CHECK_INT(STDx fwprintf(pf, L"123\n"), 4); @@ -143,12 +143,12 @@ void test_cpp() { // test C++ header char* tname; const char* tn; const auto temp_name1 = temp_file_name(); - assert((tn = temp_name1.c_str()) != nullptr); - tname = (char*) CSTD malloc(CSTD strlen(tn) + 1); + tn = temp_name1.c_str(); + tname = (char*) CSTD malloc(CSTD strlen(tn) + 1); CSTD strcpy(tname, tn); const auto temp_name2 = temp_file_name(); - assert((tn = temp_name2.c_str()) != nullptr); + tn = temp_name2.c_str(); CHECK(CSTD strcmp(tn, tname) != 0); assert((pf = CSTD fopen(tname, "w")) != nullptr); CHECK_INT(STDx fgetwc(pf), wintval); diff --git a/tests/tr1/tests/fstream1/test.cpp b/tests/tr1/tests/fstream1/test.cpp index 9380c2b34fd..aae97bc9c82 100644 --- a/tests/tr1/tests/fstream1/test.cpp +++ b/tests/tr1/tests/fstream1/test.cpp @@ -14,8 +14,6 @@ void test_main() { // test basic workings of char fstream definitions const auto temp_name = temp_file_name(); const char* tn = temp_name.c_str(); - assert(tn != nullptr); - STD string tn_str(tn); // test output file opening diff --git a/tests/tr1/tests/fstream2/test.cpp b/tests/tr1/tests/fstream2/test.cpp index c8eef3026f6..b0401459493 100644 --- a/tests/tr1/tests/fstream2/test.cpp +++ b/tests/tr1/tests/fstream2/test.cpp @@ -14,8 +14,6 @@ void test_main() { // test basic workings of wide fstream definitions const auto temp_name = temp_file_name(); const char* tn = temp_name.c_str(); - assert(tn != nullptr); - // test output file opening STD wofstream ofs(tn); CHECK(ofs.is_open()); From dc6dc9eec1bc811f63e950d8fb53ca68aacf3f57 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Mon, 20 Sep 2021 15:33:23 -0700 Subject: [PATCH 8/9] Use `tn_str` to store `temp_file_name()`. --- tests/tr1/tests/fstream1/test.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/tr1/tests/fstream1/test.cpp b/tests/tr1/tests/fstream1/test.cpp index aae97bc9c82..af558e99fd6 100644 --- a/tests/tr1/tests/fstream1/test.cpp +++ b/tests/tr1/tests/fstream1/test.cpp @@ -11,10 +11,8 @@ #include void test_main() { // test basic workings of char fstream definitions - const auto temp_name = temp_file_name(); - const char* tn = temp_name.c_str(); - - STD string tn_str(tn); + STD string tn_str = temp_file_name(); + const char* tn = tn_str.c_str(); // test output file opening STD ofstream ofs(tn); From 04843c77ae91c5db1425f4f8920b0d88f30aeb89 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Mon, 20 Sep 2021 15:46:28 -0700 Subject: [PATCH 9/9] Eliminate tmpbuf/tmpbuff, reorganize tname/tn. --- tests/tr1/tests/cstdio/test.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/tests/tr1/tests/cstdio/test.cpp b/tests/tr1/tests/cstdio/test.cpp index a18cd82c080..e8c490d88cc 100644 --- a/tests/tr1/tests/cstdio/test.cpp +++ b/tests/tr1/tests/cstdio/test.cpp @@ -126,20 +126,17 @@ void test_cpp() { // test C++ header } { // test character I/O - const char* tmpbuff; char tname[L_tmpnam]; - const char* tmpbuf; + char tn[100]; STDx FILE* pf; - char tn[100] = {0}; const auto temp_name1 = temp_file_name(); - tmpbuf = temp_name1.c_str(); - CHECK(CSTD strlen(tmpbuf) < L_tmpnam); - const auto temp_name2 = temp_file_name(); - tmpbuff = temp_name2.c_str(); + CHECK(temp_name1.size() < sizeof(tname)); + CSTD strcpy_s(tname, sizeof(tname), temp_name1.c_str()); - CSTD strcpy_s(tn, sizeof(tn), tmpbuff); - CSTD strcpy_s(tname, sizeof(tname), tmpbuf); + const auto temp_name2 = temp_file_name(); + CHECK(temp_name2.size() < sizeof(tn)); + CSTD strcpy_s(tn, sizeof(tn), temp_name2.c_str()); CHECK(CSTD strcmp(tn, tname) != 0); assert((pf = STDx fopen(tname, "w")) != nullptr);