]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
parser: Better handling of misplaced switch sections
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 2 Nov 2021 12:22:00 +0000 (13:22 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 14 Nov 2021 11:40:18 +0000 (12:40 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1246

tests/Makefile.am
tests/parser/switch-section-outside-switch.test [new file with mode: 0644]
vala/valaparser.vala
vala/valaswitchsection.vala

index 33cccdc7c0c6a23358275ea7f995ddd5b60065ab..ed3156efe85128dd7a291a3760e487a6b4f789ee 100644 (file)
@@ -909,6 +909,7 @@ TESTS = \
        parser/statement-keyword-as-identifier.vala \
        parser/statement-outside-root.test \
        parser/switch-statement.vala \
+       parser/switch-section-outside-switch.test \
        parser/template.vala \
        parser/tuple.vala \
        parser/unsupported-property-async.test \
diff --git a/tests/parser/switch-section-outside-switch.test b/tests/parser/switch-section-outside-switch.test
new file mode 100644 (file)
index 0000000..8a888d8
--- /dev/null
@@ -0,0 +1,5 @@
+Invalid Code
+
+void main () {
+       case 23:
+}
index 630c74de41749a1ae72f6c3daadce09e454aac3f..150678e062cb3275c696efc948123012213b9b7e 100644 (file)
@@ -1608,6 +1608,10 @@ public class Vala.Parser : CodeVisitor {
                        case TokenType.SWITCH:
                                stmt = parse_switch_statement ();
                                break;
+                       case TokenType.CASE:
+                       case TokenType.DEFAULT:
+                               stmt = parse_switch_section_statement ();
+                               break;
                        case TokenType.WHILE:
                                stmt = parse_while_statement ();
                                break;
@@ -1674,8 +1678,6 @@ public class Vala.Parser : CodeVisitor {
 
        void parse_statements (Block block) throws ParseError {
                while (current () != TokenType.CLOSE_BRACE
-                      && current () != TokenType.CASE
-                      && current () != TokenType.DEFAULT
                       && current () != TokenType.EOF) {
                        try {
                                Statement stmt = null;
@@ -1707,6 +1709,15 @@ public class Vala.Parser : CodeVisitor {
                                case TokenType.WITH:
                                        stmt = parse_statement (current ());
                                        break;
+                               case TokenType.CASE:
+                               case TokenType.DEFAULT:
+                                       if (block is SwitchSection) {
+                                               // begin a new switch case
+                                               return;
+                                       } else {
+                                               stmt = parse_statement (current ());
+                                       }
+                                       break;
                                case TokenType.VAR:
                                        is_decl = true;
                                        parse_local_variable_declarations (block);
index af5be68dc66ce246764ebdeef6162243cd7cdce4..a44256b85c658062f69ee0f2d8a6f6e5fb793dd8 100644 (file)
@@ -88,6 +88,12 @@ public class Vala.SwitchSection : Block {
                        return !error;
                }
 
+               if (!(parent_node is SwitchStatement)) {
+                       Report.error (source_reference, "no enclosing switch statement found");
+                       error = true;
+                       return false;
+               }
+
                foreach (SwitchLabel label in get_labels ()) {
                        label.check (context);
                }