Avoid undefined behaviour in build_option_suggestions

Message ID 20240719165206.286881-1-siddhesh@gotplt.org
State Committed
Commit e0d997e913f811ecf4b3e10891e6a4aab5b38a31
Headers
Series Avoid undefined behaviour in build_option_suggestions |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 success Test passed

Commit Message

Siddhesh Poyarekar July 19, 2024, 4:52 p.m. UTC
  The inner loop in build_option_suggestions uses OPTION to take the
address of OPTB and use it across iterations, which is undefined
behaviour since OPTB is defined within the loop.  Pull it outside the
loop to make this defined.

It's a trivial fix, so posting early.  OK to commit once build and test
succeeds?

Thanks,
Sid

gcc/ChangeLog:

	* opt-suggestions.cc
	(option_proposer::build_option_suggestions): Pull OPTB
	definition out of the innermost loop.
---
 gcc/opt-suggestions.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Jakub Jelinek July 19, 2024, 6:42 p.m. UTC | #1
On Fri, Jul 19, 2024 at 12:52:06PM -0400, Siddhesh Poyarekar wrote:
> The inner loop in build_option_suggestions uses OPTION to take the
> address of OPTB and use it across iterations, which is undefined
> behaviour since OPTB is defined within the loop.  Pull it outside the
> loop to make this defined.
> 
> It's a trivial fix, so posting early.  OK to commit once build and test
> succeeds?
> 
> Thanks,
> Sid
> 
> gcc/ChangeLog:
> 
> 	* opt-suggestions.cc
> 	(option_proposer::build_option_suggestions): Pull OPTB
> 	definition out of the innermost loop.

Ok for trunk, 14, 13 & 12 branches.

> ---
>  gcc/opt-suggestions.cc | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/gcc/opt-suggestions.cc b/gcc/opt-suggestions.cc
> index 849e4f5c439..71e5cb561a6 100644
> --- a/gcc/opt-suggestions.cc
> +++ b/gcc/opt-suggestions.cc
> @@ -167,9 +167,9 @@ option_proposer::build_option_suggestions (const char *prefix)
>  	    add_misspelling_candidates (m_option_suggestions, option,
>  					opt_text);
>  
> +	    struct cl_option optb;
>  	    for (int j = 0; sanitizer_opts[j].name != NULL; ++j)
>  	      {
> -		struct cl_option optb;
>  		/* -fsanitize=all is not valid, only -fno-sanitize=all.
>  		   So don't register the positive misspelling candidates
>  		   for it.  */
> -- 
> 2.45.1

	Jakub
  

Patch

diff --git a/gcc/opt-suggestions.cc b/gcc/opt-suggestions.cc
index 849e4f5c439..71e5cb561a6 100644
--- a/gcc/opt-suggestions.cc
+++ b/gcc/opt-suggestions.cc
@@ -167,9 +167,9 @@  option_proposer::build_option_suggestions (const char *prefix)
 	    add_misspelling_candidates (m_option_suggestions, option,
 					opt_text);
 
+	    struct cl_option optb;
 	    for (int j = 0; sanitizer_opts[j].name != NULL; ++j)
 	      {
-		struct cl_option optb;
 		/* -fsanitize=all is not valid, only -fno-sanitize=all.
 		   So don't register the positive misspelling candidates
 		   for it.  */