Skip to content

Commit

Permalink
In UTF-16[BE|LE] decoder with last=true, check space without intent t…
Browse files Browse the repository at this point in the history
…o write if the decoder has pending state.

This ensures that the outer caller has space for the REPLACEMENT CHARACTER
as happens more naturally with other encodings.

Closes #44.
  • Loading branch information
hsivonen committed Dec 18, 2019
1 parent 9778376 commit e9251e1
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/utf_16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,31 @@ impl Utf16Decoder {
},
{
debug_assert!(!self.pending_bmp);
if self.lead_surrogate != 0 {
self.lead_surrogate = 0;
match self.lead_byte {
None => {
return (DecoderResult::Malformed(2, 0), src_consumed, dest.written());
if self.lead_surrogate != 0 || self.lead_byte.is_some() {
// We need to check space without intent to write in order to
// make sure that there is space for the replacement character.
match dest.check_space_bmp() {
Space::Full(_) => {
return (DecoderResult::OutputFull, 0, 0);
}
Some(_) => {
Space::Available(_) => {
if self.lead_surrogate != 0 {
self.lead_surrogate = 0;
match self.lead_byte {
None => {
return (DecoderResult::Malformed(2, 0), src_consumed, dest.written());
}
Some(_) => {
self.lead_byte = None;
return (DecoderResult::Malformed(3, 0), src_consumed, dest.written());
}
}
}
debug_assert!(self.lead_byte.is_some());
self.lead_byte = None;
return (DecoderResult::Malformed(3, 0), src_consumed, dest.written());
return (DecoderResult::Malformed(1, 0), src_consumed, dest.written());
}
}
}
match self.lead_byte {
None => {}
Some(_) => {
self.lead_byte = None;
return (DecoderResult::Malformed(1, 0), src_consumed, dest.written());
}
}
}
},
{
Expand Down

0 comments on commit e9251e1

Please sign in to comment.