Skip to content

Commit 4996659

Browse files
tob-joeGPT 5.5
andcommitted
Enforce UintRef slice length checks in release builds
Use assert! instead of debug_assert! for UintRef slice-copy length checks so mismatched lengths keep the documented panic behavior in optimized builds. Add release-mode regression tests for copy_from_slice and conditional_copy_from_slice with longer source slices. Co-authored-by: GPT 5.5 <gpt-5.5@openai.com>
1 parent 4c6f87d commit 4996659

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

src/uint/ref_type/slice.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl UintRef {
3030
#[track_caller]
3131
pub const fn copy_from_slice(&mut self, limbs: &[Limb]) {
3232
// TODO core::slice::copy_from_slice should eventually be const
33-
debug_assert!(self.limbs.len() == limbs.len(), "length mismatch");
33+
assert!(self.limbs.len() == limbs.len(), "length mismatch");
3434
let mut i = 0;
3535
while i < self.limbs.len() {
3636
self.limbs[i] = limbs[i];
@@ -45,7 +45,7 @@ impl UintRef {
4545
#[inline(always)]
4646
#[track_caller]
4747
pub const fn conditional_copy_from_slice(&mut self, limbs: &[Limb], copy: Choice) {
48-
debug_assert!(self.limbs.len() == limbs.len(), "length mismatch");
48+
assert!(self.limbs.len() == limbs.len(), "length mismatch");
4949
let mut i = 0;
5050
while i < self.limbs.len() {
5151
self.limbs[i] = Limb::select(self.limbs[i], limbs[i], copy);
@@ -120,3 +120,23 @@ impl UintRef {
120120
self.limbs.is_empty()
121121
}
122122
}
123+
124+
#[cfg(test)]
125+
mod tests {
126+
use super::UintRef;
127+
use crate::{Choice, Limb};
128+
129+
#[test]
130+
#[should_panic(expected = "length mismatch")]
131+
fn copy_from_slice_rejects_mismatched_lengths() {
132+
let mut dst = [Limb::ZERO];
133+
UintRef::new_mut(&mut dst).copy_from_slice(&[Limb::ONE, Limb(2)]);
134+
}
135+
136+
#[test]
137+
#[should_panic(expected = "length mismatch")]
138+
fn conditional_copy_from_slice_rejects_mismatched_lengths() {
139+
let mut dst = [Limb::ZERO];
140+
UintRef::new_mut(&mut dst).conditional_copy_from_slice(&[Limb::ONE, Limb(2)], Choice::TRUE);
141+
}
142+
}

0 commit comments

Comments
 (0)