public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Immad Mir <mirimmad@outlook.com>
To: gcc-patches@gcc.gnu.org
Cc: dmalcolm@redhat.com, Immad Mir <mirimmad@outlook.com>
Subject: [PATCH] analyzer: fix ICE casued by dup2 in sm-fd.cc[PR106551]
Date: Thu, 11 Aug 2022 14:41:45 +0530	[thread overview]
Message-ID: <CY4PR1801MB1910D4556C1DD65E43A281C5C6649@CY4PR1801MB1910.namprd18.prod.outlook.com> (raw)

This patch fixes the ICE caused by valid_to_unchecked_state,
at analyzer/sm-fd.cc by handling the m_start state in
check_for_dup.

Tested lightly on x86_64.

gcc/analyzer/ChangeLog:
	PR analyzer/106551
	* sm-fd.cc (check_for_dup): handle the m_start
	state when transitioning the state of LHS
	of dup, dup2 and dup3 call.

gcc/testsuite/ChangeLog:
	* gcc.dg/analyzer/fd-dup-1.c: New testcases.
	* gcc.dg/analyzer/fd-uninit-1.c: Remove bogus
	warning.
Signed-off-by: Immad Mir <mirimmad@outlook.com>
---
 gcc/analyzer/sm-fd.cc                       | 10 +++++---
 gcc/testsuite/gcc.dg/analyzer/fd-dup-1.c    | 27 ++++++++++++++++++++-
 gcc/testsuite/gcc.dg/analyzer/fd-uninit-1.c |  2 --
 3 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/gcc/analyzer/sm-fd.cc b/gcc/analyzer/sm-fd.cc
index 8bb76d72b05..e02b86baad1 100644
--- a/gcc/analyzer/sm-fd.cc
+++ b/gcc/analyzer/sm-fd.cc
@@ -971,7 +971,8 @@ fd_state_machine::check_for_dup (sm_context *sm_ctxt, const supernode *node,
   state_t state_arg_1 = sm_ctxt->get_state (stmt, arg_1);
   if (state_arg_1 == m_stop)
     return;
-  if (!(is_constant_fd_p (state_arg_1) || is_valid_fd_p (state_arg_1)))
+  if (!(is_constant_fd_p (state_arg_1) || is_valid_fd_p (state_arg_1)
+	|| state_arg_1 == m_start))
     {
       check_for_open_fd (sm_ctxt, node, stmt, call, callee_fndecl,
 			 DIRS_READ_WRITE);
@@ -983,7 +984,7 @@ fd_state_machine::check_for_dup (sm_context *sm_ctxt, const supernode *node,
     case DUP_1:
       if (lhs)
 	{
-	  if (is_constant_fd_p (state_arg_1))
+	  if (is_constant_fd_p (state_arg_1) || state_arg_1 == m_start)
 	    sm_ctxt->set_next_state (stmt, lhs, m_unchecked_read_write);
 	  else
 	    sm_ctxt->set_next_state (stmt, lhs,
@@ -999,7 +1000,8 @@ fd_state_machine::check_for_dup (sm_context *sm_ctxt, const supernode *node,
       if (state_arg_2 == m_stop)
 	return;
       /* Check if -1 was passed as second argument to dup2.  */
-      if (!(is_constant_fd_p (state_arg_2) || is_valid_fd_p (state_arg_2)))
+      if (!(is_constant_fd_p (state_arg_2) || is_valid_fd_p (state_arg_2)
+	    || state_arg_2 == m_start))
 	{
 	  sm_ctxt->warn (
 	      node, stmt, arg_2,
@@ -1011,7 +1013,7 @@ fd_state_machine::check_for_dup (sm_context *sm_ctxt, const supernode *node,
       file descriptor i.e the first argument.  */
       if (lhs)
 	{
-	  if (is_constant_fd_p (state_arg_1))
+	  if (is_constant_fd_p (state_arg_1) || state_arg_1 == m_start)
 	    sm_ctxt->set_next_state (stmt, lhs, m_unchecked_read_write);
 	  else
 	    sm_ctxt->set_next_state (stmt, lhs,
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-dup-1.c b/gcc/testsuite/gcc.dg/analyzer/fd-dup-1.c
index eba2570568f..b971d31b1c7 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-dup-1.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-dup-1.c
@@ -220,4 +220,29 @@ test_19 (const char *path, void *buf)
         close (fd);
     }
     
-}
\ No newline at end of file
+}
+
+extern int m;
+
+void
+test_20 ()
+{
+    int fd = dup (m); 
+    close (fd);
+}
+
+void
+test_21 ()
+{
+    int fd = dup2 (m, 1); 
+    close (fd);
+}
+
+void
+test_22 (int flags)
+{
+    int fd = dup3 (m, 1, flags);
+    close (fd);
+}
+
+
diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-uninit-1.c b/gcc/testsuite/gcc.dg/analyzer/fd-uninit-1.c
index b5b189ece98..1084d1b4da2 100644
--- a/gcc/testsuite/gcc.dg/analyzer/fd-uninit-1.c
+++ b/gcc/testsuite/gcc.dg/analyzer/fd-uninit-1.c
@@ -9,8 +9,6 @@ test_1 ()
 {
   int m;
   return dup (m); /* { dg-warning "use of uninitialized value 'm'" "uninit" } */
-  /* { dg-bogus "'dup' on possibly invalid file descriptor 'm'" "invalid fd false +ve" { xfail *-*-* } .-1 } */
-  /* XFAIL: probably covered by fix for PR analyzer/106551.  */
 }
 
 int
-- 
2.25.1


             reply	other threads:[~2022-08-11  9:12 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-11  9:11 Immad Mir [this message]
2022-08-11 14:22 ` David Malcolm
  -- strict thread matches above, loose matches on Subject: below --
2022-08-10 15:05 Immad Mir
2022-08-09 16:12 Immad Mir
2022-08-09 20:02 ` David Malcolm
2022-08-10 15:04   ` Mir Immad
2022-08-10 16:56     ` David Malcolm
2022-08-10 17:21       ` Mir Immad
2022-08-10 18:43         ` David Malcolm
2022-08-11  9:10           ` Mir Immad
2022-08-09  8:13 Immad Mir
2022-08-09  7:46 Immad Mir
2022-08-09 15:12 ` David Malcolm
2022-08-09 16:14   ` Mir Immad

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=CY4PR1801MB1910D4556C1DD65E43A281C5C6649@CY4PR1801MB1910.namprd18.prod.outlook.com \
    --to=mirimmad@outlook.com \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=mirimnan017@gmail.com \
    /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).