From a0449437d5ece8f14f3b450e91325e0e8a6765e7 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Thu, 10 May 2018 17:39:00 +0200 Subject: [PATCH] Refactor flag overwrite check into separate function --- bin/blhc | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/bin/blhc b/bin/blhc index f11ba11..d56a6c0 100755 --- a/bin/blhc +++ b/bin/blhc @@ -427,35 +427,46 @@ sub all_flags_used { @{$missing_flags_ref} = @missing_flags; return 0; } +# Check if any of \@bad_flags occurs after $good_flag. Doesn't check if +# $good_flag is present. +sub flag_overwritten { + my ($line, $good_flag, $bad_flags) = @_; -sub cppflags_fortify_broken { - my ($line, $missing_flags) = @_; - - if (not any_flags_used($line, @def_cppflags_fortify_bad)) { + if (not any_flags_used($line, @{$bad_flags})) { return 0; } - # $def_cppflags_fortify[0] must be -D_FORTIFY_SOURCE=2! - my $fortify_source = $def_cppflags_fortify[0]; - - # Some build systems enable/disable fortify source multiple times, check - # the final result. - my $disable_pos = 0; - foreach my $flag (@def_cppflags_fortify_bad) { + my $bad_pos = 0; + foreach my $flag (@{$bad_flags}) { while ($line =~ /$flag/g) { - if ($disable_pos < $+[0]) { - $disable_pos = $+[0]; + if ($bad_pos < $+[0]) { + $bad_pos = $+[0]; } } } - my $enable_pos = 0; - while ($line =~ /$fortify_source/g) { - $enable_pos = $+[0]; + my $good_pos = 0; + while ($line =~ /$good_flag/g) { + $good_pos = $+[0]; } - if ($enable_pos > $disable_pos) { + if ($good_pos > $bad_pos) { return 0; } + return 1; +} +sub cppflags_fortify_broken { + my ($line, $missing_flags) = @_; + + # $def_cppflags_fortify[0] must be -D_FORTIFY_SOURCE=2! + my $fortify_source = $def_cppflags_fortify[0]; + + # Some build systems enable/disable fortify source multiple times, check + # the final result. + if (not flag_overwritten($line, + $fortify_source, + \@def_cppflags_fortify_bad)) { + return 0; + } push @{$missing_flags}, $fortify_source; return 1; } -- 2.43.2