]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Correctly handle possible null from SourceFile.get_source_line()
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 1 Aug 2023 09:59:52 +0000 (11:59 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 1 Aug 2023 09:59:52 +0000 (11:59 +0200)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1464

tests/Makefile.am
tests/parser/switch-section-missing-colon.test [new file with mode: 0644]
vala/valareport.vala

index 41f293c1f7fd4de89af686b72eb04b8649ffdb49..54456253a11a57715f693b02fcc90f0b8e5e0854 100644 (file)
@@ -1041,6 +1041,7 @@ TESTS = \
        parser/statement-keyword-as-identifier.vala \
        parser/statement-outside-root.test \
        parser/switch-statement.vala \
+       parser/switch-section-missing-colon.test \
        parser/switch-section-outside-switch.test \
        parser/template.vala \
        parser/try-catch-in-switch-case-invalid.test \
diff --git a/tests/parser/switch-section-missing-colon.test b/tests/parser/switch-section-missing-colon.test
new file mode 100644 (file)
index 0000000..923065d
--- /dev/null
@@ -0,0 +1,9 @@
+Invalid Code
+
+void main () {
+       int foo = 0;
+       switch (foo) {
+       case 0
+               break;
+       }
+}
index 081f37c42b4b7e6a69fef0821a6dc04f1092e1fb..5e4885a6ec2b456517f368410b21ddd1544355da 100644 (file)
@@ -233,7 +233,10 @@ public class Vala.Report {
         */
        private void report_source (SourceReference source) {
                for (int idx = source.begin.line; idx <= source.end.line; idx++) {
-                       string offending_line = source.file.get_source_line (idx);
+                       string? offending_line = source.file.get_source_line (idx);
+                       if (offending_line == null) {
+                               break;
+                       }
                        printerr ("%5d | %s\n", idx, offending_line);
                        printerr ("      | ");
                        stderr.puts (caret_color_start);