Skip to content

Commit 23aa467

Browse files
committed
feat: enforce little-endianness for folly::Bits<> storage
The inline docs for `folly::Bits` already mention that storage is expected to be little-endian. This change enforces little-endianness by using `folly::Endian` for load/store operations. This should be a no-op on little-endian systems. The main motivation for this change is that it immediately enables fbthrift Frozen2 files to be exchanged between little- and big-endian systems.
1 parent d2b133f commit 23aa467

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

folly/lang/BitsClass.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ struct BitsTraits<
5858
Unaligned<T>,
5959
typename std::enable_if<(std::is_integral<T>::value)>::type> {
6060
using UnderlyingType = T;
61-
static T load(const Unaligned<T>& x) { return x; }
62-
static void store(Unaligned<T>& x, T v) { x = v; }
61+
static T load(const Unaligned<T>& x) { return Endian::little<T>(x); }
62+
static void store(Unaligned<T>& x, T v) { x = Endian::little<T>(v); }
6363
static T loadRMW(const Unaligned<T>& x) {
6464
FOLLY_PUSH_WARNING
6565
FOLLY_GNU_DISABLE_WARNING("-Wuninitialized")
6666
FOLLY_GCC_DISABLE_WARNING("-Wmaybe-uninitialized")
67-
return x;
67+
return Endian::little<T>(x);
6868
FOLLY_POP_WARNING
6969
}
7070
};
@@ -76,18 +76,18 @@ struct BitsTraits<
7676
typename std::enable_if<(std::is_integral<T>::value)>::type> {
7777
using UnderlyingType = T;
7878
static T FOLLY_DISABLE_ADDRESS_SANITIZER load(const UnalignedNoASan<T>& x) {
79-
return x.value;
79+
return Endian::little<T>(x.value);
8080
}
8181
static void FOLLY_DISABLE_ADDRESS_SANITIZER
8282
store(UnalignedNoASan<T>& x, T v) {
83-
x.value = v;
83+
x.value = Endian::little<T>(v);
8484
}
8585
static T FOLLY_DISABLE_ADDRESS_SANITIZER
8686
loadRMW(const UnalignedNoASan<T>& x) {
8787
FOLLY_PUSH_WARNING
8888
FOLLY_GNU_DISABLE_WARNING("-Wuninitialized")
8989
FOLLY_GCC_DISABLE_WARNING("-Wmaybe-uninitialized")
90-
return x.value;
90+
return Endian::little<T>(x.value);
9191
FOLLY_POP_WARNING
9292
}
9393
};
@@ -98,13 +98,13 @@ struct BitsTraits<
9898
T,
9999
typename std::enable_if<(std::is_integral<T>::value)>::type> {
100100
using UnderlyingType = T;
101-
static T load(const T& x) { return x; }
102-
static void store(T& x, T v) { x = v; }
101+
static T load(const T& x) { return Endian::little<T>(x); }
102+
static void store(T& x, T v) { x = Endian::little<T>(v); }
103103
static T loadRMW(const T& x) {
104104
FOLLY_PUSH_WARNING
105105
FOLLY_GNU_DISABLE_WARNING("-Wuninitialized")
106106
FOLLY_GCC_DISABLE_WARNING("-Wmaybe-uninitialized")
107-
return x;
107+
return Endian::little<T>(x);
108108
FOLLY_POP_WARNING
109109
}
110110
};

0 commit comments

Comments
 (0)