Skip to content

Commit 4d43eba

Browse files
committed
glib: Don't include the NULL terminator in the IntoStrV slice
1 parent 1a67ae4 commit 4d43eba

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

glib/src/collections/strv.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ impl IntoStrV for StrV {
10671067
impl<'a> IntoStrV for &'a StrV {
10681068
#[inline]
10691069
fn run_with_strv<R, F: FnOnce(&[*mut c_char]) -> R>(self, f: F) -> R {
1070-
f(unsafe { std::slice::from_raw_parts(self.as_ptr(), self.len() + 1) })
1070+
f(unsafe { std::slice::from_raw_parts(self.as_ptr(), self.len()) })
10711071
}
10721072
}
10731073

@@ -1134,7 +1134,7 @@ impl IntoStrV for &[GString] {
11341134
}
11351135
*ptrs.add(self.len()) = ptr::null_mut();
11361136

1137-
f(std::slice::from_raw_parts(ptrs, self.len() + 1))
1137+
f(std::slice::from_raw_parts(ptrs, self.len()))
11381138
}
11391139
} else {
11401140
let mut s = StrV::with_capacity(self.len());
@@ -1159,7 +1159,7 @@ impl<'a> IntoStrV for &[&'a GString] {
11591159
}
11601160
*ptrs.add(self.len()) = ptr::null_mut();
11611161

1162-
f(std::slice::from_raw_parts(ptrs, self.len() + 1))
1162+
f(std::slice::from_raw_parts(ptrs, self.len()))
11631163
}
11641164
} else {
11651165
let mut s = StrV::with_capacity(self.len());
@@ -1184,7 +1184,7 @@ impl<'a> IntoStrV for &[&'a GStr] {
11841184
}
11851185
*ptrs.add(self.len()) = ptr::null_mut();
11861186

1187-
f(std::slice::from_raw_parts(ptrs, self.len() + 1))
1187+
f(std::slice::from_raw_parts(ptrs, self.len()))
11881188
}
11891189
} else {
11901190
let mut s = StrV::with_capacity(self.len());
@@ -1214,7 +1214,7 @@ impl<'a> IntoStrV for &[&'a str] {
12141214
}
12151215
*ptrs.add(self.len()) = ptr::null_mut();
12161216

1217-
f(std::slice::from_raw_parts(ptrs, self.len() + 1))
1217+
f(std::slice::from_raw_parts(ptrs, self.len()))
12181218
}
12191219
} else {
12201220
let mut s = StrV::with_capacity(self.len());
@@ -1244,7 +1244,7 @@ impl IntoStrV for &[String] {
12441244
}
12451245
*ptrs.add(self.len()) = ptr::null_mut();
12461246

1247-
f(std::slice::from_raw_parts(ptrs, self.len() + 1))
1247+
f(std::slice::from_raw_parts(ptrs, self.len()))
12481248
}
12491249
} else {
12501250
let mut s = StrV::with_capacity(self.len());
@@ -1274,7 +1274,7 @@ impl<'a> IntoStrV for &[&'a String] {
12741274
}
12751275
*ptrs.add(self.len()) = ptr::null_mut();
12761276

1277-
f(std::slice::from_raw_parts(ptrs, self.len() + 1))
1277+
f(std::slice::from_raw_parts(ptrs, self.len()))
12781278
}
12791279
} else {
12801280
let mut s = StrV::with_capacity(self.len());
@@ -1451,38 +1451,38 @@ mod test {
14511451
let items = ["str1", "str2", "str3", "str4"];
14521452

14531453
items[..].run_with_strv(|s| unsafe {
1454-
assert!(s[4].is_null());
1455-
assert_eq!(s.len(), items.len() + 1);
1454+
assert!(s.get_unchecked(4).is_null());
1455+
assert_eq!(s.len(), items.len());
14561456
let s = StrV::from_glib_borrow(s.as_ptr() as *const *const c_char);
14571457
assert_eq!(s, items);
14581458
});
14591459

14601460
Vec::from(&items[..]).run_with_strv(|s| unsafe {
1461-
assert!(s[4].is_null());
1462-
assert_eq!(s.len(), items.len() + 1);
1461+
assert!(s.get_unchecked(4).is_null());
1462+
assert_eq!(s.len(), items.len());
14631463
let s = StrV::from_glib_borrow(s.as_ptr() as *const *const c_char);
14641464
assert_eq!(s, items);
14651465
});
14661466

14671467
StrV::from(&items[..]).run_with_strv(|s| unsafe {
1468-
assert!(s[4].is_null());
1469-
assert_eq!(s.len(), items.len() + 1);
1468+
assert!(s.get_unchecked(4).is_null());
1469+
assert_eq!(s.len(), items.len());
14701470
let s = StrV::from_glib_borrow(s.as_ptr() as *const *const c_char);
14711471
assert_eq!(s, items);
14721472
});
14731473

14741474
let v = items.iter().copied().map(String::from).collect::<Vec<_>>();
14751475
items.run_with_strv(|s| unsafe {
1476-
assert!(s[4].is_null());
1477-
assert_eq!(s.len(), v.len() + 1);
1476+
assert!(s.get_unchecked(4).is_null());
1477+
assert_eq!(s.len(), v.len());
14781478
let s = StrV::from_glib_borrow(s.as_ptr() as *const *const c_char);
14791479
assert_eq!(s, items);
14801480
});
14811481

14821482
let v = items.iter().copied().map(GString::from).collect::<Vec<_>>();
14831483
items.run_with_strv(|s| unsafe {
1484-
assert!(s[4].is_null());
1485-
assert_eq!(s.len(), v.len() + 1);
1484+
assert!(s.get_unchecked(4).is_null());
1485+
assert_eq!(s.len(), v.len());
14861486
let s = StrV::from_glib_borrow(s.as_ptr() as *const *const c_char);
14871487
assert_eq!(s, items);
14881488
});

0 commit comments

Comments
 (0)