File tree Expand file tree Collapse file tree 1 file changed +26
-2
lines changed Expand file tree Collapse file tree 1 file changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -366,7 +366,7 @@ impl Add<u64> for VirtAddr {
366
366
type Output = Self ;
367
367
#[ inline]
368
368
fn add ( self , rhs : u64 ) -> Self :: Output {
369
- VirtAddr :: new ( self . 0 + rhs)
369
+ VirtAddr :: new ( self . 0 . checked_add ( rhs) . unwrap ( ) )
370
370
}
371
371
}
372
372
@@ -593,7 +593,7 @@ impl Add<u64> for PhysAddr {
593
593
type Output = Self ;
594
594
#[ inline]
595
595
fn add ( self , rhs : u64 ) -> Self :: Output {
596
- PhysAddr :: new ( self . 0 + rhs)
596
+ PhysAddr :: new ( self . 0 . checked_add ( rhs) . unwrap ( ) )
597
597
}
598
598
}
599
599
@@ -663,6 +663,30 @@ pub const fn align_up(addr: u64, align: u64) -> u64 {
663
663
mod tests {
664
664
use super :: * ;
665
665
666
+ #[ test]
667
+ #[ should_panic]
668
+ pub fn add_overflow_virtaddr ( ) {
669
+ let _ = VirtAddr :: new ( 0xffff_ffff_ffff_ffff ) + 1 ;
670
+ }
671
+
672
+ #[ test]
673
+ #[ should_panic]
674
+ pub fn add_overflow_physaddr ( ) {
675
+ let _ = PhysAddr :: new ( 0x000f_ffff_ffff_ffff ) + 0xffff_0000_0000_0000 ;
676
+ }
677
+
678
+ #[ test]
679
+ #[ should_panic]
680
+ pub fn sub_underflow_virtaddr ( ) {
681
+ let _ = VirtAddr :: new ( 0 ) - 1 ;
682
+ }
683
+
684
+ #[ test]
685
+ #[ should_panic]
686
+ pub fn sub_overflow_physaddr ( ) {
687
+ let _ = PhysAddr :: new ( 0 ) - 1 ;
688
+ }
689
+
666
690
#[ test]
667
691
pub fn virtaddr_new_truncate ( ) {
668
692
assert_eq ! ( VirtAddr :: new_truncate( 0 ) , VirtAddr ( 0 ) ) ;
You can’t perform that action at this time.
0 commit comments