Skip to content

Commit 9f685e1

Browse files
committed
Added assert_unsafe_precondition! to check that signed input is nonnegative in debug mode
1 parent df163bb commit 9f685e1

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

library/core/src/num/int_sqrt.rs

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//! "Wikipedia contributors. Integer square root. Wikipedia, The Free
1212
//! Encyclopedia."
1313
14+
use crate::assert_unsafe_precondition;
15+
1416
/// This array stores the [integer square roots][1] and remainders of each
1517
/// [`u8`](prim@u8) value. For example, `U8_ISQRT_WITH_REMAINDER[17]` will be
1618
/// `(4, 1)` because the integer square root of 17 is 4 and because 17 is 1
@@ -96,6 +98,16 @@ macro_rules! signed_fn {
9698
without modifying the original"]
9799
#[inline(always)]
98100
pub const unsafe fn $SignedT(n: $SignedT) -> $SignedT {
101+
assert_unsafe_precondition!(
102+
check_library_ub,
103+
concat!(
104+
"`isqrt` internal error: Passing a negative number to `core::num::int_sqrt::",
105+
stringify!($SignedT),
106+
"` causes undefined behavior."
107+
),
108+
(n: $SignedT = n) => n >= 0
109+
);
110+
99111
$UnsignedT(n as $UnsignedT) as $SignedT
100112
}
101113
};

0 commit comments

Comments
 (0)