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");
}

Remediation

Use write_all and read_exact, which loop until the whole buffer is transferred and report a short transfer as an error. When a partial transfer is genuinely acceptable, bind the count (let n = stream.read(&mut buf)?;) and use it to slice the buffer.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved