-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Warn about read into zero-length Vec
#8886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@rustbot claim |
Hi, now I am trying to cover as many cases as possible. // 1
let mut data = Vec::new();
f.read(&mut data)?;
// 2
let mut data = Vec::new();
let res = f.read(&mut data);
// 3
let mut data = Vec::new();
let usize = f.read(&mut data).unwrap();
// and many other cases... I am trying to catch all uses of such @rustbot label +E-help-wanted |
From a dataflow perspective, all of those have the same structure: there's a call to |
Hey @tamaroning, as mentioned by joshtriplett, these examples are almost the same from a dataflow perspective, the question is now how we can catch these. The simple approach would be to first find a vector initialization and then just check the next statement. Alternatively, you can take a look at a Possible interesting implementations:
Also, as a sidenote, I and most likely others rarely filter by the E-help-wanted label. If it seems like your help request was missed, you can also ping us or use another channel of communication. 🙃 |
@joshtriplett @xFrednet |
Uh oh!
There was an error while loading. Please reload this page.
What it does
This lint catches reads into a zero-length
Vec
. For instance, it should flag code like this:In general, it should flag any code that constructs a
Vec
(whether usingnew
orvec![]
or especiallywith_capacity
) and then reads into it (usingread
orread_exact
, whether sync or async versions) without changing the size of theVec
. The lint should warn that the number of bytes read depends on the size of the buffer read into, so this pattern will read 0 bytes. And in the specific case of a call towith_capacity
, the lint should warn that read gets the number of bytes from theVec
's length, not its capacity.In the
with_capacity
case, the lint should suggest adding something likedata.resize(len, 0);
; in other cases the lint can more generally suggest resizing before reading, or callingread_to_end
.Lint Name
read_zero_byte_vec
Category
correctness
Advantage
Correctness: reading zero bytes is almost certainly not the intended behavior of this code. This lint would catch a bug.
I've seen multiple instances of this bug, and I just wrote one myself.
Drawbacks
In theory, a very unusual read implementation could assign some semantic meaning to zero-byte reads. But it seems exceptionally unlikely that code intending to do a zero-byte read would allocate a
Vec
for it.Example
Could be written as:
Could be written as:
The text was updated successfully, but these errors were encountered: