Skip to content

Commit 2e5c75c

Browse files
committed
Avoid short writes in LineWriter
1 parent bd64bcb commit 2e5c75c

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

library/std/src/io/buffered/linewritershim.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,14 @@ impl<'a, W: ?Sized + Write> Write for LineWriterShim<'a, W> {
119119
// the buffer?
120120
// - If not, scan for the last newline that *does* fit in the buffer
121121
let tail = if flushed >= newline_idx {
122-
&buf[flushed..]
122+
let tail = &buf[flushed..];
123+
// Avoid unnecessary short writes by not splitting the remaining
124+
// bytes if they're larger than the buffer.
125+
// They can be written in full by the next call to write.
126+
if tail.len() > self.buffer.capacity() {
127+
return Ok(flushed);
128+
}
129+
tail
123130
} else if newline_idx - flushed <= self.buffer.capacity() {
124131
&buf[flushed..newline_idx]
125132
} else {

0 commit comments

Comments
 (0)