[vala/0.54] vala: Make try-statement parsing more resilient



commit 943b5d3f393161afbf0c52e54007775eabd6b173
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Mon Aug 15 08:36:49 2022 +0200

    vala: Make try-statement parsing more resilient
    
    Regression of f5934184d050d1a19f394fdab6f2ee66ff30965f
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1304

 tests/Makefile.am                                  |  1 +
 tests/parser/try-catch-in-switch-case-invalid.test | 16 ++++++++++++++++
 vala/valaparser.vala                               |  4 +++-
 3 files changed, 20 insertions(+), 1 deletion(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3733c0706..908c021a0 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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
index 000000000..1d67e569c
--- /dev/null
+++ b/tests/parser/try-catch-in-switch-case-invalid.test
@@ -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;
+       }
+}
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 34418afe7..aa90e6c50 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -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) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]