yiguolei commented on code in PR #52676:
URL: https://github.com/apache/doris/pull/52676#discussion_r2180130229


##########
be/src/vec/data_types/serde/data_type_ipv6_serde.cpp:
##########
@@ -206,20 +206,43 @@ Status DataTypeIPv6SerDe::write_column_to_orc(const 
std::string& timezone, const
     const auto& col_data = assert_cast<const ColumnIPv6&>(column).get_data();
     auto* cur_batch = assert_cast<orc::StringVectorBatch*>(orc_col_batch);
 
-    INIT_MEMORY_FOR_ORC_WRITER()
-
+    // First pass: calculate total memory needed and collect serialized values
+    std::vector<std::string> serialized_values;
+    std::vector<size_t> valid_row_indices;
+    size_t total_size = 0;
     for (size_t row_id = start; row_id < end; row_id++) {
         if (cur_batch->notNull[row_id] == 1) {
-            std::string ipv6_str = IPv6Value::to_string(col_data[row_id]);
-            size_t len = ipv6_str.size();
-
-            REALLOC_MEMORY_FOR_ORC_WRITER()
-
-            strcpy(const_cast<char*>(bufferRef.data) + offset, 
ipv6_str.c_str());
-            cur_batch->data[row_id] = const_cast<char*>(bufferRef.data) + 
offset;
-            cur_batch->length[row_id] = len;
-            offset += len;
+            auto serialized_value = IPv6Value::to_string(col_data[row_id]);
+            size_t len = serialized_value.length();
+            total_size += len;
+            serialized_values.push_back(serialized_value);
+            valid_row_indices.push_back(row_id);
+        }
+    }
+    // Allocate continues memory based on calculated size
+    char* ptr = (char*)malloc(total_size);
+    if (!ptr) {
+        return Status::InternalError(
+                "malloc memory error when write variant column data to orc 
file.");
+    }
+    StringRef bufferRef;
+    bufferRef.data = ptr;
+    bufferRef.size = total_size;
+    buffer_list.emplace_back(bufferRef);
+    // Second pass: copy data to allocated memory
+    size_t offset = 0;
+    for (size_t i = 0; i < serialized_values.size(); i++) {
+        const auto& serialized_value = serialized_values[i];
+        size_t row_id = valid_row_indices[i];
+        size_t len = serialized_value.length();
+        if (offset + len > total_size) {
+            return Status::InternalError(
+                    "Buffer overflow when writing variant column data to ORC 
file.");

Review Comment:
   输出错误消息的时候,得把 offset,len,total size 都输出出来,这样万一有一些内存写脏的错误,我们能及时发现。
   其他的地方也一样



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to