From 8db0408e6a6f6e90bdc45e21deec59e597fe9417 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Fri, 15 Mar 2024 08:23:17 +0100 Subject: [PATCH] Add extra assertions to `NaiveDate::from_yof` --- src/naive/date/mod.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/naive/date/mod.rs b/src/naive/date/mod.rs index 9c6d04b480..cf36e328de 100644 --- a/src/naive/date/mod.rs +++ b/src/naive/date/mod.rs @@ -1437,10 +1437,14 @@ impl NaiveDate { /// Create a new `NaiveDate` from a raw year-ordinal-flags `i32`. /// /// In a valid value an ordinal is never `0`, and neither are the year flags. This method - /// doesn't do any validation. + /// doesn't do any validation in release builds. #[inline] const fn from_yof(yof: i32) -> NaiveDate { - debug_assert!(yof != 0); + // The following are the invariants our ordinal and flags should uphold for a valid + // `NaiveDate`. + debug_assert!(((yof & OL_MASK) >> 3) > 1); + debug_assert!(((yof & OL_MASK) >> 3) <= MAX_OL); + debug_assert!((yof & 0b111) != 000); NaiveDate { yof: unsafe { NonZeroI32::new_unchecked(yof) } } }