Skip to content

Commit 7235aa1

Browse files
committed
Fix SmoLStrBuilder pushing null bytes on heap spill
1 parent f680abc commit 7235aa1

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.3.2 - 2024-10-23
4+
5+
- Fix `SmolStrBuilder::push` incorrectly padding null bytes when spilling onto the heap on a
6+
multibyte character push
7+
38
## 0.3.1 - 2024-09-04
49

510
- Fix `SmolStrBuilder` leaking implementation details

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ impl SmolStrBuilder {
763763
let mut heap = String::with_capacity(new_len);
764764
// copy existing inline bytes over to the heap
765765
// SAFETY: inline data is guaranteed to be valid utf8 for `old_len` bytes
766-
unsafe { heap.as_mut_vec().extend_from_slice(buf) };
766+
unsafe { heap.as_mut_vec().extend_from_slice(&buf[..*len]) };
767767
heap.push(c);
768768
self.0 = SmolStrBuilderRepr::Heap(heap);
769769
}

tests/test.rs

+9
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ fn test_to_smolstr() {
255255
assert_eq!(a, smol_str::format_smolstr!("{}", a));
256256
}
257257
}
258+
258259
#[test]
259260
fn test_builder_push_str() {
260261
//empty
@@ -290,6 +291,14 @@ fn test_builder_push_str() {
290291
let s = builder.finish();
291292
assert!(s.is_heap_allocated());
292293
assert_eq!("a".repeat(46), s);
294+
295+
// heap push on multibyte char
296+
let mut builder = SmolStrBuilder::new();
297+
builder.push_str("ohnonononononononono!");
298+
builder.push('🤯');
299+
let s = builder.finish();
300+
assert!(s.is_heap_allocated());
301+
assert_eq!("ohnonononononononono!🤯", s);
293302
}
294303

295304
#[test]

0 commit comments

Comments
 (0)