![github-actions[bot]](/assets/img/avatar_default.png)
* Create rule S7419 * Update RSPEC --------- Co-authored-by: yassin-kammoun-sonarsource <yassin-kammoun-sonarsource@users.noreply.github.com> Co-authored-by: yassin-kammoun-sonarsource <yassin.kammoun@sonarsource.com>
43 lines
1.3 KiB
Plaintext
43 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
``++io::Write::write(_vectored)++`` and ``++io::Read::read(_vectored)++`` do not guarantee processing the entire buffer, which might lead to only partial writes/reads. This can result in bugs if not handled appropriately. Instead, use ``++write_all++`` or ``++read_exact++`` methods that ensure the whole buffer is processed, providing more reliable code especially in asynchronous contexts.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,rust,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
use std::io;
|
|
fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
|
|
w.write(b"foo")?; // Noncompliant: This might not write the entire buffer.
|
|
Ok(())
|
|
}
|
|
|
|
fn bar<R: io::Read>(r: &mut R, buffer: &mut [u8]) -> io::Result<()> {
|
|
r.read(buffer)?; // Noncompliant: This might not read the entire buffer.
|
|
Ok(())
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,rust,diff-id=1,diff-type=compliant]
|
|
----
|
|
use std::io;
|
|
fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
|
|
w.write_all(b"foo")?; // Compliant: This writes the entire buffer.
|
|
Ok(())
|
|
}
|
|
|
|
fn bar<R: io::Read>(r: &mut R, buffer: &mut [u8]) -> io::Result<()> {
|
|
r.read_exact(buffer)?; // Compliant: This reads the entire buffer.
|
|
Ok(())
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#unused_io_amount
|