diff mbox series

c: Add some checking asserts to named loops handling code

Message ID Zw+0R04P4g5L06eX@tucnak
State New
Headers show
Series c: Add some checking asserts to named loops handling code | expand

Commit Message

Jakub Jelinek Oct. 16, 2024, 12:40 p.m. UTC
Hi!

Jonathan mentioned an unnamed static analyzer reported issue in
c_finish_bc_name.
It is actually a false positive, because the construction of the
loop_names vector guarantees that the last element of the vector
(if the vector is non-empty) always has either
C_DECL_LOOP_NAME (l) or C_DECL_SWITCH_NAME (l) (or both) flags
set, so c will be always non-NULL after the if at the start of the
loops.
The following patch is an attempt to help those static analyzers
(though dunno if it actually helps), by adding a checking assert.

Tested on x86_64-linux, ok for trunk?

2024-10-16  Jakub Jelinek  <jakub@redhat.com>

	* c-decl.cc (c_get_loop_names): Add checking assert that
	c is non-NULL in the loop.
	(c_finish_bc_name): Likewise.


	Jakub

Comments

Joseph Myers Oct. 16, 2024, 3:12 p.m. UTC | #1
On Wed, 16 Oct 2024, Jakub Jelinek wrote:

> Hi!
> 
> Jonathan mentioned an unnamed static analyzer reported issue in
> c_finish_bc_name.
> It is actually a false positive, because the construction of the
> loop_names vector guarantees that the last element of the vector
> (if the vector is non-empty) always has either
> C_DECL_LOOP_NAME (l) or C_DECL_SWITCH_NAME (l) (or both) flags
> set, so c will be always non-NULL after the if at the start of the
> loops.
> The following patch is an attempt to help those static analyzers
> (though dunno if it actually helps), by adding a checking assert.
> 
> Tested on x86_64-linux, ok for trunk?
> 
> 2024-10-16  Jakub Jelinek  <jakub@redhat.com>
> 
> 	* c-decl.cc (c_get_loop_names): Add checking assert that
> 	c is non-NULL in the loop.
> 	(c_finish_bc_name): Likewise.

OK.
diff mbox series

Patch

--- gcc/c/c-decl.cc.jj	2024-10-16 10:06:06.378589144 +0200
+++ gcc/c/c-decl.cc	2024-10-16 14:30:36.253187498 +0200
@@ -13881,6 +13881,7 @@  c_get_loop_names (tree before_labels, bo
 	    {
 	      if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
 		c = l;
+	      gcc_checking_assert (c);
 	      loop_names_hash->put (l, c);
 	      if (i == first)
 		break;
@@ -13952,6 +13953,7 @@  c_finish_bc_name (location_t loc, tree n
 	  {
 	    if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
 	      c = l;
+	    gcc_checking_assert (c);
 	    if (l == lab)
 	      {
 		label = c;
@@ -13970,6 +13972,7 @@  c_finish_bc_name (location_t loc, tree n
 	{
 	  if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
 	    c = l;
+	  gcc_checking_assert (c);
 	  if (is_break || C_DECL_LOOP_NAME (c))
 	    candidates.safe_push (IDENTIFIER_POINTER (DECL_NAME (l)));
 	}