From: Dr. David von Oheimb Date: Sat, 6 Jul 2024 15:55:25 +0000 (+0200) Subject: check_format.pl: fix detection of 'if' with single stmt in braces without 'else' X-Git-Tag: openssl-3.1.7~64 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f6cab2ef9bd90e4769aecf21ee63b24856851f35;p=thirdparty%2Fopenssl.git check_format.pl: fix detection of 'if' with single stmt in braces without 'else' Reviewed-by: Richard Levitte Reviewed-by: Neil Horman (Merged from https://github.com/openssl/openssl/pull/24805) (cherry picked from commit f35c0894130e34ff46a429f4373c14ca98437405) --- diff --git a/util/check-format-test-negatives.c b/util/check-format-test-negatives.c index b6c42a00a07..06a11a766f1 100644 --- a/util/check-format-test-negatives.c +++ b/util/check-format-test-negatives.c @@ -335,9 +335,8 @@ size_t UTIL_url_encode(const char *source, int f() { c; - if (1) { + if (1) c; - } c; if (1) if (2) diff --git a/util/check-format.pl b/util/check-format.pl index e1a91bcc581..04dde14c9d9 100755 --- a/util/check-format.pl +++ b/util/check-format.pl @@ -167,7 +167,7 @@ my $local_offset; # current extra indent due to label, switch case/defa my $line_body_start; # number of line where last function body started, or 0 my $line_function_start; # number of line where last function definition started, used for $line_body_start my $last_function_header; # header containing name of last function defined, used if $line_body_start != 0 -my $line_opening_brace; # number of previous line with opening brace after do/while/for, optionally for if/else +my $line_opening_brace; # number of previous line with opening brace after if/do/while/for, optionally for 'else' my $keyword_opening_brace; # name of previous keyword, used if $line_opening_brace != 0 my $block_indent; # currently required normal indentation at block/statement level @@ -972,9 +972,12 @@ while (<>) { # loop over all lines of all input files # check for code block containing a single line/statement if ($line_before2 > 0 && !$outermost_level && # within function body $in_typedecl == 0 && @nested_indents == 0 && # neither within type declaration nor inside stmt/expr - m/^[\s@]*\}/) { # leading closing brace '}', any preceding blinded comment must not be matched + m/^[\s@]*\}\s*(\w*)/) { # leading closing brace '}', any preceding blinded comment must not be matched # TODO extend detection from single-line to potentially multi-line statement + my $next_word = $1; if ($line_opening_brace > 0 && + ($keyword_opening_brace ne "if" || + $extended_1_stmt || $next_word ne "else") && ($line_opening_brace == $line_before2 || $line_opening_brace == $line_before) && $contents_before =~ m/;/) { # there is at least one terminator ';', so there is some stmt @@ -1132,9 +1135,9 @@ while (<>) { # loop over all lines of all input files $line_body_start = $contents =~ m/LONG BODY/ ? 0 : $line if $line_function_start != 0; } } else { - $line_opening_brace = $line if $keyword_opening_brace =~ m/do|while|for/; + $line_opening_brace = $line if $keyword_opening_brace =~ m/if|do|while|for/; # using, not assigning, $keyword_opening_brace here because it could be on an earlier line - $line_opening_brace = $line if $keyword_opening_brace =~ m/if|else/ && $extended_1_stmt && + $line_opening_brace = $line if $keyword_opening_brace eq "else" && $extended_1_stmt && # TODO prevent false positives for if/else where braces around single-statement branches # should be avoided but only if all branches have just single statements # The following helps detecting the exception when handling multiple 'if ... else' branches: