opts: Add an assertion to help static analyzers [PR106332]

Message ID YuECqsVtvkrv0Btn@tucnak
State New, archived
Headers
Series opts: Add an assertion to help static analyzers [PR106332] |

Commit Message

Jakub Jelinek July 27, 2022, 9:17 a.m. UTC
  Hi!

This function would have UB if called with empty candidates vector
(accessing p[-1] where p is malloc (0) result).
As analyzed in the PR, we never call it with empty vector, so this just
adds an assertion to make it clear.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2022-07-27  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/106332
	* opts-common.cc (candidates_list_and_hint): Add gcc_checking_assert
	that candidates is not an empty vector.


	Jakub
  

Comments

Richard Biener July 27, 2022, 9:23 a.m. UTC | #1
On Wed, 27 Jul 2022, Jakub Jelinek wrote:

> Hi!
> 
> This function would have UB if called with empty candidates vector
> (accessing p[-1] where p is malloc (0) result).
> As analyzed in the PR, we never call it with empty vector, so this just
> adds an assertion to make it clear.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK, I think we can make this a gcc_assert even.

Richard.

> 2022-07-27  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR middle-end/106332
> 	* opts-common.cc (candidates_list_and_hint): Add gcc_checking_assert
> 	that candidates is not an empty vector.
> 
> --- gcc/opts-common.cc.jj	2022-02-04 14:36:55.439599237 +0100
> +++ gcc/opts-common.cc	2022-07-26 11:21:49.785919993 +0200
> @@ -1347,6 +1347,8 @@ candidates_list_and_hint (const char *ar
>    const char *candidate;
>    char *p;
>  
> +  gcc_checking_assert (!candidates.is_empty ());
> +
>    FOR_EACH_VEC_ELT (candidates, i, candidate)
>      len += strlen (candidate) + 1;
>  
> 
> 	Jakub
> 
>
  

Patch

--- gcc/opts-common.cc.jj	2022-02-04 14:36:55.439599237 +0100
+++ gcc/opts-common.cc	2022-07-26 11:21:49.785919993 +0200
@@ -1347,6 +1347,8 @@  candidates_list_and_hint (const char *ar
   const char *candidate;
   char *p;
 
+  gcc_checking_assert (!candidates.is_empty ());
+
   FOR_EACH_VEC_ELT (candidates, i, candidate)
     len += strlen (candidate) + 1;