Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions oscars/src/collectors/mark_sweep/internals/gc_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,15 @@ impl GcHeader {
}

pub fn inc_roots(&self) {
// NOTE: This may panic or overflow after 2^16 - 1 roots
self.root_count.set(self.root_count.get() + 1);
// Prevent silent overflow of root_count.
// In release builds, `u16::MAX + 1` would wrap to 0 and break GC invariants,
// potentially allowing live objects to be collected incorrectly.
let current = self.root_count.get();
let new = current
.checked_add(1)
.expect("GcHeader root_count overflow");

self.root_count.set(new);
}

pub fn dec_roots(&self) {
Expand Down Expand Up @@ -170,4 +177,14 @@ mod tests {
assert!(!header.is_white(), "failed to toggle black");
assert!(!header.is_grey(), "failed to toggle black");
}

#[test]
#[should_panic(expected = "GcHeader root_count overflow")]
fn root_count_overflow_panics() {
let header = GcHeader::new_white();

for _ in 0..=u16::MAX {
header.inc_roots();
}
}
}
3 changes: 3 additions & 0 deletions oscars/src/collectors/mark_sweep/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "std")]
extern crate std;

Comment on lines +1 to +3
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extern crate std; is gated on feature = "std", but this test module doesn’t use std anywhere and the crate already conditionally links std in src/lib.rs. Consider removing this line, or (if it’s needed to make cargo test work for a #![no_std] crate) gating it with cfg(test) instead so tests don’t depend on enabling the std feature explicitly.

Suggested change
#[cfg(feature = "std")]
extern crate std;

Copilot uses AI. Check for mistakes.
use crate::collectors::mark_sweep::MarkSweepGarbageCollector;
use crate::{Finalize, Trace};

Expand Down
Loading