| @@ -355,10 +355,14 @@ fn read_str_len(cur: Cursor, width: Int) -> Result<(Value, Cursor), DecodeError> |
| 355 | 355 | } |
| 356 | 356 | |
| 357 | 357 | fn read_str(cur: Cursor, len: Int) -> Result<(Value, Cursor), DecodeError> { |
| 358 | | − use (chunk, cur) <- result.try(take(cur, len)) |
| 359 | | − match bytes.to_string(chunk) { |
| 360 | | − Ok(s) -> Ok((Str(s), cur)), |
| 361 | | − Error(e) -> Error(Malformed(cur.at - len, "a str that is not UTF-8: {e}")), |
| 358 | + let left = bytes.length(cur.bytes) - cur.at |
| 359 | + if len < 0 || len > left { |
| 360 | + Error(Malformed(cur.at, "a length of {len} with {left} bytes left")) |
| 361 | + } else { |
| 362 | + match bytes.to_string_range(cur.bytes, cur.at, len) { |
| 363 | + Ok(s) -> Ok((Str(s), Cursor(cur.bytes, cur.at + len))), |
| 364 | + Error(e) -> Error(Malformed(cur.at, "a str that is not UTF-8: {e}")), |
| 365 | + } |
| 362 | 366 | } |
| 363 | 367 | } |
| 364 | 368 | |
| @@ -442,18 +446,35 @@ fn check_count(cur: Cursor, count: Int, what: String) -> Result<(), DecodeError> |
| 442 | 446 | } |
| 443 | 447 | |
| 444 | 448 | fn read_byte(cur: Cursor) -> Result<(Int, Cursor), DecodeError> { |
| 445 | | − match bytes.get(cur.bytes, cur.at) { |
| 446 | | − Some(byte) -> Ok((byte, Cursor(cur.bytes, cur.at + 1))), |
| 447 | | − None -> Error(Malformed(cur.at, "the input ends early")), |
| 449 | + let byte = bytes.read_u8(cur.bytes, cur.at) |
| 450 | + if byte < 0 { |
| 451 | + Error(Malformed(cur.at, "the input ends early")) |
| 452 | + } else { |
| 453 | + Ok((byte, Cursor(cur.bytes, cur.at + 1))) |
| 448 | 454 | } |
| 449 | 455 | } |
| 450 | 456 | |
| 451 | 457 | /// The unsigned big-endian integer of the next `width` bytes (1, 2, 4 or |
| 452 | | −/// 8; 8 can be a big integer, above 2^62). |
| 458 | +/// 8; 8 can be a big integer, above 2^62), read in place. |
| 453 | 459 | fn read_uint(cur: Cursor, width: Int) -> Result<(Int, Cursor), DecodeError> { |
| 454 | | − use (chunk, cur) <- result.try(take(cur, width)) |
| 455 | | − let n = list.fold(bytes.to_list(chunk), 0, fn(acc, byte) { int.shift_left(acc, 8) + byte }) |
| 456 | | − Ok((n, cur)) |
| 460 | + let left = bytes.length(cur.bytes) - cur.at |
| 461 | + if width > left { |
| 462 | + Error(Malformed(cur.at, "a length of {width} with {left} bytes left")) |
| 463 | + } else { |
| 464 | + let n = |
| 465 | + match width { |
| 466 | + 1 -> bytes.read_u8(cur.bytes, cur.at), |
| 467 | + 2 -> bytes.read_u16be(cur.bytes, cur.at), |
| 468 | + 4 -> bytes.read_u32be(cur.bytes, cur.at), |
| 469 | + _ -> bytes.read_u64be(cur.bytes, cur.at), |
| 470 | + } |
| 471 | + if n < 0 { |
| 472 | + // Unreachable: the bounds were checked above. |
| 473 | + Error(Malformed(cur.at, "a length of {width} with {left} bytes left")) |
| 474 | + } else { |
| 475 | + Ok((n, Cursor(cur.bytes, cur.at + width))) |
| 476 | + } |
| 477 | + } |
| 457 | 478 | } |
| 458 | 479 | |
| 459 | 480 | /// The next `n` bytes, if they are all there. |
| @@ -794,6 +815,44 @@ fn malformed_inputs_are_errors() { |
| 794 | 815 | read(bytes.from_list([129, 161, 97])) == Error(Malformed(3, "the input ends early")), |
| 795 | 816 | debug(read(bytes.from_list([129, 161, 97]))), |
| 796 | 817 | ) |
| 818 | + // Truncated at every width of integer read, and inside bin and ext |
| 819 | + // payloads. |
| 820 | + assert( |
| 821 | + read(bytes.from_list([205, 1])) == Error(Malformed(1, "a length of 2 with 1 bytes left")), |
| 822 | + debug(read(bytes.from_list([205, 1]))), |
| 823 | + ) |
| 824 | + assert( |
| 825 | + read(bytes.from_list([206, 1, 2, 3])) == Error(Malformed(1, "a length of 4 with 3 bytes left")), |
| 826 | + debug(read(bytes.from_list([206, 1, 2, 3]))), |
| 827 | + ) |
| 828 | + assert( |
| 829 | + read(bytes.from_list([207, 1, 2, 3, 4, 5, 6, 7])) |
| 830 | + == Error(Malformed(1, "a length of 8 with 7 bytes left")), |
| 831 | + debug(read(bytes.from_list([207, 1, 2, 3, 4, 5, 6, 7]))), |
| 832 | + ) |
| 833 | + assert( |
| 834 | + read(bytes.from_list([203, 1, 2, 3])) == Error(Malformed(1, "a length of 8 with 3 bytes left")), |
| 835 | + debug(read(bytes.from_list([203, 1, 2, 3]))), |
| 836 | + ) |
| 837 | + assert( |
| 838 | + read(bytes.from_list([196, 5, 97])) == Error(Malformed(2, "a length of 5 with 1 bytes left")), |
| 839 | + debug(read(bytes.from_list([196, 5, 97]))), |
| 840 | + ) |
| 841 | + // fixext 1 without its tag byte, and fixext 2 without its payload. |
| 842 | + assert( |
| 843 | + read(bytes.from_list([212])) == Error(Malformed(1, "the input ends early")), |
| 844 | + debug(read(bytes.from_list([212]))), |
| 845 | + ) |
| 846 | + assert( |
| 847 | + read(bytes.from_list([213, 5])) == Error(Malformed(2, "a length of 2 with 0 bytes left")), |
| 848 | + debug(read(bytes.from_list([213, 5]))), |
| 849 | + ) |
| 850 | + // A str cut in the middle of a UTF-8 sequence. |
| 851 | + assert( |
| 852 | + read(bytes.from_list([162, 104, 255])) |
| 853 | + == Error(Malformed(1, "a str that is not UTF-8: invalid UTF-8 at byte 1")), |
| 854 | + debug(read(bytes.from_list([162, 104, 255]))), |
| 855 | + ) |
| 797 | 856 | } |
| 798 | 857 | |
| 799 | 858 | @test |