Skip to content

Commit c7c9336

Browse files
Avoid zero-length write_str in fmt::write
1 parent 80ac15f commit c7c9336

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

library/core/src/fmt/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,9 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
11041104
None => {
11051105
// We can use default formatting parameters for all arguments.
11061106
for (arg, piece) in iter::zip(args.args, args.pieces) {
1107-
formatter.buf.write_str(*piece)?;
1107+
if !piece.is_empty() {
1108+
formatter.buf.write_str(*piece)?;
1109+
}
11081110
(arg.formatter)(arg.value, &mut formatter)?;
11091111
idx += 1;
11101112
}
@@ -1113,7 +1115,9 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
11131115
// Every spec has a corresponding argument that is preceded by
11141116
// a string piece.
11151117
for (arg, piece) in iter::zip(fmt, args.pieces) {
1116-
formatter.buf.write_str(*piece)?;
1118+
if !piece.is_empty() {
1119+
formatter.buf.write_str(*piece)?;
1120+
}
11171121
// SAFETY: arg and args.args come from the same Arguments,
11181122
// which guarantees the indexes are always within bounds.
11191123
unsafe { run(&mut formatter, arg, &args.args) }?;

0 commit comments

Comments
 (0)