Explorar o código

Make AllocationSet.insert safer

Niko Kovacevic %!s(int64=5) %!d(string=hai) anos
pai
achega
1017d30716
Modificáronse 1 ficheiros con 10 adicións e 5 borrados
  1. 10 5
      pkg/kubecost/allocation.go

+ 10 - 5
pkg/kubecost/allocation.go

@@ -1218,16 +1218,21 @@ func (as *AllocationSet) Insert(that *Allocation) error {
 }
 
 func (as *AllocationSet) insert(that *Allocation, accumulate bool) error {
-	if as.IsEmpty() {
-		as.Lock()
-		as.allocations = map[string]*Allocation{}
-		as.idleKeys = map[string]bool{}
-		as.Unlock()
+	if as == nil {
+		return fmt.Errorf("cannot insert into nil AllocationSet")
 	}
 
 	as.Lock()
 	defer as.Unlock()
 
+	if as.allocations == nil {
+		as.allocations = map[string]*Allocation{}
+	}
+
+	if as.idleKeys == nil {
+		as.idleKeys = map[string]bool{}
+	}
+
 	// Add the given Allocation to the existing entry, if there is one;
 	// otherwise just set directly into allocations
 	if _, ok := as.allocations[that.Name]; !ok {