From 11e1318c3ed77aa08326e16eaf52598ad6430546 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Mon, 25 Mar 2024 18:27:07 -0700 Subject: [PATCH] [Sema] Check FunctionDecl has identifier before getName. (#6439) (#6457) Use identifier name without check the identifier exists will cause crash. Fixes #6426 --------- Co-authored-by: Tex Riddell Co-authored-by: github-actions[bot] (cherry picked from commit 7581ff4d297d124b340d6715b709f1c423909ca5) --- tools/clang/lib/Sema/SemaHLSL.cpp | 3 +- .../test/SemaHLSL/operator-overload.hlsl | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tools/clang/test/SemaHLSL/operator-overload.hlsl diff --git a/tools/clang/lib/Sema/SemaHLSL.cpp b/tools/clang/lib/Sema/SemaHLSL.cpp index 302159e952..871fa025e5 100644 --- a/tools/clang/lib/Sema/SemaHLSL.cpp +++ b/tools/clang/lib/Sema/SemaHLSL.cpp @@ -15690,7 +15690,8 @@ void TryAddShaderAttrFromTargetProfile(Sema &S, FunctionDecl *FD, // if this FD isn't the entry point, then we shouldn't add // a shader attribute to this decl, so just return - if (EntryPointName != FD->getIdentifier()->getName()) { + if (!FD->getIdentifier() || + EntryPointName != FD->getIdentifier()->getName()) { return; } diff --git a/tools/clang/test/SemaHLSL/operator-overload.hlsl b/tools/clang/test/SemaHLSL/operator-overload.hlsl new file mode 100644 index 0000000000..9b33b96b92 --- /dev/null +++ b/tools/clang/test/SemaHLSL/operator-overload.hlsl @@ -0,0 +1,32 @@ +// RUN: %dxc -Tps_6_0 -verify %s + +// expected-error@+1 {{overloading non-member 'operator==' is not allowed}} +bool operator==(int lhs, int rhs) { + return true; +} + +struct A { + float a; + int b; +}; + +// expected-error@+1 {{overloading non-member 'operator>' is not allowed}} +bool operator>(A a0, A a1) { + return a1.a > a0.a && a1.b > a0.b; +} +// expected-error@+1 {{overloading non-member 'operator==' is not allowed}} +bool operator==(A a0, int i) { + return a0.b == i; +} +// expected-error@+1 {{overloading non-member 'operator<' is not allowed}} +bool operator<(A a0, float f) { + return a0.a < f; +} +// expected-error@+1 {{overloading 'operator++' is not allowed}} +A operator++(A a0) { + a0.a++; + a0.b++; + return a0; +} + +void main() {} \ No newline at end of file