analyzer: fix ICE casued by dup2 in sm-fd.cc[PR106551]

Message ID CY4PR1801MB191096562F23CDD2F738C28DC6659@CY4PR1801MB1910.namprd18.prod.outlook.com
State New, archived
Headers
Series analyzer: fix ICE casued by dup2 in sm-fd.cc[PR106551] |

Commit Message

Immad Mir Aug. 10, 2022, 3:05 p.m. UTC
  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.

Signed-off-by: Immad Mir <mirimmad@outlook.com>
---
 gcc/analyzer/sm-fd.cc                    |  4 ++--
 gcc/testsuite/gcc.dg/analyzer/fd-dup-1.c | 27 +++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 3 deletions(-)
  

Patch

diff --git a/gcc/analyzer/sm-fd.cc b/gcc/analyzer/sm-fd.cc
index 8bb76d72b05..c8b9930a7b6 100644
--- a/gcc/analyzer/sm-fd.cc
+++ b/gcc/analyzer/sm-fd.cc
@@ -983,7 +983,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,
@@ -1011,7 +1011,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..bc823d1ce4e 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); /* { dg-warning "'dup' on possibly invalid file descriptor 'm'" } */
+    close (fd);
+}
+
+void
+test_21 ()
+{
+    int fd = dup2 (m, 1); /* { dg-warning "'dup2' on possibly invalid file descriptor 'm'" } */
+    close (fd);
+}
+
+void
+test_22 (int flags)
+{
+    int fd = dup3 (m, 1, flags); /* { dg-warning "'dup3' on possibly invalid file descriptor 'm'" } */
+    close (fd);
+}
+
+