]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
parser: Fix statement error recovery
authorSergey Bugaev <bugaevc@gmail.com>
Sat, 24 May 2025 10:22:55 +0000 (13:22 +0300)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 11 Jun 2025 10:27:46 +0000 (12:27 +0200)
Parser.rollback () can only be used to move *backward* through tokens,
not forward. When we run into a ParseError while parsing a statement,
'begin' points to the start of the statement, and 'e_begin' to the error
location inside the statement. 'rollback (begin)' is then justified,
since we're rolling back to the start of the statement; but moving to
'e_begin' again can not be done with 'rollback (e_begin)'.

Because we failed to seek forward, an syntax error could send us into an
infinite loop. Use Parser.jump () instead, which moves forward through
tokens.

Regession of f5934184d050d1a19f394fdab6f2ee66ff30965f

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

tests/Makefile.am
tests/parser/try-catch-format-string.test [new file with mode: 0644]
vala/valaparser.vala

index ce7f030f1a93b3094d63a76ad6b2d9e379871b38..e8e9072e89ddfb774ee0d08f1edc9a24facb4242 100644 (file)
@@ -1073,6 +1073,7 @@ TESTS = \
        parser/switch-section-missing-colon.test \
        parser/switch-section-outside-switch.test \
        parser/template.vala \
+       parser/try-catch-format-string.test \
        parser/try-catch-in-switch-case-invalid.test \
        parser/tuple.vala \
        parser/unsupported-property-async.test \
diff --git a/tests/parser/try-catch-format-string.test b/tests/parser/try-catch-format-string.test
new file mode 100644 (file)
index 0000000..5bf0873
--- /dev/null
@@ -0,0 +1,10 @@
+Invalid Code
+
+void main () {
+       try {
+               this.connection = new Gda.Connection.from_string ("", @"DB_DIR=$db_path;DB_NAME=revala", null, Gda.ConnectionOptions.NONE);
+       } catch (GLib.Error as error) {
+               stdout.printf("Error creating the connection to the database");
+               throw error;
+       }
+}
index 0f2b1004dcc9ff4b64d58b3d92fa3d3a706b61f0..656e256872be1d0111d54464e09a641a748d86b5 100644 (file)
@@ -1738,7 +1738,7 @@ public class Vala.Parser : CodeVisitor {
                        string token = ((EnumClass) typeof (TokenType).class_ref ()).get_value (type).value_nick;
                        rollback (begin);
                        if (!is_expression ()) {
-                               rollback (e_begin);
+                               jump (e_begin);
                                throw e;
                        }
                        try {
@@ -1746,10 +1746,11 @@ public class Vala.Parser : CodeVisitor {
                                Report.warning (get_src (begin), "`%s' is a syntax keyword, replace with `@%s'", token, token);
                        } catch (ParseError e2) {
                                var e2_begin = get_location ();
-                               rollback (e_begin);
+                               jump (e_begin);
                                next ();
                                Report.error (get_src (e_begin), "Possible `%s-statement' syntax error, %s", token, e.message);
-                               rollback (e2_begin);
+                               rollback (begin);
+                               jump (e2_begin);
                                throw e2;
                        }
                }