Browse Source

Fix some base64 bugs

Simon Sapin 8 years ago
parent
commit
245626794e
2 changed files with 3 additions and 18 deletions
  1. 3 3
      src/forgiving_base64.rs
  2. 0 15
      tests/wpt.rs

+ 3 - 3
src/forgiving_base64.rs

@@ -71,8 +71,7 @@ impl<F, E> Decoder<F, E> where F: FnMut(&[u8]) -> Result<(), E> {
                 // A character that’s not part of the alphabet
 
                 // Remove ASCII whitespace
-                // '\t' | '\n' | '\r' was already filtered by decode_without_base64()
-                if byte == b' ' || byte == b'\x0C' {
+                if matches!(byte, b' ' | b'\t' | b'\n' | b'\r' | b'\x0C') {
                     continue
                 }
 
@@ -88,7 +87,8 @@ impl<F, E> Decoder<F, E> where F: FnMut(&[u8]) -> Result<(), E> {
             }
             self.bit_buffer <<= 6;
             self.bit_buffer |= value as u32;
-            if self.buffer_bit_length < 24 {
+            // 18 before incrementing means we’ve just reached 24
+            if self.buffer_bit_length < 18 {
                 self.buffer_bit_length += 6;
             } else {
                 // We’ve accumulated four times 6 bits, which equals three times 8 bits.

+ 0 - 15
tests/wpt.rs

@@ -89,24 +89,9 @@ fn collect_base64<F>(add_test: &mut F)
         };
 
         let should_panic = [
-            " \t\n\u{c}\r ab\t\n\u{c}\r cd\t\n\u{c}\r ",
-            " abcd",
-            "////A",
-            "///A",
-            "AAA/",
-            "AAAA/",
-            "ab cd",
             "ab==",
-            "ab\ncd",
-            "ab\rcd",
             "ab\t\n\u{c}\r =\t\n\u{c}\r =\t\n\u{c}\r ",
-            "ab\t\n\u{c}\r cd",
-            "ab\tcd",
-            "ab\u{c}cd",
             "abc=",
-            "abcd ",
-            "abcd",
-            "abcde",
         ].contains(&&*input);
         add_test(
             format!("base64 {:?}", input),