]> 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 07:23:00 +0000 (09:23 +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 f5e0cb8ffb296f685c1561335571fd5b769bfcdc..147c66676078d3288c4c19460520fab87a5de928 100644 (file)
@@ -994,6 +994,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 cfa0776aeda6199c0567add8b40fcaaea9928805..f703e9c86749003ac6dec01601e8d5b90dc35e42 100644 (file)
@@ -2473,8 +2473,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) {