Skip to content

clang-cl incorrect SEH exception handling #62606

Open
@djelinski

Description

@djelinski

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions