mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Fix SHAKE digest length overflow in hashlib (#7125)
This commit is contained in:
1 parent
f92a7dd9ca
commit
3f49f4107c
2 files changed
+9
-4
No files matched your search
Vendored
-2
@@ -295,8 +295,6 @@ class HashLibTestCase(unittest.TestCase):
|
||||
self.assertIsInstance(h.digest(), bytes)
|
||||
self.assertEqual(hexstr(h.digest()), h.hexdigest())
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_digest_length_overflow(self):
|
||||
# See issue #34922
|
||||
large_sizes = (2**29, 2**32-10, 2**32+10, 2**61, 2**64-10, 2**64+10)
|
||||
|
||||
@@ -83,9 +83,16 @@ pub mod _hashlib {
|
||||
}
|
||||
|
||||
impl XofDigestArgs {
|
||||
// Match CPython's SHAKE output guard in Modules/sha3module.c.
|
||||
const MAX_SHAKE_OUTPUT_LENGTH: usize = 1 << 29;
|
||||
|
||||
fn length(&self, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
usize::try_from(self.length)
|
||||
.map_err(|_| vm.new_value_error("length must be non-negative"))
|
||||
let length = usize::try_from(self.length)
|
||||
.map_err(|_| vm.new_value_error("length must be non-negative"))?;
|
||||
if length >= Self::MAX_SHAKE_OUTPUT_LENGTH {
|
||||
return Err(vm.new_value_error("length is too large"));
|
||||
}
|
||||
Ok(length)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user