@@ -274,6 +274,19 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
274
274
size : Size ,
275
275
align : Align ,
276
276
) -> InterpResult < ' tcx , Option < Pointer < M :: PointerTag > > > {
277
+ fn check_offset_align ( offset : u64 , align : Align ) -> InterpResult < ' static > {
278
+ if offset % align. bytes ( ) == 0 {
279
+ Ok ( ( ) )
280
+ } else {
281
+ // The biggest power of two through which `offset` is divisible.
282
+ let offset_pow2 = 1 << offset. trailing_zeros ( ) ;
283
+ err ! ( AlignmentCheckFailed {
284
+ has: Align :: from_bytes( offset_pow2) . unwrap( ) ,
285
+ required: align,
286
+ } )
287
+ }
288
+ }
289
+
277
290
// Normalize to a `Pointer` if we definitely need one.
278
291
let normalized = if size. bytes ( ) == 0 {
279
292
// Can be an integer, just take what we got.
@@ -290,14 +303,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
290
303
if bits == 0 {
291
304
return err ! ( InvalidNullPointerUsage ) ;
292
305
}
293
- if bits % align. bytes ( ) != 0 {
294
- // The biggest power of two through which `bits` is divisible.
295
- let bits_pow2 = 1 << bits. trailing_zeros ( ) ;
296
- return err ! ( AlignmentCheckFailed {
297
- has: Align :: from_bytes( bits_pow2) . unwrap( ) ,
298
- required: align,
299
- } ) ;
300
- }
306
+ check_offset_align ( bits, align) ?;
301
307
None
302
308
}
303
309
Err ( ptr) => {
@@ -321,15 +327,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
321
327
required: align,
322
328
} ) ;
323
329
}
324
- let offset = ptr. offset . bytes ( ) ;
325
- if offset % align. bytes ( ) != 0 {
326
- // The biggest power of two through which `offset` is divisible.
327
- let bits_pow2 = 1 << offset. trailing_zeros ( ) ;
328
- return err ! ( AlignmentCheckFailed {
329
- has: Align :: from_bytes( bits_pow2) . unwrap( ) ,
330
- required: align,
331
- } )
332
- }
330
+ check_offset_align ( ptr. offset . bytes ( ) , align) ?;
333
331
334
332
// We can still be zero-sized in this branch, in which case we have to
335
333
// return `None`.
0 commit comments