Browse Source

libnetwork: Controller: getKeys, getPrimaryKeyTag: prevent panic

Prevent potential panics if we don't have the expected number of keys
for the subsystem.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 1 year ago
parent
commit
603f49706e
1 changed files with 8 additions and 2 deletions
  1. 8 2
      libnetwork/agent.go

+ 8 - 2
libnetwork/agent.go

@@ -281,8 +281,11 @@ func (c *Controller) getKeys(subsys string) ([][]byte, []uint64) {
 		}
 		}
 	}
 	}
 
 
-	keys[0], keys[1] = keys[1], keys[0]
-	tags[0], tags[1] = tags[1], tags[0]
+	if len(keys) > 1 {
+		// TODO(thaJeztah): why are we swapping order here? This code was added in https://github.com/moby/libnetwork/commit/e83d68b7d1fd9c479120914024242238f791b4dc
+		keys[0], keys[1] = keys[1], keys[0]
+		tags[0], tags[1] = tags[1], tags[0]
+	}
 	return keys, tags
 	return keys, tags
 }
 }
 
 
@@ -298,6 +301,9 @@ func (c *Controller) getPrimaryKeyTag(subsys string) ([]byte, uint64, error) {
 			keys = append(keys, key)
 			keys = append(keys, key)
 		}
 		}
 	}
 	}
+	if len(keys) < 2 {
+		return nil, 0, fmt.Errorf("no primary key found for %s subsystem: %d keys found on controller, expected at least 2", subsys, len(keys))
+	}
 	return keys[1].Key, keys[1].LamportTime, nil
 	return keys[1].Key, keys[1].LamportTime, nil
 }
 }