@@ -150,8 +150,8 @@ std::vector<uint8_t> serialize_primitive_array(const sparrow::primitive_array<T>
150
150
151
151
// arrow_arr.buffers[0] is the validity bitmap
152
152
// arrow_arr.buffers[1] is the data buffer
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 ]);
153
+ const auto validity_bitmap = static_cast <const uint8_t *>(arrow_arr.buffers [0 ]);
154
+ const auto data_buffer = static_cast <const uint8_t *>(arrow_arr.buffers [1 ]);
155
155
156
156
// Calculate the size of the validity and data buffers
157
157
int64_t validity_size = (arrow_arr.length + arrow_alignment - 1 ) / arrow_alignment;
@@ -183,10 +183,10 @@ std::vector<uint8_t> serialize_primitive_array(const sparrow::primitive_array<T>
183
183
batch_builder.Finish (batch_message_offset);
184
184
185
185
// III - Append the RecordBatch message to the final buffer
186
- uint32_t batch_meta_len = batch_builder.GetSize (); // Get the size of the batch metadata
187
- int64_t aligned_batch_meta_len = align_to_8 (batch_meta_len); // Calculate the padded length
186
+ const uint32_t batch_meta_len = batch_builder.GetSize (); // Get the size of the batch metadata
187
+ const int64_t aligned_batch_meta_len = align_to_8 (batch_meta_len); // Calculate the padded length
188
188
189
- size_t current_size = final_buffer.size (); // Get the current size (which is the end of the Schema message)
189
+ const size_t current_size = final_buffer.size (); // Get the current size (which is the end of the Schema message)
190
190
// Resize the buffer to append the new message
191
191
final_buffer.resize (current_size + sizeof (uint32_t ) + aligned_batch_meta_len + body_len);
192
192
uint8_t * dst = final_buffer.data () + current_size; // Get a pointer to where the new message will start
@@ -197,7 +197,15 @@ std::vector<uint8_t> serialize_primitive_array(const sparrow::primitive_array<T>
197
197
// Copy the RecordBatch metadata into the buffer
198
198
memcpy (dst, batch_builder.GetBufferPointer (), batch_meta_len);
199
199
// Add padding to align the body to an 8-byte boundary
200
- memset (dst + batch_meta_len, 0 , aligned_batch_meta_len - batch_meta_len);
200
+ if (aligned_batch_meta_len >= batch_meta_len)
201
+ {
202
+ memset (dst + batch_meta_len, 0 , aligned_batch_meta_len - batch_meta_len);
203
+ }
204
+ else
205
+ {
206
+ throw std::runtime_error (" aligned_batch_meta_len should be greater than batch_meta_len" );
207
+ }
208
+
201
209
dst += aligned_batch_meta_len;
202
210
// Copy the actual data buffers (the message body) into the buffer
203
211
if (validity_bitmap)
@@ -207,7 +215,8 @@ std::vector<uint8_t> serialize_primitive_array(const sparrow::primitive_array<T>
207
215
else
208
216
{
209
217
// If validity_bitmap is null, it means there are no nulls
210
- memset (dst, 0xFF , validity_size);
218
+ constexpr uint8_t no_nulls_bitmap = 0xFF ;
219
+ memset (dst, no_nulls_bitmap, validity_size);
211
220
}
212
221
dst += validity_size;
213
222
if (data_buffer)
@@ -230,7 +239,7 @@ sparrow::primitive_array<T> deserialize_primitive_array(const std::vector<uint8_
230
239
size_t current_offset = 0 ;
231
240
232
241
// I - Deserialize the Schema message
233
- uint32_t schema_meta_len;
242
+ uint32_t schema_meta_len = 0 ;
234
243
memcpy (&schema_meta_len, buf_ptr + current_offset, sizeof (schema_meta_len));
235
244
current_offset += sizeof (uint32_t );
236
245
auto schema_message = org::apache::arrow::flatbuf::GetMessage (buf_ptr + current_offset);
@@ -248,7 +257,7 @@ sparrow::primitive_array<T> deserialize_primitive_array(const std::vector<uint8_
248
257
current_offset += schema_meta_len;
249
258
250
259
// II - Deserialize the RecordBatch message
251
- uint32_t batch_meta_len;
260
+ uint32_t batch_meta_len = 0 ;
252
261
memcpy (&batch_meta_len, buf_ptr + current_offset, sizeof (batch_meta_len));
253
262
current_offset += sizeof (uint32_t );
254
263
auto batch_message = org::apache::arrow::flatbuf::GetMessage (buf_ptr + current_offset);
@@ -270,10 +279,10 @@ sparrow::primitive_array<T> deserialize_primitive_array(const std::vector<uint8_
270
279
int64_t validity_len = buffers_meta->Get (0 )->length ();
271
280
int64_t data_len = buffers_meta->Get (1 )->length ();
272
281
273
- uint8_t * validity_buffer_copy = new uint8_t [validity_len];
282
+ auto validity_buffer_copy = new uint8_t [validity_len];
274
283
memcpy (validity_buffer_copy, body_ptr + buffers_meta->Get (0 )->offset (), validity_len);
275
284
276
- uint8_t * data_buffer_copy = new uint8_t [data_len];
285
+ auto data_buffer_copy = new uint8_t [data_len];
277
286
memcpy (data_buffer_copy, body_ptr + buffers_meta->Get (1 )->offset (), data_len);
278
287
279
288
// Get name
0 commit comments