From 6c3b23fcf36f6fe0cb3823512b183b6962f99ee8 Mon Sep 17 00:00:00 2001 From: Brian Osman Date: Fri, 12 Feb 2021 13:09:58 -0500 Subject: [PATCH] SkSL: Ensure struct declarations contain at least one member Bug: skia:11314 Change-Id: I66476543462ae378a5bfb6cbd902dfa2f5fc45f5 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/369917 Commit-Queue: Brian Osman Reviewed-by: John Stiles --- gn/sksl_tests.gni | 1 + resources/sksl/errors/BinaryInvalidType.sksl | 1 + resources/sksl/errors/EmptyStruct.sksl | 1 + resources/sksl/errors/InvalidUnary.sksl | 2 +- src/sksl/SkSLParser.cpp | 5 +++++ tests/sksl/errors/BinaryInvalidType.glsl | 18 +++++++++--------- tests/sksl/errors/EmptyStruct.glsl | 4 ++++ 7 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 resources/sksl/errors/EmptyStruct.sksl create mode 100644 tests/sksl/errors/EmptyStruct.glsl diff --git a/gn/sksl_tests.gni b/gn/sksl_tests.gni index 03d4264b29..1ce0929205 100644 --- a/gn/sksl_tests.gni +++ b/gn/sksl_tests.gni @@ -96,6 +96,7 @@ sksl_error_tests = [ "/sksl/errors/DuplicateFunction.sksl", "/sksl/errors/DuplicateOutput.sksl", "/sksl/errors/DuplicateSymbol.sksl", + "/sksl/errors/EmptyStruct.sksl", "/sksl/errors/EnumValueMustBeConstInt.sksl", "/sksl/errors/ErrorsInDeadCode.sksl", "/sksl/errors/FloatRemainder.sksl", diff --git a/resources/sksl/errors/BinaryInvalidType.sksl b/resources/sksl/errors/BinaryInvalidType.sksl index d122e3f846..7f72417e0d 100644 --- a/resources/sksl/errors/BinaryInvalidType.sksl +++ b/resources/sksl/errors/BinaryInvalidType.sksl @@ -11,6 +11,7 @@ void functionBoth() { } struct S { + float x; } s; void structLeft() { diff --git a/resources/sksl/errors/EmptyStruct.sksl b/resources/sksl/errors/EmptyStruct.sksl new file mode 100644 index 0000000000..f14106cdf9 --- /dev/null +++ b/resources/sksl/errors/EmptyStruct.sksl @@ -0,0 +1 @@ +struct S { }; diff --git a/resources/sksl/errors/InvalidUnary.sksl b/resources/sksl/errors/InvalidUnary.sksl index 2cc15c57d7..186e1248ae 100644 --- a/resources/sksl/errors/InvalidUnary.sksl +++ b/resources/sksl/errors/InvalidUnary.sksl @@ -1,4 +1,4 @@ -struct Foo {} bar; +struct Foo { float x; } bar; void preincrement_matrix() { float4x4 x = float4x4(1); ++x; } void predecrement_vector() { float3 x = float3(1); --x; } diff --git a/src/sksl/SkSLParser.cpp b/src/sksl/SkSLParser.cpp index 219ff0de9b..7d4d02f27e 100644 --- a/src/sksl/SkSLParser.cpp +++ b/src/sksl/SkSLParser.cpp @@ -626,6 +626,11 @@ ASTNode::ID Parser::structDeclaration() { if (!this->expect(Token::Kind::TK_RBRACE, "'}'")) { return ASTNode::ID::Invalid(); } + if (fields.empty()) { + this->error(name.fOffset, + "struct '" + this->text(name) + "' must contain at least one field"); + return ASTNode::ID::Invalid(); + } std::unique_ptr newType = Type::MakeStructType(name.fOffset, this->text(name), fields); if (struct_is_too_deeply_nested(*newType, kMaxStructDepth)) { this->error(name.fOffset, "struct '" + this->text(name) + "' is too deeply nested"); diff --git a/tests/sksl/errors/BinaryInvalidType.glsl b/tests/sksl/errors/BinaryInvalidType.glsl index e6cdd93668..697b94dfe4 100644 --- a/tests/sksl/errors/BinaryInvalidType.glsl +++ b/tests/sksl/errors/BinaryInvalidType.glsl @@ -3,13 +3,13 @@ error: 2: type mismatch: '*' cannot operate on '', 'int' error: 6: type mismatch: '*' cannot operate on 'int', '' error: 10: type mismatch: '*' cannot operate on '', '' -error: 17: type mismatch: '*' cannot operate on 'S', 'int' -error: 21: type mismatch: '*' cannot operate on 'int', 'S' -error: 25: type mismatch: '*' cannot operate on 'S', 'S' -error: 31: type mismatch: '*' cannot operate on 'sampler2D', 'int' -error: 35: type mismatch: '*' cannot operate on 'int', 'sampler2D' -error: 39: type mismatch: '*' cannot operate on 'sampler2D', 'sampler2D' -error: 45: type mismatch: '*' cannot operate on 'int[1]', 'int' -error: 49: type mismatch: '*' cannot operate on 'int', 'int[1]' -error: 53: type mismatch: '*' cannot operate on 'int[1]', 'int[1]' +error: 18: type mismatch: '*' cannot operate on 'S', 'int' +error: 22: type mismatch: '*' cannot operate on 'int', 'S' +error: 26: type mismatch: '*' cannot operate on 'S', 'S' +error: 32: type mismatch: '*' cannot operate on 'sampler2D', 'int' +error: 36: type mismatch: '*' cannot operate on 'int', 'sampler2D' +error: 40: type mismatch: '*' cannot operate on 'sampler2D', 'sampler2D' +error: 46: type mismatch: '*' cannot operate on 'int[1]', 'int' +error: 50: type mismatch: '*' cannot operate on 'int', 'int[1]' +error: 54: type mismatch: '*' cannot operate on 'int[1]', 'int[1]' 12 errors diff --git a/tests/sksl/errors/EmptyStruct.glsl b/tests/sksl/errors/EmptyStruct.glsl new file mode 100644 index 0000000000..b1c6d773eb --- /dev/null +++ b/tests/sksl/errors/EmptyStruct.glsl @@ -0,0 +1,4 @@ +### Compilation failed: + +error: 1: struct 'S' must contain at least one field +1 error