Skip to content

Commit 9c2a9ae

Browse files
committed
Reserve 8 bytes per format placeholder
1 parent b4b4b6d commit 9c2a9ae

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

library/core/src/fmt/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,9 @@ impl<'a> Arguments<'a> {
652652
// There are some arguments, so any additional push
653653
// will reallocate the string. To avoid that,
654654
// we're "pre-doubling" the capacity here.
655-
// It cannot overflow, because the maximum length is `isize::MAX`.
656-
pieces_length * 2
655+
pieces_length
656+
.checked_add(self.args.len().checked_mul(8).unwrap_or(0))
657+
.unwrap_or(pieces_length)
657658
}
658659
}
659660
}

library/coretests/tests/fmt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn test_estimated_capacity() {
2626
assert_eq!(format_args!("Hello").estimated_capacity(), 5);
2727
assert_eq!(format_args!("Hello, {}!", { "" }).estimated_capacity(), 16);
2828
assert_eq!(format_args!("{}, hello!", { "World" }).estimated_capacity(), 0);
29-
assert_eq!(format_args!("{}. 16-bytes piece", { "World" }).estimated_capacity(), 32);
29+
assert_eq!(format_args!("{}. 16-bytes piece", { "World" }).estimated_capacity(), 24);
3030
}
3131

3232
#[test]

0 commit comments

Comments
 (0)