Skip to content

Instantly share code, notes, and snippets.

@dstuebe
Created December 12, 2013 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstuebe/7936460 to your computer and use it in GitHub Desktop.
Save dstuebe/7936460 to your computer and use it in GitHub Desktop.
Test case for weird heap corruption / free error. Depending on the index which overruns the bounds the program fails on deallocate.
!****************************************************************************
!
! PROGRAM: hello
!
! PURPOSE: Test program for strange Heap Corruption error in windows
!
!****************************************************************************
program hello
implicit none
integer :: ierr, i,j
real, allocatable :: bar(:,:)
! Body of hello
print *, 'Hello World'
print *, 'Allocated? ', allocated(bar)
allocate(bar(3,3), stat=ierr)
print *, 'bar Allocation result:', ierr, allocated(bar)
do j=1,4
do i=1,3
bar(i,j) = 33.3 ! Causes Heap Corruption on Deallocate!
!bar(j,i) = 33.3 ! No Error or Exception in Default Release Build
end do
end do
print *, 'Set values - Yikes!'
! Nicely formatted output...
!do j = 1,4
! WRITE(*,220) j,(bar(i,j),i=1,3)
!end do
!220 format(i5,2x,<3>f8.1)
print *, 'Allocated? ', allocated(bar)
deallocate(bar, stat=ierr)
print *, 'Goodbye World', ierr, allocated(bar)
end program hello
@dstuebe
Copy link
Author

dstuebe commented Dec 12, 2013

In Microsoft Visual Studio (Visual Fortran) I get:
Unhandled exception at 0x775de753 (ntdll.dll) in hello.exe: 0xC0000374: A heap has been corrupted.

I am using: Intel(R) Visual Fortran Compiler XE on IA-32, version 12.1.5 Package ID: w_fcompxe_2011.11.344

@brianmckenna
Copy link

[brianmckenna@mulder Downloads]$ gfortran --version
GNU Fortran (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4)
[brianmckenna@mulder Downloads]$ /lib64/libc.so.6 
GNU C Library stable release version 2.12, by Roland McGrath et al.
[brianmckenna@mulder Downloads]$ gfortran -o hello hello.f90 
[brianmckenna@mulder Downloads]$ ./hello 
 Hello World
 Allocated?  F
 bar Allocation result:           0 T
 Set values - Yikes!
 Allocated?  T
*** glibc detected *** ./hello: free(): invalid next size (fast): 0x0000000000fbcb10 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x76126)[0x7f8094353126]
/lib64/libc.so.6(+0x78c53)[0x7f8094355c53]
./hello[0x400c38]
./hello[0x400d5a]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x7f80942fbcdd]
./hello[0x4007a9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:03 787114                             /home/brianmckenna/Downloads/hello
00601000-00602000 rw-p 00001000 08:03 787114                             /home/brianmckenna/Downloads/hello
00fbc000-00fdd000 rw-p 00000000 00:00 0                                  [heap]
7f80942dd000-7f8094467000 r-xp 00000000 08:03 4456461                    /lib64/libc-2.12.so
7f8094467000-7f8094666000 ---p 0018a000 08:03 4456461                    /lib64/libc-2.12.so
7f8094666000-7f809466a000 r--p 00189000 08:03 4456461                    /lib64/libc-2.12.so
7f809466a000-7f809466b000 rw-p 0018d000 08:03 4456461                    /lib64/libc-2.12.so
7f809466b000-7f8094670000 rw-p 00000000 00:00 0 
7f8094670000-7f8094686000 r-xp 00000000 08:03 4456454                    /lib64/libgcc_s-4.4.7-20120601.so.1
7f8094686000-7f8094885000 ---p 00016000 08:03 4456454                    /lib64/libgcc_s-4.4.7-20120601.so.1
7f8094885000-7f8094886000 rw-p 00015000 08:03 4456454                    /lib64/libgcc_s-4.4.7-20120601.so.1
7f8094886000-7f8094909000 r-xp 00000000 08:03 4456469                    /lib64/libm-2.12.so
7f8094909000-7f8094b08000 ---p 00083000 08:03 4456469                    /lib64/libm-2.12.so
7f8094b08000-7f8094b09000 r--p 00082000 08:03 4456469                    /lib64/libm-2.12.so
7f8094b09000-7f8094b0a000 rw-p 00083000 08:03 4456469                    /lib64/libm-2.12.so
7f8094b0a000-7f8094bfa000 r-xp 00000000 08:03 22810834                   /usr/lib64/libgfortran.so.3.0.0
7f8094bfa000-7f8094df9000 ---p 000f0000 08:03 22810834                   /usr/lib64/libgfortran.so.3.0.0
7f8094df9000-7f8094dfb000 rw-p 000ef000 08:03 22810834                   /usr/lib64/libgfortran.so.3.0.0
7f8094dfb000-7f8094dfc000 rw-p 00000000 00:00 0 
7f8094dfc000-7f8094e1c000 r-xp 00000000 08:03 4456850                    /lib64/ld-2.12.so
7f8095000000-7f8095004000 rw-p 00000000 00:00 0 
7f8095019000-7f809501b000 rw-p 00000000 00:00 0 
7f809501b000-7f809501c000 r--p 0001f000 08:03 4456850                    /lib64/ld-2.12.so
7f809501c000-7f809501d000 rw-p 00020000 08:03 4456850                    /lib64/ld-2.12.so
7f809501d000-7f809501e000 rw-p 00000000 00:00 0 
7fff852e9000-7fff852fe000 rw-p 00000000 00:00 0                          [stack]
7fff85307000-7fff85308000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment