This repository has been archived by the owner on Aug 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
IoReader enhancements #80
Merged
pyfisch
merged 6 commits into
pyfisch:master
from
chrysn-pull-requests:ioreader-enhancements
Nov 23, 2018
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
519906b
tests: Add case with multiple infinite (byte) strings
chrysn 1107667
tests: Add a case for various EOFs inside indefinite strings
chrysn 9cca031
IO Reader: Use provided read_exact function
chrysn 708d981
IO Reader: Avoid clearing the scratch buffer before writing
chrysn 67c3b49
IO Reader: Don't loop in 16k chunks
chrysn 6814cb9
tests: Add zero-length byte strings into indefinite sequences
chrysn 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 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 |
---|---|---|
|
@@ -110,49 +110,46 @@ where | |
&mut self, | ||
mut n: usize, | ||
scratch: &mut Vec<u8>, | ||
mut scratch_offset: usize, | ||
scratch_offset: usize, | ||
) -> Result<Reference<'de>> { | ||
while n > 0 { | ||
// defend against malicious input pretending to be huge strings by limiting growth | ||
let to_read = cmp::min(n, 16 * 1024); | ||
n -= to_read; | ||
|
||
if to_read > scratch.len() - scratch_offset { | ||
scratch.resize(scratch_offset + to_read, 0); | ||
} | ||
assert!(scratch.len() == scratch_offset); | ||
|
||
if let Some(ch) = self.ch.take() { | ||
scratch[scratch_offset] = ch; | ||
scratch_offset += 1; | ||
} | ||
// defend against malicious input pretending to be huge strings by limiting growth | ||
scratch.reserve(cmp::min(n, 16 * 1024)); | ||
|
||
self.read_into(&mut scratch[scratch_offset..])?; | ||
scratch_offset = scratch.len(); | ||
if let Some(ch) = self.ch.take() { | ||
scratch.push(ch); | ||
n -= 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we ever call this method with |
||
} | ||
|
||
Ok(Reference::Copied) | ||
let transfer_result = { | ||
// Prepare for take() (which consumes its reader) by creating a reference adaptor | ||
// that'll only live in this block | ||
let reference = self.reader.by_ref(); | ||
// Append the first n bytes of the reader to the scratch vector (or up to | ||
// an error or EOF indicated by a shorter read) | ||
let mut taken = reference.take(n as u64); | ||
taken.read_to_end(scratch) | ||
}; | ||
|
||
match transfer_result { | ||
Ok(r) if r == n => Ok(Reference::Copied), | ||
Ok(_) => Err(Error::syntax( | ||
ErrorCode::EofWhileParsingValue, | ||
self.offset(), | ||
)), | ||
Err(e) => Err(Error::io(e)), | ||
} | ||
} | ||
|
||
fn read_into(&mut self, mut buf: &mut [u8]) -> Result<()> { | ||
while !buf.is_empty() { | ||
match self.reader.read(buf) { | ||
Ok(0) => { | ||
return Err(Error::syntax( | ||
ErrorCode::EofWhileParsingValue, | ||
self.offset(), | ||
)) | ||
} | ||
Ok(count) => { | ||
buf = &mut { | ||
buf | ||
}[count..] | ||
} | ||
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} | ||
Err(e) => return Err(Error::io(e)), | ||
fn read_into(&mut self, buf: &mut [u8]) -> Result<()> { | ||
self.reader.read_exact(buf).map_err(|e| { | ||
if e.kind() == io::ErrorKind::UnexpectedEof { | ||
Error::syntax(ErrorCode::EofWhileParsingValue, self.offset()) | ||
} else { | ||
Error::io(e) | ||
} | ||
} | ||
|
||
Ok(()) | ||
}) | ||
} | ||
|
||
#[inline] | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this always the case? If it is, we should just remove the scratch_offset parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, at least while the Deserializer uses it as it does now.
I haven't changed the internal API part yet because there are two ways of going forward here (drop scratch_offset, or drop the buf.clear() in the reader and make scratch_offset a boolean "start anew"), see d1903ff -- but I think those are better explored in a separate change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok cool - it seems fine to follow up in a separate change.