Issue 240196
Summary [flang] ALLOCATE with STAT= reports success when the allocation size overflows
Labels flang
Assignees
Reporter hmenke
    When the size of an allocation overflows `std::size_t`, `ALLOCATE` returns `stat=0` and the array is reported as allocated with the requested extent, but only one byte is actually allocated. Any subsequent access corrupts the heap.

```fortran
program flang_overflow_success
  use iso_fortran_env, only: int64, real64
  implicit none
  real(real64), allocatable :: a(:)
 integer(int64) :: n
  integer :: stat
  n = 2_int64**62
  allocate(a(n), stat=stat)
  print '(a,i0,a,l1)', 'stat=', stat, ' allocated=', allocated(a)
  if (allocated(a)) print '(a,i0)', 'size=', size(a, kind=int64)
end program flang_overflow_success
```

https://godbolt.org/z/8nasjWPvY

The calculation of `elementBytes` and `byteSize` in `Descriptor::Allocate` both have no overflow check, so it will just wrap around and allocate something completely different.

https://github.com/llvm/llvm-project/blob/932d5ec166f3ee3a09ceb06eccb4ad6e164ce1c4/flang-rt/lib/runtime/descriptor.cpp#L168-L196
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to