From fcd67514a62b61d70bfe5ef672ee489a2c415ba7 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 29 Sep 2024 21:38:04 +0200 Subject: [PATCH] fix: logic error in encode_unaligned_chunks --- src/arch/generic.rs | 7 ++++--- src/lib.rs | 6 +++--- tests/test.rs | 25 +++++++++++++++++++++---- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/arch/generic.rs b/src/arch/generic.rs index 0f70f4c..d6ebfa4 100644 --- a/src/arch/generic.rs +++ b/src/arch/generic.rs @@ -32,16 +32,17 @@ pub(crate) unsafe fn encode_unaligned_chunks( mut encode_chunk: impl FnMut(T) -> (T, T), ) { let (chunks, remainder) = chunks_unaligned::(input); - let remainder_i = chunks.len() * core::mem::size_of::(); + let n_in_chunks = chunks.len(); let chunk_output = output.cast::(); for (i, chunk) in chunks.enumerate() { let (lo, hi) = encode_chunk(chunk); unsafe { chunk_output.add(i * 2).write_unaligned(lo); - chunk_output.add((i * 2) + 1).write_unaligned(hi); + chunk_output.add(i * 2 + 1).write_unaligned(hi); } } - unsafe { encode::(remainder, unsafe { output.add(remainder_i) }) }; + let n_out_chunks = n_in_chunks * 2; + unsafe { encode::(remainder, unsafe { chunk_output.add(n_out_chunks).cast() }) }; } /// Default check function. diff --git a/src/lib.rs b/src/lib.rs index ee712f2..ef01dfa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -719,7 +719,7 @@ pub mod fuzzing { use super::*; use proptest::test_runner::TestCaseResult; use proptest::{prop_assert, prop_assert_eq}; - use std::io::Write; + use std::fmt::Write; pub fn fuzz(data: &[u8]) -> TestCaseResult { self::encode(&data)?; @@ -754,11 +754,11 @@ pub mod fuzzing { } fn mk_expected(bytes: &[u8]) -> String { - let mut s = Vec::with_capacity(bytes.len() * 2); + let mut s = String::with_capacity(bytes.len() * 2); for i in bytes { write!(s, "{i:02x}").unwrap(); } - unsafe { String::from_utf8_unchecked(s) } + s } fn test_buffer(bytes: &[u8]) -> TestCaseResult { diff --git a/tests/test.rs b/tests/test.rs index c779f0e..e1b0116 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -112,10 +112,27 @@ fn decode_upper() { #[test] #[cfg(feature = "alloc")] -fn decode_long() { - let s = "608060405234801561001057600080fd5b506040516104913803806104918339818101604052602081101561003357600080fd5b810190808051604051939291908464010000000082111561005357600080fd5b90830190602082018581111561006857600080fd5b825164010000000081118282018810171561008257600080fd5b82525081516020918201929091019080838360005b838110156100af578181015183820152602001610097565b50505050905090810190601f1680156100dc5780820380516001836020036101000a031916815260200191505b50604052505081516100f6915060009060208401906100fd565b505061019e565b828054600181600116156101000203166002900490600052602060002090601f0160209004810192826101335760008555610179565b82601f1061014c57805160ff1916838001178555610179565b82800160010185558215610179579182015b8281111561017957825182559160200191906001019061015e565b50610185929150610189565b5090565b5b80821115610185576000815560010161018a565b6102e4806101ad6000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063a41368621461003b578063cfae3217146100e3575b600080fd5b6100e16004803603602081101561005157600080fd5b81019060208101813564010000000081111561006c57600080fd5b82018360208201111561007e57600080fd5b803590602001918460018302840111640100000000831117156100a057600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610160945050505050565b005b6100eb610177565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561012557818101518382015260200161010d565b50505050905090810190601f1680156101525780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b805161017390600090602084019061020d565b5050565b60008054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156102035780601f106101d857610100808354040283529160200191610203565b820191906000526020600020905b8154815290600101906020018083116101e657829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f0160209004810192826102435760008555610289565b82601f1061025c57805160ff1916838001178555610289565b82800160010185558215610289579182015b8281111561028957825182559160200191906001019061026e565b50610295929150610299565b5090565b5b80821115610295576000815560010161029a56fea26469706673582212208b9161dfd195d53618942a72a3b481d61a7b142de919925a0b34f9c986e5707e64736f6c63430007060033"; - const_hex::check(s).unwrap(); - assert_eq!(const_hex::decode(s).unwrap(), hex::decode(s).unwrap()); +fn roundtrips() { + test_roundtrip("1234"); + test_roundtrip("00000000000011"); + test_roundtrip("0000000000000022"); + test_roundtrip("000000000000000033"); + test_roundtrip("00000000000000003344"); + test_roundtrip("05161049138038061049183398181016"); +} + +#[test] +#[cfg(feature = "alloc")] +fn roundtrip_long() { + test_roundtrip("608060405234801561001057600080fd5b506040516104913803806104918339818101604052602081101561003357600080fd5b810190808051604051939291908464010000000082111561005357600080fd5b90830190602082018581111561006857600080fd5b825164010000000081118282018810171561008257600080fd5b82525081516020918201929091019080838360005b838110156100af578181015183820152602001610097565b50505050905090810190601f1680156100dc5780820380516001836020036101000a031916815260200191505b50604052505081516100f6915060009060208401906100fd565b505061019e565b828054600181600116156101000203166002900490600052602060002090601f0160209004810192826101335760008555610179565b82601f1061014c57805160ff1916838001178555610179565b82800160010185558215610179579182015b8281111561017957825182559160200191906001019061015e565b50610185929150610189565b5090565b5b80821115610185576000815560010161018a565b6102e4806101ad6000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063a41368621461003b578063cfae3217146100e3575b600080fd5b6100e16004803603602081101561005157600080fd5b81019060208101813564010000000081111561006c57600080fd5b82018360208201111561007e57600080fd5b803590602001918460018302840111640100000000831117156100a057600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610160945050505050565b005b6100eb610177565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561012557818101518382015260200161010d565b50505050905090810190601f1680156101525780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b805161017390600090602084019061020d565b5050565b60008054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156102035780601f106101d857610100808354040283529160200191610203565b820191906000526020600020905b8154815290600101906020018083116101e657829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f0160209004810192826102435760008555610289565b82601f1061025c57805160ff1916838001178555610289565b82800160010185558215610289579182015b8281111561028957825182559160200191906001019061026e565b50610295929150610299565b5090565b5b80821115610295576000815560010161029a56fea26469706673582212208b9161dfd195d53618942a72a3b481d61a7b142de919925a0b34f9c986e5707e64736f6c63430007060033"); +} + +#[cfg(feature = "alloc")] +fn test_roundtrip(s: &str) { + const_hex::check(s).expect(s); + let decoded = const_hex::decode(s).expect(s); + assert_eq!(decoded, hex::decode(s).expect(s)); + assert_eq!(const_hex::encode(&decoded), s); } #[test]