This is an automated email from the ASF dual-hosted git repository.

serverglen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 2729272a fix flatmap element space should align with usertype (#2288)
2729272a is described below

commit 2729272ae5ee69e4c4142d14b7fafbf800944316
Author: Dongsheng He <[email protected]>
AuthorDate: Fri Jul 14 20:00:05 2023 +0800

    fix flatmap element space should align with usertype (#2288)
---
 src/butil/containers/flat_map.h | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/butil/containers/flat_map.h b/src/butil/containers/flat_map.h
index 9e6eec1f..9bfd8ec1 100644
--- a/src/butil/containers/flat_map.h
+++ b/src/butil/containers/flat_map.h
@@ -96,6 +96,7 @@
 #include <stdint.h>
 #include <functional>
 #include <iostream>                               // std::ostream
+#include <type_traits>                            // std::aligned_storage
 #include "butil/type_traits.h"
 #include "butil/logging.h"
 #include "butil/find_cstr.h"
@@ -251,22 +252,23 @@ public:
 
     struct Bucket {
         explicit Bucket(const _K& k) : next(NULL)
-        { new (element_spaces) Element(k); }
+        { new (&element_spaces) Element(k); }
         Bucket(const Bucket& other) : next(NULL)
-        { new (element_spaces) Element(other.element()); }
+        { new (&element_spaces) Element(other.element()); }
         bool is_valid() const { return next != (const Bucket*)-1UL; }
         void set_invalid() { next = (Bucket*)-1UL; }
         // NOTE: Only be called when is_valid() is true.
         Element& element() {
-            void* spaces = element_spaces; // Suppress strict-aliasing
+            void* spaces = &element_spaces; // Suppress strict-aliasing
             return *reinterpret_cast<Element*>(spaces);
         }
         const Element& element() const {
-            const void* spaces = element_spaces;
+            const void* spaces = &element_spaces;
             return *reinterpret_cast<const Element*>(spaces);
         }
-        Bucket* next;
-        char element_spaces[sizeof(Element)];
+        Bucket *next;
+        typename std::aligned_storage<sizeof(Element), alignof(Element)>::type
+            element_spaces;
     };
 
     allocator_type& get_allocator() { return _pool.get_allocator(); }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to