Skip to content

Commit cbc4ecd

Browse files
authored
ctutils: improve and expand rustdoc warnings (#1278)
Some of them weren't formatted properly and were rendering incorrectly.
1 parent b82eb7e commit cbc4ecd

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

ctutils/src/choice.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ impl Choice {
6666
/// HACK: workaround to allow `const fn` boolean support on Rust 1.85.
6767
///
6868
/// This does not apply `black_box` to the output.
69+
///
70+
/// <div class = "warning">
71+
/// <b>Security Warning</b>
72+
///
73+
/// See the security warnings for [`Choice::to_bool`].
74+
/// </div>
6975
// TODO(tarcieri): deprecate/remove this in favor of `to_bool` when MSRV is Rust 1.86
7076
pub const fn to_bool_vartime(self) -> bool {
7177
self.0 != 0
@@ -355,6 +361,16 @@ impl From<Choice> for u8 {
355361
}
356362
}
357363

364+
/// Convert `Choice` into a `bool`.
365+
///
366+
/// <div class = "warning">
367+
/// <b>Security Warning</b>
368+
///
369+
/// Using this function will introduce timing variability, since computing this at all currently
370+
/// requires a branch.
371+
///
372+
/// See the security warnings for [`Choice::to_bool`].
373+
/// </div>
358374
impl From<Choice> for bool {
359375
fn from(choice: Choice) -> bool {
360376
choice.to_bool()

ctutils/src/ct_option.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ impl<T> CtOption<T> {
116116
self.value
117117
}
118118

119-
/// Return a copy of the contained value without consuming `self`.
119+
/// Return the contained value, consuming the `self` value, with `const fn` support.
120+
///
121+
/// Relies on a `Copy` bound which implies `!Drop` which is needed to be able to move out of
122+
/// `self` in a `const fn` without `feature(const_precise_live_drops)`.
120123
///
121124
/// # Panics
122125
/// In the event `self.is_some()` is [`Choice::FALSE`], panics with a custom panic message
@@ -157,6 +160,8 @@ impl<T> CtOption<T> {
157160
/// type inference.
158161
///
159162
/// <div class="warning">
163+
/// <b>Warning: variable-time!</b>
164+
///
160165
/// This implementation doesn't intend to be constant-time nor try to protect the leakage of the
161166
/// `T` value since the [`Option`] will do it anyway.
162167
/// </div>
@@ -176,6 +181,8 @@ impl<T> CtOption<T> {
176181
/// `const fn` and destructors.
177182
///
178183
/// <div class="warning">
184+
/// <b>Warning: variable-time!</b>
185+
///
179186
/// This implementation doesn't intend to be constant-time nor try to protect the leakage of the
180187
/// `T` value since the [`Option`] will do it anyway.
181188
/// </div>
@@ -310,8 +317,10 @@ impl<T> CtOption<T> {
310317
/// [`Choice::FALSE`].
311318
///
312319
/// <div class="warning">
320+
/// <b>Warning: variable-time!</b>
321+
///
313322
/// This implementation doesn't intend to be constant-time nor try to protect the leakage of the
314-
/// `T` value since the [`Option`] will do it anyway.
323+
/// `T` value since the [`Result`] will do it anyway.
315324
/// </div>
316325
#[inline]
317326
pub fn ok_or<E>(self, err: E) -> Result<T, E> {
@@ -320,6 +329,13 @@ impl<T> CtOption<T> {
320329

321330
/// Transforms a `CtOption<T>` into a `Result<T, E>` by unconditionally calling the provided
322331
/// callback value and using its result in the event `self.is_some()` is [`Choice::FALSE`].
332+
///
333+
/// <div class="warning">
334+
/// <b>Warning: variable-time!</b>
335+
///
336+
/// This implementation doesn't intend to be constant-time nor try to protect the leakage of the
337+
/// `T` value since the [`Result`] will do it anyway.
338+
/// </div>
323339
#[inline]
324340
pub fn ok_or_else<E, F>(self, err: F) -> Result<T, E>
325341
where
@@ -524,10 +540,10 @@ impl<T: Default> Default for CtOption<T> {
524540
/// [`CtOption::is_some`] is a truthy or falsy [`Choice`].
525541
///
526542
/// <div class="warning">
543+
/// <b>Warning: variable-time!</b>
527544
///
528545
/// This implementation doesn't intend to be constant-time nor try to protect the leakage of the
529546
/// `T` value since the `Option` will do it anyway.
530-
///
531547
/// </div>
532548
impl<T> From<CtOption<T>> for Option<T> {
533549
fn from(src: CtOption<T>) -> Option<T> {

0 commit comments

Comments
 (0)