Network-functions: add check if variables are defined

Message ID 1510062807-12906-1-git-send-email-alexander.marx@ipfire.org
State Dropped
Headers show
Series Network-functions: add check if variables are defined | expand

Commit Message

Alexander Marx Nov. 8, 2017, 12:53 a.m. UTC
in function network_equal and network2bin a check for undefined variables were missing.
added them.
---
 config/cfgroot/network-functions.pl | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

Comments

Michael Tremer Nov. 8, 2017, 3:20 a.m. UTC | #1
Hi,

this patch conflicted with the previous one that you sent today. It would have
been better to send them all together in one patchset.

It also had some whitespace errors.

However, I edited this and merged it.

-Michael

On Tue, 2017-11-07 at 14:53 +0100, Alexander Marx wrote:
> in function network_equal and network2bin a check for undefined variables were
> missing.
> added them.
> ---
>  config/cfgroot/network-functions.pl | 16 ++++++++++++----
>  1 file changed, 12 insertions(+), 4 deletions(-)
> 
> diff --git a/config/cfgroot/network-functions.pl b/config/cfgroot/network-
> functions.pl
> index 93b8190..9e50d7c 100644
> --- a/config/cfgroot/network-functions.pl
> +++ b/config/cfgroot/network-functions.pl
> @@ -108,10 +108,14 @@ sub network_equal {
>  	my $network1 = shift;
>  	my $network2 = shift;
>  
> -	my $bin1 = &network2bin($network1);
> -	my $bin2 = &network2bin($network2);
> +	my @bin1 = &network2bin($network1);
> +	my @bin2 = &network2bin($network2);
>  
> -	if ($bin1 eq $bin2) {
> +	if(!defined $bin1 || !defined $bin2){
> +		return undef;
> +	}
> +
> +	if ($bin1[0] eq $bin2[0] && $bin1[1] eq $bin2[1]){
>  		return 1;
>  	}
>  
> @@ -132,6 +136,10 @@ sub network2bin($) {
>  
>  	my $address_bin = &ip2bin($address);
>  	my $netmask_bin = &ip2bin($netmask);
> +	
> +	if (!defined $address_bin || !defined $netmask_bin){
> +		return undef;
> +	}
>  
>  	my $network_start = $address_bin & $netmask_bin;
>  
> @@ -457,7 +465,7 @@ sub testsuite() {
>  	assert(!$result);
>  
>  	$result = &network_equal("192.168.0.1/24", "192.168.0.XXX/24");
> -	assert($result);
> +	assert(!$result);
>  
>  	$result = &ip_address_in_network("10.0.1.4", "10.0.0.0/8");
>  	assert($result);

Patch

diff --git a/config/cfgroot/network-functions.pl b/config/cfgroot/network-functions.pl
index 93b8190..9e50d7c 100644
--- a/config/cfgroot/network-functions.pl
+++ b/config/cfgroot/network-functions.pl
@@ -108,10 +108,14 @@  sub network_equal {
 	my $network1 = shift;
 	my $network2 = shift;
 
-	my $bin1 = &network2bin($network1);
-	my $bin2 = &network2bin($network2);
+	my @bin1 = &network2bin($network1);
+	my @bin2 = &network2bin($network2);
 
-	if ($bin1 eq $bin2) {
+	if(!defined $bin1 || !defined $bin2){
+		return undef;
+	}
+
+	if ($bin1[0] eq $bin2[0] && $bin1[1] eq $bin2[1]){
 		return 1;
 	}
 
@@ -132,6 +136,10 @@  sub network2bin($) {
 
 	my $address_bin = &ip2bin($address);
 	my $netmask_bin = &ip2bin($netmask);
+	
+	if (!defined $address_bin || !defined $netmask_bin){
+		return undef;
+	}
 
 	my $network_start = $address_bin & $netmask_bin;
 
@@ -457,7 +465,7 @@  sub testsuite() {
 	assert(!$result);
 
 	$result = &network_equal("192.168.0.1/24", "192.168.0.XXX/24");
-	assert($result);
+	assert(!$result);
 
 	$result = &ip_address_in_network("10.0.1.4", "10.0.0.0/8");
 	assert($result);