This fix tries to fix the http panics caused by container deletion
with empty names in #22210.
The issue was because when an empty string was passed, `GetByName()`
tried to access the first element of the name string without checking
the length. A length check has been added.
A test case for #22210 has been added.
This fix fixes #22210.
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
| ... | ... |
@@ -137,6 +137,10 @@ type Daemon struct {
|
| 137 | 137 |
// unique enough to only return a single container object |
| 138 | 138 |
// If none of these searches succeed, an error is returned |
| 139 | 139 |
func (daemon *Daemon) GetContainer(prefixOrName string) (*container.Container, error) {
|
| 140 |
+ if len(prefixOrName) == 0 {
|
|
| 141 |
+ return nil, errors.NewBadRequestError(fmt.Errorf("No container name or ID supplied"))
|
|
| 142 |
+ } |
|
| 143 |
+ |
|
| 140 | 144 |
if containerByID := daemon.containers.Get(prefixOrName); containerByID != nil {
|
| 141 | 145 |
// prefix is an exact match to a full container ID |
| 142 | 146 |
return containerByID, nil |
| ... | ... |
@@ -530,6 +534,9 @@ func (daemon *Daemon) newContainer(name string, config *containertypes.Config, i |
| 530 | 530 |
|
| 531 | 531 |
// GetByName returns a container given a name. |
| 532 | 532 |
func (daemon *Daemon) GetByName(name string) (*container.Container, error) {
|
| 533 |
+ if len(name) == 0 {
|
|
| 534 |
+ return nil, fmt.Errorf("No container name supplied")
|
|
| 535 |
+ } |
|
| 533 | 536 |
fullName := name |
| 534 | 537 |
if name[0] != '/' {
|
| 535 | 538 |
fullName = "/" + name |
| ... | ... |
@@ -1616,3 +1616,11 @@ func (s *DockerSuite) TestPostContainersCreateWithOomScoreAdjInvalidRange(c *che |
| 1616 | 1616 |
c.Fatalf("Expected output to contain %q, got %q", expected, string(b))
|
| 1617 | 1617 |
} |
| 1618 | 1618 |
} |
| 1619 |
+ |
|
| 1620 |
+// test case for #22210 where an emtpy container name caused panic. |
|
| 1621 |
+func (s *DockerSuite) TestContainerApiDeleteWithEmptyName(c *check.C) {
|
|
| 1622 |
+ status, out, err := sockRequest("DELETE", "/containers/", nil)
|
|
| 1623 |
+ c.Assert(err, checker.IsNil) |
|
| 1624 |
+ c.Assert(status, checker.Equals, http.StatusBadRequest) |
|
| 1625 |
+ c.Assert(string(out), checker.Contains, "No container name or ID supplied") |
|
| 1626 |
+} |