Bug 1466475 - Make mozilla::Span produce aligned bogus pointers per new Rust rules. r?froydnj
MozReview-Commit-ID: JFVSRu53Geh
--- a/mfbt/Span.h
+++ b/mfbt/Span.h
@@ -834,19 +834,20 @@ private:
template<class ExtentType>
class storage_type : public ExtentType
{
public:
template<class OtherExtentType>
constexpr storage_type(pointer elements,
OtherExtentType ext)
: ExtentType(ext)
- // Replace nullptr with 0x1 for Rust slice compatibility. See
+ // Replace nullptr with aligned bogus pointer for Rust slice
+ // compatibility. See
// https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html
- , data_(elements ? elements : reinterpret_cast<pointer>(0x1))
+ , data_(elements ? elements : reinterpret_cast<pointer>(alignof(element_type)))
{
const size_t extentSize = ExtentType::size();
MOZ_RELEASE_ASSERT(
(!elements && extentSize == 0) ||
(elements && extentSize != mozilla::MaxValue<size_t>::value));
}
constexpr pointer data() const { return data_; }
--- a/mfbt/tests/gtest/TestSpan.cpp
+++ b/mfbt/tests/gtest/TestSpan.cpp
@@ -58,32 +58,32 @@ static_assert(!IsConvertible<const nsTAr
static_assert(IsConvertible<nsTArray<const int>, Span<const int>>::value,
"nsTArray should convert into const");
static_assert(!IsConvertible<nsTArray<const int>, Span<int>>::value,
"nsTArray should not drop const in conversion");
/**
* Rust slice-compatible nullptr replacement value.
*/
-#define SLICE_CONST_INT_PTR reinterpret_cast<const int*>(0x1)
+#define SLICE_CONST_INT_PTR reinterpret_cast<const int*>(alignof(const int))
/**
* Rust slice-compatible nullptr replacement value.
*/
-#define SLICE_INT_PTR reinterpret_cast<int*>(0x1)
+#define SLICE_INT_PTR reinterpret_cast<int*>(alignof(int))
/**
* Rust slice-compatible nullptr replacement value.
*/
-#define SLICE_CONST_INT_PTR_PTR reinterpret_cast<const int**>(0x1)
+#define SLICE_CONST_INT_PTR_PTR reinterpret_cast<const int**>(alignof(const int*))
/**
* Rust slice-compatible nullptr replacement value.
*/
-#define SLICE_INT_PTR_PTR reinterpret_cast<int**>(0x1)
+#define SLICE_INT_PTR_PTR reinterpret_cast<int**>(alignof(int *))
namespace {
struct BaseClass
{
};
struct DerivedClass : BaseClass
{
};
@@ -1992,17 +1992,17 @@ SPAN_TEST(as_bytes)
{
Span<int> s;
auto bs = AsBytes(s);
ASSERT_EQ(bs.Length(), s.Length());
ASSERT_EQ(bs.Length(), 0U);
ASSERT_EQ(bs.size_bytes(), 0U);
ASSERT_EQ(static_cast<const void*>(bs.data()),
static_cast<const void*>(s.data()));
- ASSERT_EQ(bs.data(), reinterpret_cast<const uint8_t*>(0x1));
+ ASSERT_EQ(bs.data(), reinterpret_cast<const uint8_t*>(SLICE_INT_PTR));
}
{
Span<int> s = a;
auto bs = AsBytes(s);
ASSERT_EQ(static_cast<const void*>(bs.data()),
static_cast<const void*>(s.data()));
ASSERT_EQ(bs.Length(), s.LengthBytes());
@@ -2026,17 +2026,17 @@ SPAN_TEST(as_writable_bytes)
{
Span<int> s;
auto bs = AsWritableBytes(s);
ASSERT_EQ(bs.Length(), s.Length());
ASSERT_EQ(bs.Length(), 0U);
ASSERT_EQ(bs.size_bytes(), 0U);
ASSERT_EQ(static_cast<void*>(bs.data()), static_cast<void*>(s.data()));
- ASSERT_EQ(bs.data(), reinterpret_cast<uint8_t*>(0x1));
+ ASSERT_EQ(bs.data(), reinterpret_cast<uint8_t*>(SLICE_INT_PTR));
}
{
Span<int> s = a;
auto bs = AsWritableBytes(s);
ASSERT_EQ(static_cast<void*>(bs.data()), static_cast<void*>(s.data()));
ASSERT_EQ(bs.Length(), s.LengthBytes());
}