Skip to content

Commit 147c4fd

Browse files
committed
Fixed str::raw::push_byte
It was previously pushing the byte on top of the string's null terminator. I added a test to make sure it doesn't break in the future.
1 parent 93432a2 commit 147c4fd

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/libstd/str.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ pub mod raw {
878878
let new_len = s.len() + 1;
879879
s.reserve_at_least(new_len);
880880
do s.as_mut_buf |buf, len| {
881-
*ptr::mut_offset(buf, len as int) = b;
881+
*ptr::mut_offset(buf, (len-1) as int) = b;
882882
}
883883
set_len(&mut *s, new_len);
884884
}
@@ -2801,6 +2801,13 @@ mod tests {
28012801
assert!(!" _ ".is_whitespace());
28022802
}
28032803

2804+
#[test]
2805+
fn test_push_byte() {
2806+
let mut s = ~"ABC";
2807+
unsafe{raw::push_byte(&mut s, 'D' as u8)};
2808+
assert_eq!(s, ~"ABCD");
2809+
}
2810+
28042811
#[test]
28052812
fn test_shift_byte() {
28062813
let mut s = ~"ABC";

0 commit comments

Comments
 (0)