]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Make try-statement parsing more resilient
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 15 Aug 2022 06:36:49 +0000 (08:36 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 15 Aug 2022 13:13:17 +0000 (15:13 +0200)
Regression of f5934184d050d1a19f394fdab6f2ee66ff30965f

Fixes https://gitlab.gnome.org/GNOME/vala/issues/1304

tests/Makefile.am
tests/parser/try-catch-in-switch-case-invalid.test [new file with mode: 0644]
vala/valaparser.vala

index 3733c0706f7b83348ef83ce818a8c22e2d4cc5e1..908c021a02f90aa8b15687bd42b2e11dd0f59307 100644 (file)
@@ -963,6 +963,7 @@ TESTS = \
        parser/switch-statement.vala \
        parser/switch-section-outside-switch.test \
        parser/template.vala \
+       parser/try-catch-in-switch-case-invalid.test \
        parser/tuple.vala \
        parser/unsupported-property-async.test \
        parser/unsupported-property-throws.test \
diff --git a/tests/parser/try-catch-in-switch-case-invalid.test b/tests/parser/try-catch-in-switch-case-invalid.test
new file mode 100644 (file)
index 0000000..1d67e56
--- /dev/null
@@ -0,0 +1,16 @@
+Invalid Code
+
+void main () {
+       switch (42) {
+       case 42:
+               try {
+                       GLib.print ("42");
+                catch (GLib.Error e) {
+                       debug ("foo");
+               }
+               break;
+       default:
+               debug ("bar");
+               break;
+       }
+}
index 34418afe710eb24968c6111a62fdc28a0b5475ad..aa90e6c503dd83ca6930a85d286c09569a0a2150 100644 (file)
@@ -2345,8 +2345,10 @@ public class Vala.Parser : CodeVisitor {
                        if (current () == TokenType.FINALLY) {
                                finally_clause = parse_finally_clause ();
                        }
-               } else {
+               } else if (current () == TokenType.FINALLY) {
                        finally_clause = parse_finally_clause ();
+               } else {
+                       report_parse_error (new ParseError.SYNTAX ("expected `catch' or `finally'"));
                }
                var stmt = new TryStatement (try_block, finally_clause, get_src (begin));
                foreach (CatchClause clause in catch_clauses) {