@@ -58,6 +58,24 @@ impl SlotConstructor for PyMemoryView {
58
58
59
59
#[ pyimpl( with( Hashable , Comparable , AsBuffer , SlotConstructor ) ) ]
60
60
impl PyMemoryView {
61
+ #[ cfg( debug_assertions) ]
62
+ fn validate ( self ) -> Self {
63
+ let options = & self . buffer . options ;
64
+ let bytes_len = options. len * options. itemsize ;
65
+ let buffer_len = self . buffer . internal . obj_bytes ( ) . len ( ) ;
66
+ let t1 = self . range . len ( ) == bytes_len;
67
+ let t2 = buffer_len >= self . range . end ;
68
+ let t3 = buffer_len >= self . range . start + bytes_len;
69
+ assert ! ( t1) ;
70
+ assert ! ( t2) ;
71
+ assert ! ( t3) ;
72
+ self
73
+ }
74
+ #[ cfg( not( debug_assertions) ) ]
75
+ fn validate ( self ) -> Self {
76
+ self
77
+ }
78
+
61
79
fn parse_format ( format : & str , vm : & VirtualMachine ) -> PyResult < FormatSpec > {
62
80
FormatSpec :: parse ( format, vm)
63
81
}
@@ -77,7 +95,8 @@ impl PyMemoryView {
77
95
step : 1 ,
78
96
format_spec,
79
97
hash : OnceCell :: new ( ) ,
80
- } )
98
+ }
99
+ . validate ( ) )
81
100
}
82
101
83
102
pub fn from_buffer_range (
@@ -96,7 +115,8 @@ impl PyMemoryView {
96
115
step : 1 ,
97
116
format_spec,
98
117
hash : OnceCell :: new ( ) ,
99
- } )
118
+ }
119
+ . validate ( ) )
100
120
}
101
121
102
122
fn as_contiguous ( & self ) -> Option < BorrowedValue < [ u8 ] > > {
@@ -270,6 +290,7 @@ impl PyMemoryView {
270
290
format_spec,
271
291
hash : OnceCell :: new ( ) ,
272
292
}
293
+ . validate ( )
273
294
. into_object ( vm) ) ;
274
295
}
275
296
@@ -315,6 +336,7 @@ impl PyMemoryView {
315
336
format_spec,
316
337
hash : OnceCell :: new ( ) ,
317
338
}
339
+ . validate ( )
318
340
. into_object ( vm) ) ;
319
341
} ;
320
342
@@ -345,6 +367,7 @@ impl PyMemoryView {
345
367
format_spec,
346
368
hash : OnceCell :: new ( ) ,
347
369
}
370
+ . validate ( )
348
371
. into_object ( vm) )
349
372
}
350
373
@@ -538,6 +561,7 @@ impl PyMemoryView {
538
561
hash : OnceCell :: new ( ) ,
539
562
..* zelf
540
563
}
564
+ . validate ( )
541
565
. into_ref ( vm) )
542
566
}
543
567
@@ -607,6 +631,7 @@ impl PyMemoryView {
607
631
hash : OnceCell :: new ( ) ,
608
632
..* zelf
609
633
}
634
+ . validate ( )
610
635
. into_ref ( vm) )
611
636
}
612
637
0 commit comments