public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-4474] diagnostics: fix ICE on sarif output when source file is unreadable [PR111700]
@ 2023-10-08 22:50 David Malcolm
  0 siblings, 0 replies; only message in thread
From: David Malcolm @ 2023-10-08 22:50 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:94caa6a6b4bd73b6c2bf3ab5e43ca42c5da4287a

commit r14-4474-g94caa6a6b4bd73b6c2bf3ab5e43ca42c5da4287a
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Sun Oct 8 18:43:15 2023 -0400

    diagnostics: fix ICE on sarif output when source file is unreadable [PR111700]
    
    gcc/ChangeLog:
            PR driver/111700
            * input.cc (file_cache::add_file): Update leading comment to
            clarify that it can fail.
            (file_cache::lookup_or_add_file): Likewise.
            (file_cache::get_source_file_content): Gracefully handle
            lookup_or_add_file failing.
    
    gcc/testsuite/ChangeLog:
            PR driver/111700
            * c-c++-common/diagnostic-format-sarif-file-pr111700.c: New test.
    
    Signed-off-by: David Malcolm <dmalcolm@redhat.com>

Diff:
---
 gcc/input.cc                                                 | 12 ++++++++++--
 .../c-c++-common/diagnostic-format-sarif-file-pr111700.c     | 12 ++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/gcc/input.cc b/gcc/input.cc
index 0b00b8923db..1956f2fcbad 100644
--- a/gcc/input.cc
+++ b/gcc/input.cc
@@ -443,7 +443,10 @@ file_cache::evicted_cache_tab_entry (unsigned *highest_use_count)
    accessed by caret diagnostic.  This cache is added to an array of
    cache and can be retrieved by lookup_file_in_cache_tab.  This
    function returns the created cache.  Note that only the last
-   num_file_slots files are cached.  */
+   num_file_slots files are cached.
+
+   This can return nullptr if the FILE_PATH can't be opened for
+   reading, or if the content can't be converted to the input_charset.  */
 
 file_cache_slot*
 file_cache::add_file (const char *file_path)
@@ -547,7 +550,10 @@ file_cache::~file_cache ()
 /* Lookup the cache used for the content of a given file accessed by
    caret diagnostic.  If no cached file was found, create a new cache
    for this file, add it to the array of cached file and return
-   it.  */
+   it.
+
+   This can return nullptr on a cache miss if FILE_PATH can't be opened for
+   reading, or if the content can't be converted to the input_charset.  */
 
 file_cache_slot*
 file_cache::lookup_or_add_file (const char *file_path)
@@ -1072,6 +1078,8 @@ char_span
 file_cache::get_source_file_content (const char *file_path)
 {
   file_cache_slot *c = lookup_or_add_file (file_path);
+  if (c == nullptr)
+    return char_span (nullptr, 0);
   return c->get_full_file_content ();
 }
 
diff --git a/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-pr111700.c b/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-pr111700.c
new file mode 100644
index 00000000000..06605accf6e
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-pr111700.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-fdiagnostics-format=sarif-file" } */
+
+# 0 "this-file-does-not-exist.c"
+#warning message
+
+/* Verify that some JSON was written to a file with the expected name.  */
+/* { dg-final { verify-sarif-file } } */
+
+/* ...and that it at least includes the warning
+       { dg-final { scan-sarif-file "\"message\": " } }
+         { dg-final { scan-sarif-file "\"text\": \"#warning message" } } */

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-10-08 22:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-08 22:50 [gcc r14-4474] diagnostics: fix ICE on sarif output when source file is unreadable [PR111700] David Malcolm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).