Browse code

Fix panic caused by invalid ip range

xiangpengzhao authored on 2016/08/04 12:12:23
Showing 2 changed files
... ...
@@ -23,11 +23,17 @@ func ValidateIPAddressRange(iprange string) error {
23 23
 	if strings.Count(iprange, "-") < 1 {
24 24
 		return ValidateIPAddress(iprange)
25 25
 	}
26
+	if strings.Count(iprange, "-") > 1 {
27
+		return fmt.Errorf("invalid IP range format: %s", iprange)
28
+	}
26 29
 
27 30
 	// Its an IP range of the form: n.n.n.n-n
28 31
 	rangeLimits := strings.Split(iprange, "-")
29 32
 	startIP := rangeLimits[0]
30 33
 	parts := strings.Split(startIP, ".")
34
+	if len(parts) < 4 {
35
+		return fmt.Errorf("invalid IP range start fomat: %s", startIP)
36
+	}
31 37
 	rangeStart := parts[3]
32 38
 	rangeEnd := rangeLimits[1]
33 39
 	if err := ValidateIPAddress(startIP); err != nil {
... ...
@@ -17,7 +17,7 @@ func TestValidateIPAddress(t *testing.T) {
17 17
 
18 18
 	invalidIPs := []string{"1.1.1.256", "256.256.256.256",
19 19
 		"1024.512.256.128", "a.b.c.d", "1.2.3.4.abc", "5.6.7.8def",
20
-		"a.12.13.14", "9999.888.77.6",
20
+		"a.12.13.14", "9999.888.77.6", "1.2.3",
21 21
 	}
22 22
 
23 23
 	for _, ip := range invalidIPs {
... ...
@@ -42,7 +42,7 @@ func TestValidateIPAddressRange(t *testing.T) {
42 42
 	invalidRanges := []string{"1.1.1.256-250", "1.1.1.1-0",
43 43
 		"1.1.1.5-1", "255.255.255.255-259", "1024.512.256.128-255",
44 44
 		"a.b.c.d-e", "1.2.3.4.abc-def", "5.6.7.8def-1.2.3.4abc",
45
-		"a.12.13.14-55", "9999.888.77.6-66",
45
+		"a.12.13.14-55", "9999.888.77.6-66", "1.2.3.4-5-6", "1.2.3-4",
46 46
 	}
47 47
 
48 48
 	for _, iprange := range invalidRanges {
... ...
@@ -72,7 +72,7 @@ func TestValidateVirtualIPs(t *testing.T) {
72 72
 		"1.2.3.4-5,1024.512.256.128-255", "1.1.1.1,a.b.c.d-e",
73 73
 		"a.b.c.d-e,5.4.3.2", "1.2.3.4.abc-def",
74 74
 		"5.6.7.8def-1.2.3.4abc", "4.1.1.1,a.12.13.14-55",
75
-		"8.8.8.8,9999.888.77.6-66,4.4.4.4-8",
75
+		"8.8.8.8,9999.888.77.6-66,4.4.4.4-8", "1.2.3.4-5-6", "1.2.3-4",
76 76
 	}
77 77
 
78 78
 	for _, vips := range invalidVIPs {
... ...
@@ -103,7 +103,7 @@ func TestValidateCmdOptionsVIPs(t *testing.T) {
103 103
 		"1.2.3.4-5,1024.512.256.128-255", "1.1.1.1,a.b.c.d-e",
104 104
 		"a.b.c.d-e,5.4.3.2", "1.2.3.4.abc-def",
105 105
 		"5.6.7.8def-1.2.3.4abc", "4.1.1.1,a.12.13.14-55",
106
-		"8.8.8.8,9999.888.77.6-66,4.4.4.4-8",
106
+		"8.8.8.8,9999.888.77.6-66,4.4.4.4-8", "1.2.3.4-5-6", "1.2.3-4",
107 107
 	}
108 108
 
109 109
 	for _, vips := range invalidVIPs {