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 {
366366 type Output = Self ;
367367 #[ inline]
368368 fn add ( self , rhs : u64 ) -> Self :: Output {
369- VirtAddr :: new ( self . 0 + rhs)
369+ VirtAddr :: new ( self . 0 . checked_add ( rhs) . unwrap ( ) )
370370 }
371371}
372372
@@ -593,7 +593,7 @@ impl Add<u64> for PhysAddr {
593593 type Output = Self ;
594594 #[ inline]
595595 fn add ( self , rhs : u64 ) -> Self :: Output {
596- PhysAddr :: new ( self . 0 + rhs)
596+ PhysAddr :: new ( self . 0 . checked_add ( rhs) . unwrap ( ) )
597597 }
598598}
599599
@@ -663,6 +663,30 @@ pub const fn align_up(addr: u64, align: u64) -> u64 {
663663mod tests {
664664 use super :: * ;
665665
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+
666690 #[ test]
667691 pub fn virtaddr_new_truncate ( ) {
668692 assert_eq ! ( VirtAddr :: new_truncate( 0 ) , VirtAddr ( 0 ) ) ;
You can’t perform that action at this time.
0 commit comments