From 50127014a2b863fb4b5e31f5f61230b22f371e63 Mon Sep 17 00:00:00 2001
From: Stephen Hemminger <stephen.hemminger@vyatta.com>
Date: Tue, 5 Feb 2008 10:50:00 -0800
Subject: handle undefined values better

If getprotocol or getdsfield is called with undefined value, then
return undefined.
---
 scripts/VyattaQosUtil.pm | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/scripts/VyattaQosUtil.pm b/scripts/VyattaQosUtil.pm
index ef2e916..3a115db 100644
--- a/scripts/VyattaQosUtil.pm
+++ b/scripts/VyattaQosUtil.pm
@@ -2,6 +2,7 @@ package VyattaQosUtil;
 use POSIX;
 require Exporter;
 @EXPORT	= qw/getRate getSize getProtocol getDsfield interfaceRate/;
+use strict;
 
 sub get_num  {
     my ($str) = @_;
@@ -77,17 +78,18 @@ sub getSize {
 }
 
 sub getProtocol {
-    my ($p) = @_;
+    my ($str) = @_;
 
-    if ($p =~ /^([0-9]+)|(0x[0-9a-fA-F]+)$/) {
-	if ($p < 0 || $p > 255) {
-	    die "$p is not a valid protocol number\n";
+    defined $str or return;
+    if ($str =~ /^([0-9]+)|(0x[0-9a-fA-F]+)$/) {
+	if ($str < 0 || $str > 255) {
+	    die "$str is not a valid protocol number\n";
 	}
-	return $p;
+	return $str;
     }
 
-    my ($name, $aliases, $proto) =  getprotobyname($p);
-    (defined $proto)  or die "\"$p\" unknown protocol\n";
+    my ($name, $aliases, $proto) =  getprotobyname($str);
+    (defined $proto)  or die "\"$str\" unknown protocol\n";
     return $proto;
 }
 
@@ -98,6 +100,8 @@ sub getDsfield {
     my $match = undef;
     my $dsFileName = '/etc/iproute2/rt_dsfield';
     
+    defined $str or return;
+
     if ($str =~ /^([0-9]+)|(0x[0-9a-fA-F]+)$/) {
 	if ($str < 0 || $str > 255) {
 	    die "$str is not a valid dsfield value\n";
@@ -109,9 +113,9 @@ sub getDsfield {
     while (<$ds>) {
 	next if /^#/;
 	chomp;
-	my @fields = split;
-	if ($str eq $fields[1]) {
-	    $match = $fields[0];
+	my ($value, $name) = split;
+	if ($str eq $name) {
+	    $match = $value;
 	    last;
 	}
     }
-- 
cgit v1.2.3