🐛 Fix: handle empty result values in mq_eval function#1348
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adjusts the mq-ffi C API so that mq_eval() returns a null values pointer when the evaluation produces an empty result set, aligning the pointer value with values_len == 0 and updating the corresponding test.
Changes:
- Return
values = NULLfrommq_eval()when there are no result values. - Update
test_empty_inputto expectvalues == NULLwithvalues_len == 0.
| let ptr = if c_values.is_empty() { | ||
| ptr::null_mut() | ||
| } else { | ||
| let p = c_values.as_mut_ptr(); | ||
| std::mem::forget(c_values); // Prevent Rust from freeing the Vec's memory | ||
| p | ||
| }; |
There was a problem hiding this comment.
mq_eval leaks c_values by returning as_mut_ptr() + mem::forget, but mq_free_result later reconstructs a Vec with Vec::from_raw_parts(result.values, result.values_len, result.values_len). This is UB whenever the original Vec capacity != values_len (which is common due to growth strategy), and can lead to invalid deallocation. Consider switching to a boxed slice (into_boxed_slice) and freeing with Box::from_raw(slice_from_raw_parts_mut(ptr, len)), or alternatively include the capacity in MqResult and pass it through so from_raw_parts uses the correct capacity.
No description provided.