Skip to content

Commit c89b8f5

Browse files
committed
fix: quick remedy for invalid memory access in obuf_create
Tarantool's struct obuf contains one more field in debug builds which is utilized for additional allocation checks. Before this commit we didn't mirror it in our rust definition which sometimes resulted in crashes. This is not a proper fix to the underlying problem but it's certainly better than leaving it as is.
1 parent e8b43dc commit c89b8f5

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

tarantool/src/ffi/sql.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use libc::{iovec, size_t};
44
use std::cmp;
55
use std::io::Read;
6+
use std::mem::MaybeUninit;
67
use std::os::raw::{c_char, c_int, c_void};
78

89
pub const IPROTO_DATA: u8 = 0x30;
@@ -72,18 +73,11 @@ impl ObufWrapper {
7273
let inner_buf = unsafe {
7374
let slab_c = cord_slab_cache();
7475

75-
let mut buf = Obuf {
76-
_slab_cache: std::mem::zeroed(),
77-
pos: 0,
78-
n_iov: 0,
79-
used: 0,
80-
start_capacity: start_capacity as size_t,
81-
capacity: std::mem::zeroed(),
82-
iov: std::mem::zeroed(),
83-
};
84-
obuf_create(&mut buf as *mut Obuf, slab_c, 1024);
85-
buf
76+
let mut buf = MaybeUninit::<Obuf>::zeroed();
77+
obuf_create(buf.as_mut_ptr(), slab_c, start_capacity);
78+
buf.assume_init()
8679
};
80+
8781
Self {
8882
inner: inner_buf,
8983
read_pos: 0,
@@ -135,6 +129,7 @@ impl Read for ObufWrapper {
135129
}
136130
}
137131

132+
// TODO: ASan-enabled build has a different layout (obuf_asan.h).
138133
#[repr(C)]
139134
pub(crate) struct Obuf {
140135
_slab_cache: *const c_void,
@@ -144,6 +139,11 @@ pub(crate) struct Obuf {
144139
pub start_capacity: size_t,
145140
pub capacity: [size_t; 32],
146141
pub iov: [iovec; 32],
142+
// This flag is only present in debug builds (!NDEBUG),
143+
// but it's easier to just add it unconditionally to
144+
// prevent illegal memory access in obuf_create.
145+
// TODO: prevent this class of errors using a better solution.
146+
pub reserved: bool,
147147
}
148148

149149
impl Drop for Obuf {

0 commit comments

Comments
 (0)