Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/include/internal
diff options
context:
space:
mode:
authorMartin Hořeňovský2019-10-27 22:07:10 +0100
committerMartin Hořeňovský2019-10-27 22:07:10 +0100
commit7ada02e21ee66ac89783011526ca8058f8234053 (patch)
tree6538bc074f0f23893efc8435b0428a4461228f97 /include/internal
parent2fbd66c51cbf3a406dc6bb8ddc6b9a0b96077a72 (diff)
downloadcatch3-7ada02e21ee66ac89783011526ca8058f8234053.tar.gz
Avoid technically UB type punning when determining endianness
Diffstat (limited to 'include/internal')
-rw-r--r--include/internal/catch_tostring.cpp16
1 files changed, 7 insertions, 9 deletions
diff --git a/include/internal/catch_tostring.cpp b/include/internal/catch_tostring.cpp
index 2f66f49d..a289c341 100644
--- a/include/internal/catch_tostring.cpp
+++ b/include/internal/catch_tostring.cpp
@@ -38,13 +38,11 @@ namespace Detail {
enum Arch { Big, Little };
static Arch which() {
- union _{
- int asInt;
- char asChar[sizeof (int)];
- } u;
-
- u.asInt = 1;
- return ( u.asChar[sizeof(int)-1] == 1 ) ? Big : Little;
+ int one = 1;
+ // If the lowest byte we read is non-zero, we can assume
+ // that little endian format is used.
+ auto value = *reinterpret_cast<char*>(&one);
+ return value ? Little : Big;
}
};
}
@@ -241,13 +239,13 @@ std::string StringMaker<std::nullptr_t>::convert(std::nullptr_t) {
}
int StringMaker<float>::precision = 5;
-
+
std::string StringMaker<float>::convert(float value) {
return fpToString(value, precision) + 'f';
}
int StringMaker<double>::precision = 10;
-
+
std::string StringMaker<double>::convert(double value) {
return fpToString(value, precision);
}