Skip to content

Commit 442d5c8

Browse files
authored
Avoid modifying the string in a constinit expression (in case that's the cause of the failure rather than #embed itself)
1 parent 658a6d0 commit 442d5c8

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

Sources/_TestingInternals/Versions.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,32 @@
1313
#include <array>
1414
#include <algorithm>
1515
#include <iterator>
16+
#include <mutex>
1617

1718
const char *swt_getTestingLibraryVersion(void) {
1819
#if defined(SWT_TESTING_LIBRARY_VERSION)
1920
// The current environment explicitly specifies a version string to return.
2021
return SWT_TESTING_LIBRARY_VERSION;
2122
#elif __clang_major__ >= 17 && defined(__has_embed)
2223
#if __has_embed("../../VERSION.txt")
23-
static constinit auto version = [] () constexpr {
24-
// Read the version from version.txt at the root of the package's repo.
25-
char version[] = {
24+
// Read the version from version.txt at the root of the package's repo.
25+
static char version[] = {
2626
#pragma clang diagnostic push
2727
#pragma clang diagnostic ignored "-Wc23-extensions"
2828
#embed "../../VERSION.txt"
2929
#pragma clang diagnostic pop
30-
};
30+
};
3131

32-
// Copy the first line from the C string into a C array so that we can
33-
// return it from this closure.
34-
std::array<char, std::size(version) + 1> result {};
32+
// Zero out the newline character and anything after it.
33+
static std::once_flag once;
34+
std::call_once(once, [] {
3535
auto i = std::find_if(std::begin(version), std::end(version), [] (char c) {
3636
return c == '\r' || c == '\n';
3737
});
38-
std::copy(std::begin(version), i, result.begin());
39-
return result;
40-
}();
38+
std::fill(i, std::end(version), '\0');
39+
});
4140

42-
return version.data();
41+
return version;
4342
#else
4443
#warning SWT_TESTING_LIBRARY_VERSION not defined and VERSION.txt not found: testing library version is unavailable
4544
return nullptr;

0 commit comments

Comments
 (0)