File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -1168,6 +1168,21 @@ impl<A: Allocator> Dlmalloc<A> {
1168
1168
}
1169
1169
}
1170
1170
1171
+ pub unsafe fn validate_size ( & mut self , ptr : * mut u8 , size : usize ) {
1172
+ let p = Chunk :: from_mem ( ptr) ;
1173
+ let psize = Chunk :: size ( p) ;
1174
+
1175
+ let min_overhead = self . overhead_for ( p) ;
1176
+ assert ! ( psize >= size + min_overhead) ;
1177
+
1178
+ if !Chunk :: mmapped ( p) {
1179
+ let max_overhead =
1180
+ min_overhead + self . min_chunk_size ( ) * 2 + mem:: align_of :: < usize > ( ) - 1 ;
1181
+
1182
+ assert ! ( psize <= size + max_overhead) ;
1183
+ }
1184
+ }
1185
+
1171
1186
pub unsafe fn free ( & mut self , mem : * mut u8 ) {
1172
1187
self . check_malloc_state ( ) ;
1173
1188
Original file line number Diff line number Diff line change @@ -141,7 +141,8 @@ impl<A: Allocator> Dlmalloc<A> {
141
141
/// method contracts.
142
142
#[ inline]
143
143
pub unsafe fn free ( & mut self , ptr : * mut u8 , size : usize , align : usize ) {
144
- let _ = ( size, align) ;
144
+ let _ = align;
145
+ self . 0 . validate_size ( ptr, size) ;
145
146
self . 0 . free ( ptr)
146
147
}
147
148
@@ -162,6 +163,8 @@ impl<A: Allocator> Dlmalloc<A> {
162
163
old_align : usize ,
163
164
new_size : usize ,
164
165
) -> * mut u8 {
166
+ self . 0 . validate_size ( ptr, old_size) ;
167
+
165
168
if old_align <= self . 0 . malloc_alignment ( ) {
166
169
self . 0 . realloc ( ptr, new_size)
167
170
} else {
You can’t perform that action at this time.
0 commit comments