Skip to content

Commit 4ae58a7

Browse files
ArielG-NVcsyonghe
andauthored
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>
1 parent f114433 commit 4ae58a7

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

source/slang/slang-check-expr.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -3743,9 +3743,8 @@ namespace Slang
37433743
case 'w': case 'a': elementIndex = 3; break;
37443744
default:
37453745
// An invalid character in the swizzle is an error
3746-
getSink()->diagnose(swizExpr, Diagnostics::invalidSwizzleExpr, swizzleText, baseElementType->toString());
37473746
anyError = true;
3748-
continue;
3747+
break;
37493748
}
37503749

37513750
// TODO(tfoley): GLSL requires that all component names
@@ -3754,9 +3753,16 @@ namespace Slang
37543753
// Make sure the index is in range for the source type
37553754
if (elementIndex >= limitElement)
37563755
{
3757-
getSink()->diagnose(swizExpr, Diagnostics::invalidSwizzleExpr, swizzleText, baseElementType->toString());
37583756
anyError = true;
3759-
continue;
3757+
break;
3758+
}
3759+
3760+
// If elementCount is already at 4 stop trying to assign a swizzle element and send an error,
3761+
// we cannot have more valid swizzle elements than 4.
3762+
if (elementCount >= 4)
3763+
{
3764+
anyError = true;
3765+
break;
37603766
}
37613767

37623768
// Check if we've seen this index before
@@ -3778,6 +3784,7 @@ namespace Slang
37783784

37793785
if (anyError)
37803786
{
3787+
getSink()->diagnose(swizExpr, Diagnostics::invalidSwizzleExpr, swizzleText, baseElementType->toString());
37813788
return CreateErrorExpr(memberRefExpr);
37823789
}
37833790
else if (elementCount == 1)
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//TEST:SIMPLE(filecheck=CHECK): -target spirv -stage compute -entry computeMain -emit-spirv-directly
2+
// CHECK: error 30052
3+
// CHECK-NOT: error 30052
4+
RWStructuredBuffer<float> outputBuffer;
5+
6+
[numthreads(1,1,1)]
7+
void computeMain( uint2 dispatchThreadID : SV_DispatchThreadID )
8+
{
9+
float4 vecVal = float4(0);
10+
outputBuffer[0] = vecVal.xxtyxx;
11+
}

0 commit comments

Comments
 (0)