Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit 165c806

Browse files
authored
Merge pull request #732 from sdroege/empty-slices
Handle empty slices correctly
2 parents 87e012d + 0cc8cdf commit 165c806

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

gdk/src/visual.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ impl Visual {
1313

1414
unsafe {
1515
ffi::gdk_query_depths(&mut ptr, &mut count);
16-
Vec::from(slice::from_raw_parts(ptr as *const i32, count as usize))
16+
if ptr.is_null() || count == 0 {
17+
vec![]
18+
} else {
19+
Vec::from(slice::from_raw_parts(ptr as *const i32, count as usize))
20+
}
1721
}
1822
}
1923
}

gtk/src/signal.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ mod editable {
115115
) where
116116
T: IsA<Editable>,
117117
{
118-
let buf = if new_text_length != -1 {
118+
let buf = if new_text_length == 0 {
119+
&[]
120+
} else if new_text_length != -1 {
119121
slice::from_raw_parts(new_text as *mut c_uchar, new_text_length as usize)
120122
} else {
121123
CStr::from_ptr(new_text).to_bytes()

gtk/src/text_buffer.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,16 @@ impl<O: IsA<TextBuffer>> TextBufferExtManual for O {
223223
let f: &F = &*(f as *const F);
224224
let mut location_copy = from_glib_none(location);
225225

226+
let text = if len <= 0 {
227+
&[]
228+
} else {
229+
slice::from_raw_parts(text as *const u8, len as usize)
230+
};
231+
226232
f(
227233
TextBuffer::from_glib_borrow(this).unsafe_cast_ref(),
228234
&mut location_copy,
229-
str::from_utf8(slice::from_raw_parts(text as *const u8, len as usize)).unwrap(),
235+
str::from_utf8(text).unwrap(),
230236
);
231237

232238
*location = *location_copy.to_glib_none().0;

gtk/src/tree_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl TreePath {
1414
mut_override(self.to_glib_none().0),
1515
&mut count,
1616
);
17-
if ptr.is_null() {
17+
if ptr.is_null() || count == 0 {
1818
vec![]
1919
} else {
2020
slice::from_raw_parts(ptr, count as usize).to_owned()

0 commit comments

Comments
 (0)