From b987ea4940b34d6ccd05fff063c2904e9f309beb Mon Sep 17 00:00:00 2001 From: teastep Date: Mon, 9 Mar 2009 20:56:53 +0000 Subject: [PATCH] Replace discrete tests with fatal_error() calls with assert() calls. Signed-off-by: Tom Eastep git-svn-id: https://shorewall.svn.sourceforge.net/svnroot/shorewall/trunk@9672 fbd18981-670d-0410-9b5c-8dc0c1a9a2bb --- Shorewall/Perl/Shorewall/Actions.pm | 2 +- Shorewall/Perl/Shorewall/Chains.pm | 16 ++++++++-------- Shorewall/Perl/Shorewall/Config.pm | 21 +++++++++++++++------ Shorewall/Perl/Shorewall/IPAddrs.pm | 2 +- Shorewall/Perl/Shorewall/Policy.pm | 2 +- Shorewall/Perl/Shorewall/Proc.pm | 2 +- Shorewall/Perl/Shorewall/Rules.pm | 2 +- Shorewall/Perl/Shorewall/Tc.pm | 2 +- Shorewall/Perl/Shorewall/Zones.pm | 2 +- 9 files changed, 30 insertions(+), 21 deletions(-) diff --git a/Shorewall/Perl/Shorewall/Actions.pm b/Shorewall/Perl/Shorewall/Actions.pm index 67c02649a..776eed8bc 100644 --- a/Shorewall/Perl/Shorewall/Actions.pm +++ b/Shorewall/Perl/Shorewall/Actions.pm @@ -712,7 +712,7 @@ sub process_action3( $$$$$ ) { if ( $action2type & ACTION ) { $target2 = (find_logactionchain ( $target = $target2 ))->{name}; } else { - fatal_error "Internal Error" unless $action2type & ( MACRO | LOGRULE | NFQ | CHAIN ); + assert( $action2type & ( MACRO | LOGRULE | NFQ | CHAIN ) ); } } diff --git a/Shorewall/Perl/Shorewall/Chains.pm b/Shorewall/Perl/Shorewall/Chains.pm index e4a4b1c71..dfff33e95 100644 --- a/Shorewall/Perl/Shorewall/Chains.pm +++ b/Shorewall/Perl/Shorewall/Chains.pm @@ -387,7 +387,7 @@ sub incr_cmd_level( $ ) { } sub decr_cmd_level( $ ) { - fatal_error "Internal error in decr_cmd_level()" if --$_[0]->{cmdlevel} < 0; + assert( --$_[0]->{cmdlevel} >= 0); } # @@ -535,7 +535,7 @@ sub add_rule($$;$) { my ($chainref, $rule, $expandports) = @_; - fatal_error 'Internal Error in add_rule()' if reftype $rule; + assert( ! reftype $rule ); $iprangematch = 0; # @@ -627,7 +627,7 @@ sub insert_rule1($$$) { my ($chainref, $number, $rule) = @_; - fatal_error 'Internal Error in insert_rule()' if $chainref->{cmdlevel}; + assert( ! $chainref->{cmdlevel}); $rule .= "-m comment --comment \"$comment\"" if $comment; @@ -670,7 +670,7 @@ sub move_rules( $$ ) { my @rules = @{$chain1->{rules}}; for ( @rules ) { - fatal_error "Internal Error in move_rules()" unless /^-A/; + assert( /^-A/ ); } splice @{$chain2->{rules}}, 0, 0, @rules; @@ -903,7 +903,7 @@ sub new_chain($$) { my ($table, $chain) = @_; - fatal_error "Internal error in new_chain()" if $chain_table{$table}{$chain} || $builtin_target{ $chain }; + assert( ! $chain_table{$table}{$chain} && ! $builtin_target{ $chain } ); $chain_table{$table}{$chain} = { name => $chain, rules => [], @@ -920,7 +920,7 @@ sub ensure_chain($$) { my ($table, $chain) = @_; - fatal_error 'Internal Error in ensure_chain' unless $table && $chain; + assert( $table && $chain ); my $ref = $chain_table{$table}{$chain}; @@ -2774,7 +2774,7 @@ sub create_netfilter_load( $ ) { for my $chain ( @builtins ) { my $chainref = $chain_table{$table}{$chain}; if ( $chainref ) { - fatal_error "Internal error in create_netfilter_load()" if $chainref->{cmdlevel}; + assert( $chainref->{cmdlevel} == 0 ); emit_unindented ":$chain $chainref->{policy} [0:0]"; push @chains, $chainref; } @@ -2785,7 +2785,7 @@ sub create_netfilter_load( $ ) { for my $chain ( grep $chain_table{$table}{$_}->{referenced} , ( sort keys %{$chain_table{$table}} ) ) { my $chainref = $chain_table{$table}{$chain}; unless ( $chainref->{builtin} ) { - fatal_error "Internal error in create_netfilter_load()" if $chainref->{cmdlevel}; + assert( $chainref->{cmdlevel} == 0 ); emit_unindented ":$chainref->{name} - [0:0]"; push @chains, $chainref; } diff --git a/Shorewall/Perl/Shorewall/Config.pm b/Shorewall/Perl/Shorewall/Config.pm index 3a029fa48..b97cd9be9 100644 --- a/Shorewall/Perl/Shorewall/Config.pm +++ b/Shorewall/Perl/Shorewall/Config.pm @@ -45,6 +45,7 @@ our @ISA = qw(Exporter); our @EXPORT = qw( warning_message fatal_error + assert progress_message progress_message_nocompress progress_message2 @@ -702,6 +703,14 @@ sub fatal_error1 { die " ERROR: @_\n"; } +sub assert( $ ) { + unless ( $_[0] ) { + my @caller = caller 1; + + fatal_error "Internal error in $caller[0]::$caller[3]"; + } +} + # # Convert value to decimal number # @@ -746,7 +755,7 @@ sub in_hex8( $ ) { # Replaces leading spaces with tabs as appropriate and suppresses consecutive blank lines. # sub emit { - fatal_error 'Internal Error in emit' unless $object_enabled; + assert ( $object_enabled ); if ( $object ) { # @@ -772,7 +781,7 @@ sub emit { # Write passed message to the object with newline but no indentation. # sub emit_unindented( $ ) { - fatal_error 'Internal Error in emit_unindented' unless $object_enabled; + assert( $object_enabled ); print $object "$_[0]\n" if $object; } @@ -964,7 +973,7 @@ sub pop_indent() { # Functions for copying files into the object # sub copy( $ ) { - fatal_error 'Internal Error in copy' unless $object_enabled; + assert( $object_enabled ); if ( $object ) { my $file = $_[0]; @@ -996,7 +1005,7 @@ sub copy( $ ) { # This one handles line continuation and 'here documents' sub copy1( $ ) { - fatal_error 'Internal Error in copy1' unless $object_enabled; + assert( $object_enabled ); if ( $object ) { my $file = $_[0]; @@ -1274,7 +1283,7 @@ sub do_open_file( $ ) { sub open_file( $ ) { my $fname = find_file $_[0]; - fatal_error 'Internal Error in open_file()' if defined $currentfile; + assert( ! defined $currentfile ); -f $fname && -s _ ? do_open_file $fname : ''; } @@ -1448,7 +1457,7 @@ sub embedded_perl( $ ) { if ( $scriptfile ) { fatal_error "INCLUDEs nested too deeply" if @includestack >= 4; - close $scriptfile or fatal_error "Internal Error in embedded_perl()"; + close $scriptfile or assert(0); $scriptfile = undef; diff --git a/Shorewall/Perl/Shorewall/IPAddrs.pm b/Shorewall/Perl/Shorewall/IPAddrs.pm index 7fa60d834..f3f2afae9 100644 --- a/Shorewall/Perl/Shorewall/IPAddrs.pm +++ b/Shorewall/Perl/Shorewall/IPAddrs.pm @@ -202,7 +202,7 @@ sub validate_4net( $$ ) { } if ( defined wantarray ) { - fatal_error "Internal Error in validate_net()" if $allow_name; + assert ( ! $allow_name ); if ( wantarray ) { ( decodeaddr( $net ) , $vlsm ); } else { diff --git a/Shorewall/Perl/Shorewall/Policy.pm b/Shorewall/Perl/Shorewall/Policy.pm index ff4e9970b..09b002591 100644 --- a/Shorewall/Perl/Shorewall/Policy.pm +++ b/Shorewall/Perl/Shorewall/Policy.pm @@ -372,7 +372,7 @@ sub default_policy( $$$ ) { my $policy = $policyref->{policy}; my $loglevel = $policyref->{loglevel}; - fatal_error "Internal error in default_policy()" unless $policyref; + assert( $policyref ); if ( $chainref eq $policyref ) { policy_rules $chainref , $policy, $loglevel , $default, $config{MULTICAST}; diff --git a/Shorewall/Perl/Shorewall/Proc.pm b/Shorewall/Perl/Shorewall/Proc.pm index 26385ebbc..fe474d12d 100644 --- a/Shorewall/Perl/Shorewall/Proc.pm +++ b/Shorewall/Perl/Shorewall/Proc.pm @@ -71,7 +71,7 @@ sub setup_arp_filtering() { my $file = "/proc/sys/net/ipv4/conf/$interface/arp_ignore"; my $value = get_interface_option $interface, 'arp_ignore'; - fatal_error "Internal Error in setup_arp_filtering()" unless defined $value; + assert( defined $value ); emit ( "if [ -f $file ]; then", " echo $value > $file"); diff --git a/Shorewall/Perl/Shorewall/Rules.pm b/Shorewall/Perl/Shorewall/Rules.pm index 7f05df5ef..46f14d4f2 100644 --- a/Shorewall/Perl/Shorewall/Rules.pm +++ b/Shorewall/Perl/Shorewall/Rules.pm @@ -1643,7 +1643,7 @@ sub generate_matrix() { return $chain if $chainref && $chainref->{referenced}; return 'ACCEPT' if $zone eq $zone1; - fatal_error "Internal Error in rules_target()" unless $chainref; + assert( $chainref ); if ( $chainref->{policy} ne 'CONTINUE' ) { my $policyref = $filter_table->{$chainref->{policychain}}; diff --git a/Shorewall/Perl/Shorewall/Tc.pm b/Shorewall/Perl/Shorewall/Tc.pm index 9e7e593c9..f465d46d9 100644 --- a/Shorewall/Perl/Shorewall/Tc.pm +++ b/Shorewall/Perl/Shorewall/Tc.pm @@ -495,7 +495,7 @@ sub dev_by_number( $ ) { $dev = $devnums[ $devnum ]; fatal_error "Undefined INTERFACE number ($_[0])" unless defined $dev; $devref = $tcdevices{$dev}; - fatal_error "Internal Error in dev_by_number()" unless $devref; + assert( $devref ); } else { $devref = $tcdevices{$dev}; fatal_error "Unknown INTERFACE ($dev)" unless $devref; diff --git a/Shorewall/Perl/Shorewall/Zones.pm b/Shorewall/Perl/Shorewall/Zones.pm index dc35f0968..be48a8a01 100644 --- a/Shorewall/Perl/Shorewall/Zones.pm +++ b/Shorewall/Perl/Shorewall/Zones.pm @@ -344,7 +344,7 @@ sub determine_zones() } } - fatal_error "Internal error in determine_zones()" unless scalar @zones == scalar @z; + assert( scalar @zones == scalar @z ); }