mirror of
https://github.com/k8snetworkplumbingwg/multus-cni.git
synced 2025-09-03 18:06:11 +00:00
Fix potential issue, slice bounds out of range error
This commit is contained in:
@@ -139,6 +139,7 @@ func deleteDefaultGWCacheBytes(cacheFile []byte, ipv4, ipv6 bool) ([]byte, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func deleteDefaultGWResultRoutes(routes []interface{}, dstGW string) ([]interface{}, error) {
|
func deleteDefaultGWResultRoutes(routes []interface{}, dstGW string) ([]interface{}, error) {
|
||||||
|
var newRoutes []interface{}
|
||||||
for i, r := range routes {
|
for i, r := range routes {
|
||||||
route, ok := r.(map[string]interface{})
|
route, ok := r.(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -150,12 +151,12 @@ func deleteDefaultGWResultRoutes(routes []interface{}, dstGW string) ([]interfac
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("wrong dst format: %v", route["dst"])
|
return nil, fmt.Errorf("wrong dst format: %v", route["dst"])
|
||||||
}
|
}
|
||||||
if dst == dstGW {
|
if dst != dstGW {
|
||||||
routes = append(routes[:i], routes[i+1:]...)
|
newRoutes = append(newRoutes, routes[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return routes, nil
|
return newRoutes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteDefaultGWResult(result map[string]interface{}, ipv4, ipv6 bool) (map[string]interface{}, error) {
|
func deleteDefaultGWResult(result map[string]interface{}, ipv4, ipv6 bool) (map[string]interface{}, error) {
|
||||||
|
@@ -1421,3 +1421,23 @@ var _ = Describe("netutil cnicache function testing", func() {
|
|||||||
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
var _ = Describe("other function unit testing", func() {
|
||||||
|
It("deleteDefaultGWResultRoutes with invalid config", func() {
|
||||||
|
cniRouteConfig := []byte(`[
|
||||||
|
{ "dst": "0.0.0.0/0", "gw": "10.1.1.1" },
|
||||||
|
{ "dst": "10.1.1.0/24" },
|
||||||
|
{ "dst": "0.0.0.0/0", "gw": "10.1.1.1" }
|
||||||
|
]`)
|
||||||
|
|
||||||
|
var routes []interface{}
|
||||||
|
err := json.Unmarshal(cniRouteConfig, &routes)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
newRoute, err := deleteDefaultGWResultRoutes(routes, "0.0.0.0/0")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
routeJSON, err := json.Marshal(newRoute)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(routeJSON).Should(MatchJSON(`[{"dst":"10.1.1.0/24"}]`))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
Reference in New Issue
Block a user