1212
1313namespace
1414{
15+ constexpr int64_t arrow_alignment = 8 ;
16+
1517 // Aligns a value to the next multiple of 8, as required by the Arrow IPC format for message bodies.
1618 int64_t align_to_8 (int64_t n)
1719 {
18- return (n + 7 ) & -8 ;
20+ return (n + arrow_alignment - 1 ) & -arrow_alignment ;
1921 }
2022
2123 // TODO Complete this with all possible formats?
@@ -137,7 +139,7 @@ std::vector<uint8_t> serialize_primitive_array(const sparrow::primitive_array<T>
137139 // Copy the metadata into the buffer, after the 4-byte length prefix
138140 memcpy (final_buffer.data () + sizeof (uint32_t ), schema_builder.GetBufferPointer (), schema_len);
139141 // Write the 4-byte metadata length at the beginning of the message
140- *( reinterpret_cast < uint32_t *>( final_buffer.data ())) = schema_len;
142+ memcpy ( final_buffer.data (), &schema_len, sizeof ( schema_len)) ;
141143 }
142144
143145 // II - Serialize the RecordBatch message
@@ -148,11 +150,11 @@ std::vector<uint8_t> serialize_primitive_array(const sparrow::primitive_array<T>
148150
149151 // arrow_arr.buffers[0] is the validity bitmap
150152 // arrow_arr.buffers[1] is the data buffer
151- const uint8_t * validity_bitmap = reinterpret_cast <const uint8_t *>(arrow_arr.buffers [0 ]);
152- const uint8_t * data_buffer = reinterpret_cast <const uint8_t *>(arrow_arr.buffers [1 ]);
153+ const uint8_t * validity_bitmap = static_cast <const uint8_t *>(arrow_arr.buffers [0 ]);
154+ const uint8_t * data_buffer = static_cast <const uint8_t *>(arrow_arr.buffers [1 ]);
153155
154156 // Calculate the size of the validity and data buffers
155- int64_t validity_size = (arrow_arr.length + 7 ) / 8 ;
157+ int64_t validity_size = (arrow_arr.length + arrow_alignment - 1 ) / arrow_alignment ;
156158 int64_t data_size = arrow_arr.length * sizeof (T);
157159 int64_t body_len = validity_size + data_size; // The total size of the message body
158160
@@ -190,7 +192,7 @@ std::vector<uint8_t> serialize_primitive_array(const sparrow::primitive_array<T>
190192 uint8_t * dst = final_buffer.data () + current_size; // Get a pointer to where the new message will start
191193
192194 // Write the 4-byte metadata length for the RecordBatch message
193- *( reinterpret_cast < uint32_t *>( dst)) = batch_meta_len;
195+ memcpy ( dst, &batch_meta_len, sizeof ( batch_meta_len)) ;
194196 dst += sizeof (uint32_t );
195197 // Copy the RecordBatch metadata into the buffer
196198 memcpy (dst, batch_builder.GetBufferPointer (), batch_meta_len);
@@ -228,7 +230,8 @@ sparrow::primitive_array<T> deserialize_primitive_array(const std::vector<uint8_
228230 size_t current_offset = 0 ;
229231
230232 // I - Deserialize the Schema message
231- uint32_t schema_meta_len = *(reinterpret_cast <const uint32_t *>(buf_ptr + current_offset));
233+ uint32_t schema_meta_len;
234+ memcpy (&schema_meta_len, buf_ptr + current_offset, sizeof (schema_meta_len));
232235 current_offset += sizeof (uint32_t );
233236 auto schema_message = org::apache::arrow::flatbuf::GetMessage (buf_ptr + current_offset);
234237 if (schema_message->header_type () != org::apache::arrow::flatbuf::MessageHeader::Schema)
@@ -245,7 +248,8 @@ sparrow::primitive_array<T> deserialize_primitive_array(const std::vector<uint8_
245248 current_offset += schema_meta_len;
246249
247250 // II - Deserialize the RecordBatch message
248- uint32_t batch_meta_len = *(reinterpret_cast <const uint32_t *>(buf_ptr + current_offset));
251+ uint32_t batch_meta_len;
252+ memcpy (&batch_meta_len, buf_ptr + current_offset, sizeof (batch_meta_len));
249253 current_offset += sizeof (uint32_t );
250254 auto batch_message = org::apache::arrow::flatbuf::GetMessage (buf_ptr + current_offset);
251255 if (batch_message->header_type () != org::apache::arrow::flatbuf::MessageHeader::RecordBatch)
0 commit comments