Skip to contentSkip to conversation

rust: bitmap: document panics in `next_bit` and `next_zero_bit`

PATCH Superseded Georgios Androutsopoulos 2 participants 2 messages Aug 27 – Aug 28

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: bitmap: document panics in `next_bit` and `next_zero_bit`

next_bit() and next_zero_bit() use bitmap_assert!() to check that start is less than self.len(), which panics when CONFIG_RUST_BITMAP_HARDENED is enabled. However, neither function has a # Panics section, and both document that None is returned for exactly the input that triggers the panic.

Add the missing # Panics sections and condition the None case on CONFIG_RUST_BITMAP_HARDENED being disabled, matching the style used by set_bit(), clear_bit() and their atomic versions in the same file. Also add the missing blank doc comment line before Returns in next_zero_bit().

---
 rust/kernel/bitmap.rs | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
rust/kernel/bitmap.rs +15 -2
index b27e0ec80..b33f7ce5c 100644
@@ -459,7 +459,13 @@ pub fn last_bit(&self) -> Option<usize> {
 
     /// Finds next set bit, starting from `start`.
     ///
-    /// Returns `None` if `start` is greater or equal to `self.nbits`.
+    /// If `CONFIG_RUST_BITMAP_HARDENED` is not enabled and `start` is greater
+    /// than or equal to `self.len()`, returns `None`.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `CONFIG_RUST_BITMAP_HARDENED` is enabled and `start` is
+    /// greater than or equal to `self.len()`.
     #[inline]
     pub fn next_bit(&self, start: usize) -> Option<usize> {
         bitmap_assert!(
@@ -479,7 +485,14 @@ pub fn next_bit(&self, start: usize) -> Option<usize> {
     }
 
     /// Finds next zero bit, starting from `start`.
-    /// Returns `None` if `start` is greater than or equal to `self.len()`.
+    ///
+    /// If `CONFIG_RUST_BITMAP_HARDENED` is not enabled and `start` is greater
+    /// than or equal to `self.len()`, returns `None`.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `CONFIG_RUST_BITMAP_HARDENED` is enabled and `start` is
+    /// greater than or equal to `self.len()`.
     #[inline]
     pub fn next_zero_bit(&self, start: usize) -> Option<usize> {
         bitmap_assert!(

base-commit: 73e3f0710014fe6d4ed98cfc02292f6121db7558

Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 12 lines · Georgios Androutsopoulos

next_bit() and next_zero_bit() use bitmap_assert!() to check that start is less than self.len(), which panics when CONFIG_RUST_BITMAP_HARDENED is enabled. However, neither function has a # Panics section, and both document that None is returned for exactly the input that triggers the panic.

Add the missing # Panics sections and condition the None case on CONFIG_RUST_BITMAP_HARDENED being disabled, matching the style used by set_bit(), clear_bit() and their atomic versions in the same file. Also add the missing blank doc comment line before Returns in next_zero_bit().

Quoted 3 lines

There's an unncessary empty newline between Link: and Signed-off-by:

Quoted 9 lines
 rust/kernel/bitmap.rs | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
rust/kernel/bitmap.rs +0 -0
index b27e0ec80..b33f7ce5c 100644
@@ -459,7 +459,13 @@ pub fn last_bit(&self) -> Option<usize> {
 

rust/kernel/bitmap.rs pub fn last_bit(&self) -> Option<usize> { · line 468

Quoted 10 lines
rust/kernel/bitmap.rs +7 -1
     /// Finds next set bit, starting from `start`.
     ///
-    /// Returns `None` if `start` is greater or equal to `self.nbits`.
+    /// If `CONFIG_RUST_BITMAP_HARDENED` is not enabled and `start` is greater
+    /// than or equal to `self.len()`, returns `None`.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `CONFIG_RUST_BITMAP_HARDENED` is enabled and `start` is
+    /// greater than or equal to `self.len()`.

This wording is a bit repetetive. I think we can simplify to:

/// Finds next set bit, starting from start.
///
/// Returns None if no bits are set on or after the given index. The /// index start must be in bounds.
///
/// # Panics
///
/// Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and start is /// out of bounds.

Alice