Repository navigation
Introduce a wrapper on std::io::Stdin that checks for closed STDIN before reading
#15835
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
Open
RenjiSann
wants to merge
6
commits into
uutils:main
Choose a base branch
from
RenjiSann:feature-ustdin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+207
−23
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
069f582
uucore: Introduce `ustdin`, a wrapper for stdin...
RenjiSann 5d40075
tests: Allow to close stdin
RenjiSann 19f8a7b
checksum: Use `ustdin`
RenjiSann 59224f7
base*: Use `ustdin`
RenjiSann 37408ea
csplit: Use `ustdin`
RenjiSann 0f1cd0c
cpslit: rework error handling to print error messages in the right order
RenjiSann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| use std::io; | ||
|
|
||
| #[cfg(all(feature = "signals", unix, not(target_os = "fuchsia")))] | ||
| use crate::{signals, translate}; | ||
|
RenjiSann marked this conversation as resolved.
RenjiSann marked this conversation as resolved.
|
||
|
|
||
| #[cfg_attr( | ||
| not(all(feature = "signals", unix, not(target_os = "fuchsia"))), | ||
| expect(clippy::unnecessary_wraps) | ||
| )] | ||
| fn err_if_stdin_closed() -> io::Result<()> { | ||
| #[cfg(all(feature = "signals", unix, not(target_os = "fuchsia")))] | ||
| if signals::stdin_was_closed() { | ||
| return Err(io::Error::other(translate!("error-bad-file-descriptor"))); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Wrapper around [`std::io::Stdin`] that checks if stdin was closed | ||
| /// before trying to read. | ||
| pub struct UStdin { | ||
| inner: io::Stdin, | ||
| } | ||
|
|
||
| impl UStdin { | ||
| pub fn lock(&self) -> UStdinLock<'static> { | ||
| UStdinLock { | ||
| inner: self.inner.lock(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl io::Read for UStdin { | ||
| fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
| err_if_stdin_closed()?; | ||
| self.inner.read(buf) | ||
| } | ||
|
|
||
| fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { | ||
| err_if_stdin_closed()?; | ||
| self.inner.read_to_end(buf) | ||
| } | ||
|
|
||
| fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { | ||
|
RenjiSann marked this conversation as resolved.
|
||
| err_if_stdin_closed()?; | ||
| self.inner.read_to_string(buf) | ||
| } | ||
|
|
||
| fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { | ||
| err_if_stdin_closed()?; | ||
| self.inner.read_exact(buf) | ||
| } | ||
| } | ||
|
|
||
| pub struct UStdinLock<'a> { | ||
| inner: io::StdinLock<'a>, | ||
| } | ||
|
|
||
| impl io::Read for UStdinLock<'_> { | ||
| fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
| err_if_stdin_closed()?; | ||
| self.inner.read(buf) | ||
| } | ||
|
|
||
| fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { | ||
| err_if_stdin_closed()?; | ||
| self.inner.read_to_end(buf) | ||
| } | ||
|
|
||
| fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { | ||
| err_if_stdin_closed()?; | ||
| self.inner.read_to_string(buf) | ||
| } | ||
|
|
||
| fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { | ||
| err_if_stdin_closed()?; | ||
| self.inner.read_exact(buf) | ||
| } | ||
| } | ||
|
|
||
| impl io::BufRead for UStdinLock<'_> { | ||
| fn fill_buf(&mut self) -> io::Result<&[u8]> { | ||
| err_if_stdin_closed()?; | ||
| self.inner.fill_buf() | ||
| } | ||
|
|
||
| fn consume(&mut self, amount: usize) { | ||
| self.inner.consume(amount); | ||
| } | ||
| } | ||
|
|
||
| pub fn stdin() -> UStdin { | ||
| UStdin { inner: io::stdin() } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.