Skip to content

Commit 85ac1aa

Browse files
committed
fix: rust bitstream segfault
1 parent 39e051b commit 85ac1aa

File tree

1 file changed

+8
-24
lines changed

1 file changed

+8
-24
lines changed

src/rust/src/libccxr_exports/bitstream.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@ unsafe fn copy_bitstream_from_rust_to_c(
1313
bitstream_ptr: *mut bitstream,
1414
rust_bitstream: &BitStreamRust,
1515
) {
16-
// Handle empty slice case
17-
if rust_bitstream.data.is_empty() {
18-
(*bitstream_ptr).pos = std::ptr::null_mut();
19-
(*bitstream_ptr).bpos = 8;
20-
(*bitstream_ptr).end = std::ptr::null_mut();
21-
(*bitstream_ptr).bitsleft = 0;
22-
(*bitstream_ptr).error = c_int::from(rust_bitstream.error);
23-
(*bitstream_ptr)._i_pos = std::ptr::null_mut();
24-
(*bitstream_ptr)._i_bpos = 0;
25-
return;
26-
}
27-
2816
// Get the original pos (which is the base of our slice)
2917
let base_ptr = rust_bitstream.data.as_ptr() as *mut c_uchar;
3018
let end_ptr = base_ptr.add(rust_bitstream.data.len());
@@ -63,19 +51,15 @@ unsafe fn copy_bitstream_c_to_rust(value: *mut bitstream) -> BitStreamRust<'stat
6351
// Calculate total buffer length from pos to end
6452
let total_len = (*value).end.offset_from((*value).pos) as usize;
6553

66-
if total_len > 0 {
67-
// Create slice from current position to end
68-
slice = std::slice::from_raw_parts((*value).pos, total_len);
69-
// Current position in this slice is 0 (since slice starts from current pos)
70-
current_pos_in_slice = 0;
54+
// Create slice from current position to end
55+
slice = std::slice::from_raw_parts((*value).pos, total_len);
56+
// Current position in this slice is 0 (since slice starts from current pos)
57+
current_pos_in_slice = 0;
7158

72-
// Calculate _i_pos relative to the slice start (which is current pos)
73-
if !(*value)._i_pos.is_null() {
74-
let i_offset = (*value)._i_pos.offset_from((*value).pos);
75-
i_pos_in_slice = i_offset.max(0) as usize;
76-
}
77-
} else if (*value).pos == (*value).end {
78-
slice = std::slice::from_raw_parts((*value).pos, 1);
59+
// Calculate _i_pos relative to the slice start (which is current pos)
60+
if !(*value)._i_pos.is_null() {
61+
let i_offset = (*value)._i_pos.offset_from((*value).pos);
62+
i_pos_in_slice = i_offset.max(0) as usize;
7963
}
8064
}
8165

0 commit comments

Comments
 (0)