From ec79d7049548b1fc667815599f45ab07bef8b47e Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Sat, 24 May 2025 13:22:55 +0300 Subject: [PATCH] parser: Fix statement error recovery 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 | 1 + tests/parser/try-catch-format-string.test | 10 ++++++++++ vala/valaparser.vala | 7 ++++--- 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 tests/parser/try-catch-format-string.test diff --git a/tests/Makefile.am b/tests/Makefile.am index ce7f030f1..e8e9072e8 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 000000000..5bf087315 --- /dev/null +++ b/tests/parser/try-catch-format-string.test @@ -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; + } +} diff --git a/vala/valaparser.vala b/vala/valaparser.vala index 0f2b1004d..656e25687 100644 --- a/vala/valaparser.vala +++ b/vala/valaparser.vala @@ -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; } } -- 2.47.2