Skip to content

Commit b3c03be

Browse files
teh-cmcPSeitz
authored andcommitted
implement test demonstrating the issue
1 parent a61ee5f commit b3c03be

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/block/compress.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ fn count_same_bytes(input: &[u8], cur: &mut usize, source: &[u8], candidate: usi
221221
/// to produce a total length. When the byte value is 255, another byte must read and added, and so
222222
/// on. There can be any number of bytes of value "255" following token
223223
#[inline]
224-
fn write_integer(output: &mut impl Sink, mut n: usize) {
224+
pub(super) fn write_integer(output: &mut impl Sink, mut n: usize) {
225225
// Note: Since `n` is usually < 0xFF and writing multiple bytes to the output
226226
// requires 2 branches of bound check (due to the possibility of add overflows)
227227
// the simple byte at a time implementation below is faster in most cases.

src/block/decompress.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ unsafe fn copy_from_dict(
128128
/// is encoded to _255 + 255 + 255 + 4 = 769_. The bytes after the first 4 is ignored, because
129129
/// 4 is the first non-0xFF byte.
130130
#[inline]
131-
fn read_integer_ptr(
131+
pub(super) fn read_integer_ptr(
132132
input_ptr: &mut *const u8,
133133
_input_ptr_end: *const u8,
134134
) -> Result<u32, DecompressError> {

src/block/decompress_safe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use alloc::vec::Vec;
2525
/// is encoded to _255 + 255 + 255 + 4 = 769_. The bytes after the first 4 is ignored, because
2626
/// 4 is the first non-0xFF byte.
2727
#[inline]
28-
fn read_integer(input: &[u8], input_pos: &mut usize) -> Result<u32, DecompressError> {
28+
pub(super) fn read_integer(input: &[u8], input_pos: &mut usize) -> Result<u32, DecompressError> {
2929
// We start at zero and count upwards.
3030
let mut n: u32 = 0;
3131
// If this byte takes value 255 (the maximum value it can take), another byte is read

src/block/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,25 @@ pub fn uncompressed_size(input: &[u8]) -> Result<(usize, &[u8]), DecompressError
152152
let rest = &input[4..];
153153
Ok((uncompressed_size, rest))
154154
}
155+
156+
#[test]
157+
#[cfg(target_pointer_width = "64")] // only relevant for 64bit CPUs
158+
fn large_integer_roundtrip() {
159+
let u32_max = usize::try_from(u32::MAX).unwrap();
160+
let value = u32_max + u32_max / 2;
161+
162+
let mut buf = vec![0u8; value / 255 + 1];
163+
let mut sink = crate::sink::SliceSink::new(&mut buf, 0);
164+
self::compress::write_integer(&mut sink, value);
165+
166+
#[cfg(feature = "safe-decode")]
167+
let value_decompressed = self::decompress_safe::read_integer(&buf, &mut 0).unwrap();
168+
169+
#[cfg(not(feature = "safe-decode"))]
170+
let value_decompressed = {
171+
let mut ptr_range = buf.as_ptr_range();
172+
self::decompress::read_integer_ptr(&mut ptr_range.start, ptr_range.end).unwrap()
173+
};
174+
175+
assert_eq!(value, value_decompressed);
176+
}

0 commit comments

Comments
 (0)