Skip to content

Commit a1c03f3

Browse files
committed
Account for upper bound of format pieces length
1 parent 295e573 commit a1c03f3

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

library/core/src/fmt/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,9 @@ impl<'a> Arguments<'a> {
636636
/// when using `format!`. Note: this is neither the lower nor upper bound.
637637
#[inline]
638638
pub fn estimated_capacity(&self) -> usize {
639+
// Since the format args are constructed from a single string literal in
640+
// `format_args!`, the total length of the pieces is under `isize::MAX`
641+
// and this sum cannot overflow.
639642
let pieces_length: usize = self.pieces.iter().map(|x| x.len()).sum();
640643

641644
if self.args.is_empty() {
@@ -649,7 +652,8 @@ impl<'a> Arguments<'a> {
649652
// There are some arguments, so any additional push
650653
// will reallocate the string. To avoid that,
651654
// we're "pre-doubling" the capacity here.
652-
pieces_length.checked_mul(2).unwrap_or(0)
655+
// It cannot overflow, because the maximum length is `isize::MAX`.
656+
pieces_length * 2
653657
}
654658
}
655659
}

0 commit comments

Comments
 (0)