deb_f.f90
program debug
 
 ! Do not remove
 ! Benoit Leveugle, CINES - ASA
 ! This small program try to regroup most encountered bugs in HPC related work
 ! Use -cpp for preprocessing with gfortran and -fpp with ifort
 
! OMP_LIB will be used for stack overflow example
!$use OMP_LIB
 
 implicit none ! always use this, not using this is suicide
 
#ifdef hello
 ! hello world
 print *,"hello world !"
#endif
 
 
 ! ############################################################################################
 ! ## Floating point exceptions
 ! ##############################
 
#ifdef divzero 
! divided by zero, fpe
 
 real(8) :: d1,d2,d3
 
 d2 = 10.0d0
 d3 = 0.0d0
 d1 = d2 / d3
 print *,d1,d2,d3
 
#endif
 
#ifdef nan 
! generate a nan, fpe
 
 real(8) :: d1,d2
 
 d2 = 10.0d0
 d1 = dacos(d2)
 print *,d1,d2
 
#endif
 
#ifdef overflow 
! generate an overflow, same as underflow, fpe
 
 real(8) :: d1,d2
 
 d2 = 10d10
 d1 = dexp(d2)
 print *,d1,d2
 
#endif
 
 
 
 ! ##################################
 ! ## uninitialized
 ! ##############################
 
#ifdef uninitstatic
! use a non initialized value, static
 
 real(8) :: d1,d2
 
 d1 = d2*10.0d0
 print *,d1,d2
 
#endif
 
#ifdef uninitdynamic
! use a non initialized value, dynamic
 
 real(8), allocatable, dimension(:) :: d1,d2
 
 allocate(d1(1:10), d2(1:10))
 d1(3) = d2(4)*10.0d0
 print *,d1(3),d2(4)
 deallocate(d1)
 
#endif
 
#ifdef uninitalloc
! use a non initialized value, not allocated
 
 real(8), allocatable, dimension(:) :: d1,d2
 
 allocate(d1(1:10))
 d1(3) = d2(4)*10.0d0
 print *,d1(3),d2(4)
 deallocate(d1)
 
#endif
 
 
 
 ! ##################################
 ! ## allocations
 ! ##############################
 
#ifdef unalloc
! try do free an non allocated variable
 
 real(8), allocatable, dimension(:) :: d1,d2
 
 deallocate(d1)
 
#endif
 
#ifdef allocalloc
! try do allocate an already allocated variable
 
 real(8), allocatable, dimension(:) :: d1,d2
 
 allocate(d1(1:10))
 allocate(d1(1:100))
 deallocate(d1)
 
#endif
 
#ifdef nonfree
! detect not freed memory
 
 real(8), allocatable, dimension(:) :: d1,d2
 
 allocate(d1(1:10),d2(1:100))
 d2(:) = 100.0d0
 d1(3) = d2(4)*10.0d0
 print *,d1(3),d2(4)
 deallocate(d1)
 
#endif
 
 
 
 ! ##################################
 ! ## arrays
 ! ##############################
 
#ifdef outofbound
! out of array bounds, read or write
 
 real(8), allocatable, dimension(:) :: d1
 
 allocate(d1(1:10))
 d1(1:10) = 10.0d0
 print *,d1(:)
 print *,d1(10+1)
 deallocate(d1)
 
#endif
 
 
 
 ! ##################################
 ! ## io
 ! ##############################
 
#ifdef ionotopen
! read in not open files
! note that in fortran, writing or reading in a not open file result in writing or reading fort.10
 
 integer :: i1
 
 write(10,*) "Hello dude !" ! Will write in fort.10
 read(10) i1 ! Will try to read fort.10, but fail
 
#endif
 
 
 
 ! ##################################
 ! ## memory leak
 ! ##############################
 
#ifdef leak
! memory leak, not possible in fortran :(
real(8), dimension(1:1000) :: t1,t2
integer :: i
 
t1(:)=10.0d0
t2(:)=20.0d0
 
do i=1,10
 call swap(t1,t2)
 t1(:) = t1(:) + 10.0d0
end do
 
#endif
 
 
 ! ##################################
 ! ## stack overflow
 ! ##############################
 
#ifdef stackover
 real(8), dimension(100,100,100) :: U0,U1
 
 U0(:,:,:) = 1.0d0
 U1(:,:,:) = U0(:,:,:)
 
 !$OMP PARALLEL DEFAULT(PRIVATE)
 print *,"hello"
 !$OMP END PARALLEL
 
#endif
 
 
 ! ##################################
 ! ## buffer overflow
 ! ##############################
 
#ifdef bufferover
 
 character(len=10) :: X
 character(len=4) :: S
 X = 'ZZZZZZZZZZZ'
 call dump(X)
 !write(S,'(A)') X
 
#endif
 
 print *," "
 print *,"End"
 
end program debug
 
 
 
#ifdef leak
! memory leak, do not work in recent fortran
 
subroutine swap(a, b)
 real(8), dimension(1:1000), intent(inout) :: a, b
 real(8), allocatable :: work(:)
 allocate( work(size(a)) )
 work(:) = a(:)
 a = b
 b = work
 !deallocate( work ) !necessary
 
end subroutine swap
 
#endif
 
#ifdef bufferover
 
 subroutine dump(S)
 character(len=20) :: S
 S = 'AAAA'
 end subroutine
 
#endif