Skip to content

C0019 unbounded-read

Rule: A read to the end of a stream is bounded with take().

Code C0019 (stable, never reused)
Id unbounded-read
Category code
Source spirit_level
Fix by hand

Level per tier

Tier Level Reason
small off
small-workspace off
medium off
big warn input from outside sets the memory a read to the end takes; candidates only, a place to look
ai warn input from outside sets the memory a read to the end takes; candidates only, a place to look
embedded off
ui off
egui off
units-strict off
units-paranoid off

Parameters

Name Type Default Meaning
methods list of strings ["read_to_end", "read_to_string"] methods that read until the end
bounds list of strings ["take"] methods in the receiver chain that bound the read

Why

read_to_end and read_to_string read until the other side stops. On a socket, a request body or a file from outside, the other side decides how much memory the process takes. Every input that grows from outside needs a limit.

The lint reports each call whose receiver chain has no take(...). It cannot tell a socket from a file the program wrote itself, so each finding is a candidate: a place to look at, never a failed run.

stream.read_to_end(&mut buf)?;                       // reported
stream.take(MAX_FRAME_BYTES).read_to_end(&mut buf)?; // passes

How to fix

Bound the read with take(LIMIT) and refuse input that reached the limit. Where the source is trusted (the program's own file), waive the line with that reason.

Configure

[lints.unbounded-read]
level = "deny"
methods = ["read_to_end", "read_to_string"]
bounds = ["take"]
reason = "why this project wants it at this level"

Allow one finding in the code with // spirit-level: allow(unbounded-read) <reason> (waivers); the code works there too, as in [lints.C0019].