<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/111404>111404</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Flang] flang-new-20 Segmentation fault with open(,status=old) of non existing file when setting MALLOC_PERTURB
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          edoapra
      </td>
    </tr>
</table>

<pre>
    When compiling the code below with the current flang-new-20 and executing with `MALLOC_PERTURB_=$(($RANDOM % 255 + 1))` and with the file to be opened being not available, the code stops with a segmentation fault.  
flang-new-18 and flang-new-19 are not affected by this issue.
Code
```
 program test
      implicit none
      logical status
 open(unit=10,file='./nwchemrc',
     +     status='old',err=9999)
 write(6,*) ' open ok'
      stop
 9999 continue
      write(6,*) ' open failed'
      end
```
gdb stack
```
Program received signal SIGSEGV, Segmentation fault.
0x000055555557a076 in Fortran::runtime::io::ExternalFileUnit::EndIoStatement() ()
(gdb) where
#0  0x000055555557a076 in Fortran::runtime::io::ExternalFileUnit::EndIoStatement() ()
#1 0x000055555556f52d in Fortran::runtime::io::IoStatementState::EndIoStatement() ()
#2 0x00005555555566d4 in _QQmain ()
#3  0x0000555555556772 in main ()
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8VE2P2zgM_TXKhZhApr8PPqTJpCjQbtuZ7e5xIFu0ra0sBbI8mf77heR0PotFTysIsEWR75E0_cQ8q8EQNSx_x_LDRix-tK4hacXJiU1r5Y_m75EMdHY6Ka3MAH4k6KwkaEnbM5yVH1fb4hwZD70WZrgydL5CDsJIoAfqFh9Coy8r-Kfdx4-f93dfrm_-_Hbz7o6lB4YZwyru7Gb3x-HzJ2CYA-Y5MHwHCcM67GJFfOTslSbwFloCeyJDEloKRMZ6EPdCadFqYrh_Snr29jSvAAJmGiYyXnhlDfRi0X4LwPiB8d1TFUkVOZ8ZahCOVo6-p84H2h_gRzWDmueFtivE3kpa31jBLzse4eTs4MQEnmZ_McWlppNWnfJgrKHnF9oOqhMaZi_8Ml9uQsUMq8Uoz9JDwhnuQz9iN8stw6M5dyNNrmNYMtw_wwstDesCFwOslqsfOcfSQ13XseNr0NkpTwyrIuDgjmENDMuYAdjvIe5ZsqHFl3NAgc4ar8zyoqD_AuyF0iRfgZKRv-zlINtQRvf9l7dfLo121JG6Jwlh2oWG2w_vb6_f_xUm4_btDKyx_IFzzvN1lYKXBSgDR-u8E4alO5bu3GK8mmg9KLs-rx88OSP0UWn6Fr9NtBr5wd564SmwxUEPJVePPWZYDbIN1vNI7ufgYMoB_vdM0uQlZ9HnKH-P8xl0fPl9TnzJmReFzALn3devk1DmtXsKr_3LEoP_W-efM7GRTSrrtBYbapISqzSp6yTbjI3M20RmVCRdX4k2TfI6rdK-a1vMeZ2XuFENcswSzsukyJM828qEI-UyS7ESWJJkGadJKL3V-n7aWjdsohI0SZJkPNto0ZKeo8giRiVhiEFvXRMCrtplmFnGtZr9_AThlddRmY8xIj-81Na3k7vq2kUXGO4f_-_4c9dg-6AsQA9qjnoc5fMc5H0mHy0vlXmzON2M3p_m8BnxyPA4KD8u7bazE8NjyPTyuDo5-w91nuExVj4zPF6Kv2_w3wAAAP__s5zinw">