public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Mir Immad <mirimnan017@gmail.com>
To: gcc-patches@gcc.gnu.org, David Malcolm <dmalcolm@redhat.com>
Subject: [PATCH 1/2] analyzer: show close event for use_after_close diagnostic
Date: Tue, 5 Jul 2022 23:07:42 +0530	[thread overview]
Message-ID: <CAE1-7owa7nexL8sBHerU_xTDPWC-Pab3MqUam81q3WVyF0rYLQ@mail.gmail.com> (raw)

From be60d5194068355ccdbf832d0de9dbfed1e0b074 Mon Sep 17 00:00:00 2001
From: Immad Mir <mirimmad17@gmail.com>
Date: Tue, 5 Jul 2022 21:14:06 +0530
Subject: [PATCH 1/2] analyzer: show close event for use_after_close
diagnostic

This patch saves the "close" event in use_after_close diagnostic
and shows it where possible.

gcc/analyzer/ChangeLog:
* sm-fd.cc (use_after_close): save the "close" event and
show it where possible.

gcc/testsuite/ChangeLog:
* gcc.dg/analyzer/fd-4.c (test_3): change the message note to conform to the
changes in analyzer/sm-fd.cc
(test_4): Likewise.

Signed-off-by: Immad Mir <mirimmad17@gmail.com>
---
 gcc/analyzer/sm-fd.cc                | 13 +++++++++++--
 gcc/testsuite/gcc.dg/analyzer/fd-4.c |  4 ++--
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/gcc/analyzer/sm-fd.cc b/gcc/analyzer/sm-fd.cc
index 4058ac53308..a85c95cc502 100644
--- a/gcc/analyzer/sm-fd.cc
+++ b/gcc/analyzer/sm-fd.cc
@@ -454,7 +454,10 @@ public:
       return label_text::borrow ("opened here");

     if (change.m_new_state == m_sm.m_closed)
+    {
+      m_first_close_event = change.m_event_id;
        return change.formatted_print ("closed here");
+    }

     return fd_diagnostic::describe_state_change (change);
   }
@@ -462,11 +465,17 @@ public:
   label_text
   describe_final_event (const evdesc::final_event &ev) final override
   {
-    return ev.formatted_print ("%qE on closed file descriptor %qE here",
-                               m_callee_fndecl, m_arg);
+    if (m_first_close_event.known_p ())
+      return ev.formatted_print (
+          "%qE on closed file descriptor %qE; %qs was at %@",
m_callee_fndecl,
+          m_arg, "close", &m_first_close_event);
+    else
+      return ev.formatted_print ("%qE on closed file descriptor %qE",
+                                 m_callee_fndecl, m_arg);
   }

 private:
+  diagnostic_event_id_t m_first_close_event;
   const tree m_callee_fndecl;
 };

diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-4.c
b/gcc/testsuite/gcc.dg/analyzer/fd-4.c
index a973704f403..c992db619e7 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-4.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-4.c
@@ -45,7 +45,7 @@ test_3 (const char *path, void *buf)
     {
         close(fd); /* {dg-message "\\(2\\) closed here"} */
         read(fd, buf, 1); /* { dg-warning "'read' on closed file
descriptor 'fd'" }  */
-        /* {dg-message "\\(3\\) 'read' on closed file descriptor 'fd'
here" "" {target *-*-*} .-1 } */
+        /* {dg-message "\\(3\\) 'read' on closed file descriptor 'fd';
'close' was at \\(2\\)" "" {target *-*-*} .-1 } */
     }
 }

@@ -57,6 +57,6 @@ test_4 (const char *path, void *buf)
     {
         close(fd); /* {dg-message "\\(2\\) closed here"} */
         write(fd, buf, 1); /* { dg-warning "'write' on closed file
descriptor 'fd'" }  */
-        /* {dg-message "\\(3\\) 'write' on closed file descriptor 'fd'
here" "" {target *-*-*} .-1 } */
+        /* {dg-message "\\(3\\) 'write' on closed file descriptor 'fd';
'close' was at \\(2\\)" "" {target *-*-*} .-1 } */
     }
 }
-- 
2.25.1

             reply	other threads:[~2022-07-05 17:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-05 17:37 Mir Immad [this message]
2022-07-05 20:50 ` David Malcolm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAE1-7owa7nexL8sBHerU_xTDPWC-Pab3MqUam81q3WVyF0rYLQ@mail.gmail.com \
    --to=mirimnan017@gmail.com \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).