public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Immad Mir <mir@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-1860] analyzer: add get_meaning_for_state_change vfunc to fd_diagnostic in sm-fd.cc [PR106286]
Date: Wed, 27 Jul 2022 13:55:43 +0000 (GMT)	[thread overview]
Message-ID: <20220727135543.ADB613857B86@sourceware.org> (raw)

https://gcc.gnu.org/g:0f82c0ea8d86ee3bb404c460a04ff2ccfb56d2a0

commit r13-1860-g0f82c0ea8d86ee3bb404c460a04ff2ccfb56d2a0
Author: Immad Mir <mirimmad@outlook.com>
Date:   Wed Jul 27 19:16:36 2022 +0530

    analyzer: add get_meaning_for_state_change vfunc to fd_diagnostic in sm-fd.cc [PR106286]
    
    This patch adds get_meaning_for_state_change vfunc to
    fd_diagnostic in sm-fd.cc which could be used by SARIF output.
    
    Lightly tested on x86_64 Linux.
    
    gcc/analyzer/ChangeLog:
            PR analyzer/106286
            * sm-fd.cc:
            (fd_diagnostic::get_meaning_for_state_change): New.
    
    gcc/testsuite/ChangeLog:
            PR analyzer/106286
            * gcc.dg/analyzer/fd-meaning.c: New test.
    
    Signed-off-by: Immad Mir <mirimmad@outlook.com>

Diff:
---
 gcc/analyzer/sm-fd.cc                      | 14 +++++++++++
 gcc/testsuite/gcc.dg/analyzer/fd-meaning.c | 37 ++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/gcc/analyzer/sm-fd.cc b/gcc/analyzer/sm-fd.cc
index 56b0063ba42..ed923ade100 100644
--- a/gcc/analyzer/sm-fd.cc
+++ b/gcc/analyzer/sm-fd.cc
@@ -229,6 +229,20 @@ public:
     return label_text ();
   }
 
+  diagnostic_event::meaning
+  get_meaning_for_state_change (
+      const evdesc::state_change &change) const final override
+  {
+    if (change.m_old_state == m_sm.get_start_state ()
+		&& (m_sm.is_unchecked_fd_p (change.m_new_state)))
+      return diagnostic_event::meaning (diagnostic_event::VERB_acquire,
+			 diagnostic_event::NOUN_resource);
+    if (change.m_new_state == m_sm.m_closed)
+      return diagnostic_event::meaning (diagnostic_event::VERB_release,
+			 diagnostic_event::NOUN_resource);
+    return diagnostic_event::meaning ();
+  }
+
 protected:
   const fd_state_machine &m_sm;
   tree m_arg;
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-meaning.c b/gcc/testsuite/gcc.dg/analyzer/fd-meaning.c
new file mode 100644
index 00000000000..6a9ec921fd3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-meaning.c
@@ -0,0 +1,37 @@
+  /* { dg-additional-options "-fanalyzer-verbose-state-changes" } */
+int open(const char *, int mode);
+void close(int fd);
+
+#define O_RDONLY 0
+#define O_WRONLY 1
+#define O_RDWR 2
+
+void test_1 (const char* path)
+{
+    int fd = open (path, O_RDWR); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */
+    if (fd != -1)
+    {
+        close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */
+        close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */
+    }
+}
+
+void test_2 (const char* path)
+{
+    int fd = open (path, O_RDONLY); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */
+    if (fd != -1)
+    {
+        close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */
+        close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */
+    }
+}
+
+void test_3 (const char* path)
+{
+    int fd = open (path, O_WRONLY); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */
+    if (fd != -1)
+    {
+        close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */
+        close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */
+    }
+}
\ No newline at end of file


                 reply	other threads:[~2022-07-27 13:55 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220727135543.ADB613857B86@sourceware.org \
    --to=mir@gcc.gnu.org \
    --cc=gcc-cvs@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).