prql icon indicating copy to clipboard operation
prql copied to clipboard

Memory safety bug in prqlc-c: Vec::from_raw_parts with wrong element type

Open prql-bot opened this issue 8 months ago • 0 comments

In prqlc/bindings/prqlc-c/src/lib.rs (lines 238-242), the prqlc_destroy_compile_result function reconstructs a Vec<i8> from a pointer that originally pointed to Vec<Message>:

drop(Vec::from_raw_parts(
    res.messages as *mut i8,
    res.messages_len,
    res.messages_len,
));

This has two issues:

  1. Wrong element type: The pointer is cast to *mut i8 but the original allocation was for Message elements. When the Vec is dropped, dealloc is called with messages_len * size_of::<i8>() bytes, but the actual allocation size was capacity * size_of::<Message>() bytes. This is undefined behavior per the Vec::from_raw_parts safety contract.

  2. Capacity assumption: The capacity is set to messages_len, but the original Vec was created with Vec::with_capacity(err.inner.len()). While these should be equal in practice (the Vec is extended with exactly err.inner.len() items), the safety contract requires the exact capacity from the original allocation.

Suggested fix:

drop(Vec::from_raw_parts(
    res.messages as *mut Message,
    res.messages_len,
    res.messages_len,
));

The individual Message fields are already cleaned up in the loop above (lines 218-236), so the Vec destructor just needs to free the allocation with the correct layout.

prql-bot avatar Mar 25 '26 06:03 prql-bot