byte count returned by read/write is discarded
ID |
rust.correctness.unused_io_amount |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Error Handling |
Language |
Rust |
Description
Reports a call to Read::read, Write::write or their vectored forms whose returned byte count is thrown away - as a bare statement, through let _ =, or by unwrapping only the Result while ignoring the usize inside it.
Rationale
Both calls are allowed to transfer fewer bytes than requested, and on sockets and pipes they routinely do. Discarding the count means the tail of a buffer is silently never written, or a buffer is treated as full when only its first bytes were filled and the rest is stale data. The resulting truncation shows up as a corrupt message far from this line, and it is load-dependent, so it survives testing and appears under production traffic.
The following code illustrates the pattern detected by this rule:
use std::io::{Read, Write};
use std::net::TcpStream;
fn send_greeting(stream: &mut TcpStream) {
// FLAGGED: byte count returned by read/write is discarded
stream.write(b"HELLO\r\n");
}