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

    <tr>
        <th>Summary</th>
        <td>
            clang-cl incorrect SEH exception handling
        </td>
    </tr>

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

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

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

<pre>
    The following code is supposed to read from potentially inaccessible memory, and return the provided default value if the memory is not accessible. It works fine when compiled with with `cl` (outputs `except` and `done`), but crashes when compiled with `clang-cl`:
```
#include <stdio.h>
#include <windows.h>

//#define WORKAROUND

int SafeFetch32(const int* adr, int errValue) {
  int v = 0;
  __try {
#ifdef WORKAROUND
         printf("");
#endif
    v = *adr;
#ifdef WORKAROUND
         printf("");
#endif
  }
  __except(EXCEPTION_EXECUTE_HANDLER) {
        printf("except\n");
    v = errValue;
  }
  return v;
}
int main() {
        SafeFetch32(0, -1);
        printf("done");
        return 0;
}
```
When compiled with `-DWORKAROUND`, the code generated by `clang-cl` works as expected.

Clang downloaded with MS Visual Studio 2022 Community:
```
>clang-cl --version
clang version 15.0.1
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\Llvm\x64\bin
```
Not sure if that's relevant, but the results were collected on Windows 11 21H2.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVVFv4joT_TXmZQRynCaQhzywAbSrb7ddtd123yonnhDvOnZkO1D-_ScnaYHe3rcrWS34jGfO8Zkx3Dm514g5Sb6QZDPjvW-MzcUfVFK7v3JWGnHKHxuE2ihljlLvoTICQTpwfdcZhwK8AYtcQG1NC53xqL3kSp1Aal5V6JwsFUKLrbEnwgrgWoBF31sNvkHorDlIgQIE1rxXHg5c9QiyHtDxWKinjYdzvgV883A09q-DWmqEY4MaKtN2UqGAo_TN-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