Skip to content

Commit

Permalink
Give better error messages when decoding data with trailing newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
untitaker committed Sep 26, 2020
1 parent 2dc0296 commit d15cd38
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,17 @@ fn decode_helper(
// and the fast decode logic cannot handle padding
0 => INPUT_CHUNK_LEN,
// 1 and 5 trailing bytes are illegal: can't decode 6 bits of input into a byte
1 | 5 => return Err(DecodeError::InvalidLength),
1 | 5 => {
// trailing whitespace is so common that it's worth it to check the last byte to
// possibly return a better error message
if let Some(b) = input.last() {
if decode_table[*b as usize] == tables::INVALID_VALUE {
return Err(DecodeError::InvalidByte(input.len() - 1, *b));
}
}

return Err(DecodeError::InvalidLength);
}
// This will decode to one output byte, which isn't enough to overwrite the 2 extra bytes
// written by the fast decode loop. So, we have to ignore both these 2 bytes and the
// previous chunk.
Expand Down Expand Up @@ -865,4 +875,14 @@ mod tests {
decode_config(b"+//+", crate::STANDARD_NO_PAD)
);
}

#[test]
fn decode_invalid_trailing_bytes() {
// The case of trailing newlines is common enough to warrant a test for a good error
// message.
assert_eq!(
decode(b"Zm9vCg==\n"),
Err(DecodeError::InvalidByte(8, b'\n'))
);
}
}

0 comments on commit d15cd38

Please sign in to comment.