You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fmt/fmt-big-endian-fixes.patch

69 lines
2.2 KiB

From 214cf13f17c7177912cc8f9b6c070c4b3a282c36 Mon Sep 17 00:00:00 2001
From: Vladislav Shchapov <phprus@gmail.com>
Date: Wed, 5 Jan 2022 03:23:42 +0500
Subject: [PATCH] Fix endianness bug in write_digit2_separated (#2699)
* Fix endianness bug in write_digit2_separated
* Move endianness check to compile time if it possible
* Turn 8 into a constant
---
include/fmt/chrono.h | 10 +++++++++-
include/fmt/format.h | 10 +++++++++-
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h
index 908999ab5f..682efd8d21 100644
--- a/include/fmt/chrono.h
+++ b/include/fmt/chrono.h
@@ -558,7 +558,15 @@ inline void write_digit2_separated(char* buf, unsigned a, unsigned b,
auto usep = static_cast<unsigned long long>(sep);
// Add ASCII '0' to each digit byte and insert separators.
digits |= 0x3030003030003030 | (usep << 16) | (usep << 40);
- memcpy(buf, &digits, 8);
+
+ constexpr const size_t len = 8;
+ if (const_check(is_big_endian())) {
+ char tmp[len];
+ memcpy(tmp, &digits, len);
+ std::reverse_copy(tmp, tmp + len, buf);
+ } else {
+ memcpy(buf, &digits, len);
+ }
}
template <typename Period> FMT_CONSTEXPR inline const char* get_units() {
diff --git a/include/fmt/format.h b/include/fmt/format.h
index a6f1fdc89e..8b789574ec 100644
--- a/include/fmt/format.h
+++ b/include/fmt/format.h
@@ -296,10 +296,18 @@ FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To {
}
inline auto is_big_endian() -> bool {
+#ifdef _WIN32
+ return false;
+#elif defined(__BIG_ENDIAN__)
+ return true;
+#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
+ return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
+#else
struct bytes {
char data[sizeof(int)];
};
return bit_cast<bytes>(1).data[0] == 0;
+#endif
}
// A fallback implementation of uintptr_t for systems that lack it.
@@ -309,7 +317,7 @@ struct fallback_uintptr {
fallback_uintptr() = default;
explicit fallback_uintptr(const void* p) {
*this = bit_cast<fallback_uintptr>(p);
- if (is_big_endian()) {
+ if (const_check(is_big_endian())) {
for (size_t i = 0, j = sizeof(void*) - 1; i < j; ++i, --j)
std::swap(value[i], value[j]);
}