Skip to content

Commit 3a1ec42

Browse files
HsiuCheYuojeda
authored andcommitted
rust: num: bounded: mark __new as unsafe
The `Bounded::__new()` constructor relies on the caller to ensure the value can be represented within N bits. Failing to uphold this requirement breaks the type invariant. Mark it as unsafe and document this requirement in a Safety section to make the contract explicit. Update all call sites to use unsafe blocks and change their comments from `INVARIANT:` to `SAFETY:`, as they are now justifying unsafe operations rather than establishing type invariants. Fixes: 01e345e ("rust: num: add Bounded integer wrapping type") Link: https://lore.kernel.org/all/aS1qC_ol2XEpZ44b@google.com/ Reported-by: Miguel Ojeda <ojeda@kernel.org> Closes: #1211 Signed-off-by: Hsiu Che Yu <yu.whisper.personal@gmail.com> Acked-by: Alexandre Courbot <acourbot@nvidia.com> Link: https://patch.msgid.link/20251204033849.23480-1-yu.whisper.personal@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 609db7e commit 3a1ec42

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

rust/kernel/num/bounded.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,9 @@ macro_rules! impl_const_new {
259259
assert!(fits_within!(VALUE, $type, N));
260260
}
261261

262-
// INVARIANT: `fits_within` confirmed that `VALUE` can be represented within
262+
// SAFETY: `fits_within` confirmed that `VALUE` can be represented within
263263
// `N` bits.
264-
Self::__new(VALUE)
264+
unsafe { Self::__new(VALUE) }
265265
}
266266
}
267267
)*
@@ -284,7 +284,11 @@ where
284284
///
285285
/// The caller remains responsible for checking, either statically or dynamically, that `value`
286286
/// can be represented as a `T` using at most `N` bits.
287-
const fn __new(value: T) -> Self {
287+
///
288+
/// # Safety
289+
///
290+
/// The caller must ensure that `value` can be represented within `N` bits.
291+
const unsafe fn __new(value: T) -> Self {
288292
// Enforce the type invariants.
289293
const {
290294
// `N` cannot be zero.
@@ -328,8 +332,8 @@ where
328332
/// ```
329333
pub fn try_new(value: T) -> Option<Self> {
330334
fits_within(value, N).then(|| {
331-
// INVARIANT: `fits_within` confirmed that `value` can be represented within `N` bits.
332-
Self::__new(value)
335+
// SAFETY: `fits_within` confirmed that `value` can be represented within `N` bits.
336+
unsafe { Self::__new(value) }
333337
})
334338
}
335339

@@ -370,8 +374,8 @@ where
370374
"Requested value larger than maximal representable value."
371375
);
372376

373-
// INVARIANT: `fits_within` confirmed that `expr` can be represented within `N` bits.
374-
Self::__new(expr)
377+
// SAFETY: `fits_within` confirmed that `expr` can be represented within `N` bits.
378+
unsafe { Self::__new(expr) }
375379
}
376380

377381
/// Returns the wrapped value as the backing type.
@@ -410,9 +414,9 @@ where
410414
);
411415
}
412416

413-
// INVARIANT: The value did fit within `N` bits, so it will all the more fit within
417+
// SAFETY: The value did fit within `N` bits, so it will all the more fit within
414418
// the larger `M` bits.
415-
Bounded::__new(self.0)
419+
unsafe { Bounded::__new(self.0) }
416420
}
417421

418422
/// Attempts to shrink the number of bits usable for `self`.
@@ -466,9 +470,9 @@ where
466470
// `U` and `T` have the same sign, hence this conversion cannot fail.
467471
let value = unsafe { U::try_from(self.get()).unwrap_unchecked() };
468472

469-
// INVARIANT: Although the backing type has changed, the value is still represented within
473+
// SAFETY: Although the backing type has changed, the value is still represented within
470474
// `N` bits, and with the same signedness.
471-
Bounded::__new(value)
475+
unsafe { Bounded::__new(value) }
472476
}
473477
}
474478

@@ -944,9 +948,9 @@ macro_rules! impl_from_primitive {
944948
Self: AtLeastXBits<{ <$type as Integer>::BITS as usize }>,
945949
{
946950
fn from(value: $type) -> Self {
947-
// INVARIANT: The trait bound on `Self` guarantees that `N` bits is
951+
// SAFETY: The trait bound on `Self` guarantees that `N` bits is
948952
// enough to hold any value of the source type.
949-
Self::__new(T::from(value))
953+
unsafe { Self::__new(T::from(value)) }
950954
}
951955
}
952956
)*
@@ -1051,8 +1055,8 @@ where
10511055
T: Integer + From<bool>,
10521056
{
10531057
fn from(value: bool) -> Self {
1054-
// INVARIANT: A boolean can be represented using a single bit, and thus fits within any
1058+
// SAFETY: A boolean can be represented using a single bit, and thus fits within any
10551059
// integer type for any `N` > 0.
1056-
Self::__new(T::from(value))
1060+
unsafe { Self::__new(T::from(value)) }
10571061
}
10581062
}

0 commit comments

Comments
 (0)