@@ -274,6 +274,19 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
274274 size : Size ,
275275 align : Align ,
276276 ) -> 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+
277290 // Normalize to a `Pointer` if we definitely need one.
278291 let normalized = if size. bytes ( ) == 0 {
279292 // Can be an integer, just take what we got.
@@ -290,14 +303,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
290303 if bits == 0 {
291304 return err ! ( InvalidNullPointerUsage ) ;
292305 }
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) ?;
301307 None
302308 }
303309 Err ( ptr) => {
@@ -321,15 +327,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
321327 required: align,
322328 } ) ;
323329 }
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) ?;
333331
334332 // We can still be zero-sized in this branch, in which case we have to
335333 // return `None`.
0 commit comments