diff options
| author | ArielG-NV <159081215+ArielG-NV@users.noreply.github.com> | 2024-07-19 14:52:25 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-19 11:52:25 -0700 |
| commit | 4ae58a720e6f8d63ff5c74fd14f630137dfd0c0c (patch) | |
| tree | 25d3e4813d0cbd25090cef9870b36f3c6930483a | |
| parent | f114433debfba67cbe1db239b6e92278d41ed438 (diff) | |
Fix for invalid swizzle causing crash (#4690)
* Fix for invalid swizzle causing crash
Fixes #4689
If swizzle code is provided 5+ element swizzle the checkSwizzleExpr code will do an out of bounds array access and crash.
* switch test to check for to ensure no crash
* cleanup swizzle errors to only emit once
---------
Co-authored-by: Yong He <yonghe@outlook.com>
| -rw-r--r-- | source/slang/slang-check-expr.cpp | 15 | ||||
| -rw-r--r-- | tests/bugs/invalid-swizzle-count.slang | 11 |
2 files changed, 22 insertions, 4 deletions
diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index f60ead1e9..fd03e5e87 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -3743,9 +3743,8 @@ namespace Slang case 'w': case 'a': elementIndex = 3; break; default: // An invalid character in the swizzle is an error - getSink()->diagnose(swizExpr, Diagnostics::invalidSwizzleExpr, swizzleText, baseElementType->toString()); anyError = true; - continue; + break; } // TODO(tfoley): GLSL requires that all component names @@ -3754,9 +3753,16 @@ namespace Slang // Make sure the index is in range for the source type if (elementIndex >= limitElement) { - getSink()->diagnose(swizExpr, Diagnostics::invalidSwizzleExpr, swizzleText, baseElementType->toString()); anyError = true; - continue; + break; + } + + // If elementCount is already at 4 stop trying to assign a swizzle element and send an error, + // we cannot have more valid swizzle elements than 4. + if (elementCount >= 4) + { + anyError = true; + break; } // Check if we've seen this index before @@ -3778,6 +3784,7 @@ namespace Slang if (anyError) { + getSink()->diagnose(swizExpr, Diagnostics::invalidSwizzleExpr, swizzleText, baseElementType->toString()); return CreateErrorExpr(memberRefExpr); } else if (elementCount == 1) diff --git a/tests/bugs/invalid-swizzle-count.slang b/tests/bugs/invalid-swizzle-count.slang new file mode 100644 index 000000000..811cf6f44 --- /dev/null +++ b/tests/bugs/invalid-swizzle-count.slang @@ -0,0 +1,11 @@ +//TEST:SIMPLE(filecheck=CHECK): -target spirv -stage compute -entry computeMain -emit-spirv-directly +// CHECK: error 30052 +// CHECK-NOT: error 30052 +RWStructuredBuffer<float> outputBuffer; + +[numthreads(1,1,1)] +void computeMain( uint2 dispatchThreadID : SV_DispatchThreadID ) +{ + float4 vecVal = float4(0); + outputBuffer[0] = vecVal.xxtyxx; +} |
