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

yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 5d624dfe6c [bugfix]fix segmentation fault at unalign address cast to 
int128 (#10094)
5d624dfe6c is described below

commit 5d624dfe6cbc4f851eec5d7d2544a83e851e9149
Author: Pxl <[email protected]>
AuthorDate: Tue Jun 14 15:32:58 2022 +0800

    [bugfix]fix segmentation fault at unalign address cast to int128 (#10094)
---
 be/src/olap/types.h              | 39 ++++++++++++++++++++++++---------------
 be/src/util/types.h              | 13 +++++++++----
 be/test/olap/row_cursor_test.cpp |  7 +++----
 3 files changed, 36 insertions(+), 23 deletions(-)

diff --git a/be/src/olap/types.h b/be/src/olap/types.h
index 666a5895b7..915239b0ee 100644
--- a/be/src/olap/types.h
+++ b/be/src/olap/types.h
@@ -537,18 +537,27 @@ template <FieldType field_type>
 struct BaseFieldtypeTraits : public CppTypeTraits<field_type> {
     using CppType = typename CppTypeTraits<field_type>::CppType;
 
+    static inline CppType get_cpp_type_value(const void* address) {
+        if constexpr (field_type == OLAP_FIELD_TYPE_LARGEINT) {
+            return get_int128_from_unalign(address);
+        }
+        return *reinterpret_cast<const CppType*>(address);
+    }
+
+    static inline void set_cpp_type_value(void* address, const CppType& value) 
{
+        memcpy(address, &value, sizeof(CppType));
+    }
+
     static inline bool equal(const void* left, const void* right) {
-        CppType l_value = *reinterpret_cast<const CppType*>(left);
-        CppType r_value = *reinterpret_cast<const CppType*>(right);
-        return l_value == r_value;
+        return get_cpp_type_value(left) == get_cpp_type_value(right);
     }
 
     static inline int cmp(const void* left, const void* right) {
-        CppType left_int = *reinterpret_cast<const CppType*>(left);
-        CppType right_int = *reinterpret_cast<const CppType*>(right);
-        if (left_int < right_int) {
+        CppType left_value = get_cpp_type_value(left);
+        CppType right_value = get_cpp_type_value(right);
+        if (left_value < right_value) {
             return -1;
-        } else if (left_int > right_int) {
+        } else if (left_value > right_value) {
             return 1;
         } else {
             return 0;
@@ -556,19 +565,19 @@ struct BaseFieldtypeTraits : public 
CppTypeTraits<field_type> {
     }
 
     static inline void shallow_copy(void* dest, const void* src) {
-        *reinterpret_cast<CppType*>(dest) = *reinterpret_cast<const 
CppType*>(src);
+        memcpy(dest, src, sizeof(CppType));
     }
 
     static inline void deep_copy(void* dest, const void* src, MemPool* 
mem_pool) {
-        *reinterpret_cast<CppType*>(dest) = *reinterpret_cast<const 
CppType*>(src);
+        memcpy(dest, src, sizeof(CppType));
     }
 
     static inline void copy_object(void* dest, const void* src, MemPool* 
mem_pool) {
-        *reinterpret_cast<CppType*>(dest) = *reinterpret_cast<const 
CppType*>(src);
+        memcpy(dest, src, sizeof(CppType));
     }
 
     static inline void direct_copy(void* dest, const void* src) {
-        *reinterpret_cast<CppType*>(dest) = *reinterpret_cast<const 
CppType*>(src);
+        memcpy(dest, src, sizeof(CppType));
     }
 
     static inline void direct_copy_may_cut(void* dest, const void* src) { 
direct_copy(dest, src); }
@@ -579,11 +588,11 @@ struct BaseFieldtypeTraits : public 
CppTypeTraits<field_type> {
     }
 
     static inline void set_to_max(void* buf) {
-        *reinterpret_cast<CppType*>(buf) = std::numeric_limits<CppType>::max();
+        set_cpp_type_value(buf, std::numeric_limits<CppType>::max());
     }
 
     static inline void set_to_min(void* buf) {
-        *reinterpret_cast<CppType*>(buf) = std::numeric_limits<CppType>::min();
+        set_cpp_type_value(buf, std::numeric_limits<CppType>::min());
     }
 
     static inline uint32_t hash_code(const void* data, uint32_t seed) {
@@ -591,7 +600,7 @@ struct BaseFieldtypeTraits : public 
CppTypeTraits<field_type> {
     }
 
     static std::string to_string(const void* src) {
-        return std::to_string(*reinterpret_cast<const CppType*>(src));
+        return std::to_string(get_cpp_type_value(src));
     }
 
     static Status from_string(void* buf, const std::string& scan_key) {
@@ -599,7 +608,7 @@ struct BaseFieldtypeTraits : public 
CppTypeTraits<field_type> {
         if (scan_key.length() > 0) {
             value = static_cast<CppType>(strtol(scan_key.c_str(), nullptr, 
10));
         }
-        *reinterpret_cast<CppType*>(buf) = value;
+        set_cpp_type_value(buf, value);
         return Status::OK();
     }
 };
diff --git a/be/src/util/types.h b/be/src/util/types.h
index 7d45141bad..b622b91aad 100644
--- a/be/src/util/types.h
+++ b/be/src/util/types.h
@@ -17,6 +17,8 @@
 
 #pragma once
 
+#include "olap/olap_common.h"
+
 namespace doris {
 
 // Because __int128 in memory is not aligned, but GCC7 will generate SSE 
instruction
@@ -32,13 +34,16 @@ struct PackedInt128 {
         value = value_;
         return *this;
     }
-    PackedInt128& operator=(const PackedInt128& rhs) {
-        value = rhs.value;
-        return *this;
-    }
+    PackedInt128& operator=(const PackedInt128& rhs) = default;
 #pragma GCC diagnostic pop
 
     __int128 value;
 } __attribute__((packed));
 
+// unalign address directly casted to int128 will core dump
+inline int128_t get_int128_from_unalign(const void* address) {
+    int128_t value = 0;
+    memcpy(&value, address, sizeof(int128_t));
+    return value;
+}
 } // namespace doris
diff --git a/be/test/olap/row_cursor_test.cpp b/be/test/olap/row_cursor_test.cpp
index 817fa9ade3..cdabe13387 100644
--- a/be/test/olap/row_cursor_test.cpp
+++ b/be/test/olap/row_cursor_test.cpp
@@ -26,6 +26,7 @@
 #include "runtime/mem_pool.h"
 #include "runtime/mem_tracker.h"
 #include "util/logging.h"
+#include "util/types.h"
 
 namespace doris {
 
@@ -498,8 +499,7 @@ TEST_F(TestRowCursor, AggregateWithoutNull) {
 
     agg_update_row(&row, right, nullptr);
 
-    int128_t agg_value = 0;
-    memcpy(&agg_value, row.cell_ptr(2), 16);
+    int128_t agg_value = get_int128_from_unalign(row.cell_ptr(2));
     EXPECT_TRUE(agg_value == ((int128_t)(1) << 101));
 
     double agg_double = *reinterpret_cast<double*>(row.cell_ptr(3));
@@ -559,8 +559,7 @@ TEST_F(TestRowCursor, AggregateWithNull) {
 
     agg_update_row(&row, right, nullptr);
 
-    int128_t agg_value = 0;
-    memcpy(&agg_value, row.cell_ptr(2), 16);
+    int128_t agg_value = get_int128_from_unalign(row.cell_ptr(2));
     EXPECT_TRUE(agg_value == ((int128_t)(1) << 101));
 
     bool is_null_double = left.is_null(3);


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

Reply via email to