1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Avoid nil pointer dereference while creating a container with an empty Config

Signed-off-by: Antonio Murdaca <runcom@linux.com>
This commit is contained in:
Antonio Murdaca 2015-06-06 18:41:42 +02:00
parent 3c132ff62d
commit 4ce817796e
5 changed files with 25 additions and 4 deletions

View file

@ -658,10 +658,6 @@ func (s *Server) postCommit(version version.Version, w http.ResponseWriter, r *h
return err
}
if c == nil {
c = &runconfig.Config{}
}
containerCommitConfig := &daemon.ContainerCommitConfig{
Pause: pause,
Repo: r.Form.Get("repo"),

View file

@ -221,6 +221,10 @@ func Commit(d *daemon.Daemon, name string, c *daemon.ContainerCommitConfig) (str
return "", err
}
if c.Config == nil {
c.Config = &runconfig.Config{}
}
newConfig, err := BuildFromConfig(d, c.Config, c.Changes)
if err != nil {
return "", err

View file

@ -15,6 +15,10 @@ import (
)
func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hostConfig *runconfig.HostConfig) (string, []string, error) {
if config == nil {
return "", nil, fmt.Errorf("Config cannot be empty in order to create a container")
}
warnings, err := daemon.verifyHostConfig(hostConfig)
if err != nil {
return "", warnings, err

View file

@ -836,6 +836,19 @@ func (s *DockerSuite) TestContainerApiCreate(c *check.C) {
}
}
func (s *DockerSuite) TestContainerApiCreateEmptyConfig(c *check.C) {
config := map[string]interface{}{}
status, b, err := sockRequest("POST", "/containers/create", config)
c.Assert(err, check.IsNil)
c.Assert(status, check.Equals, http.StatusInternalServerError)
expected := "Config cannot be empty in order to create a container\n"
if body := string(b); body != expected {
c.Fatalf("Expected to get %q, got %q", expected, body)
}
}
func (s *DockerSuite) TestContainerApiCreateWithHostName(c *check.C) {
hostName := "test-host"
config := map[string]interface{}{

View file

@ -143,6 +143,10 @@ func (c ContainerConfigWrapper) HostConfig() *HostConfig {
return c.hostConfigWrapper.GetHostConfig()
}
// DecodeContainerConfig decodes a json encoded config into a ContainerConfigWrapper
// struct and returns both a Config and an HostConfig struct
// Be aware this function is not checking whether the resulted structs are nil,
// it's your business to do so
func DecodeContainerConfig(src io.Reader) (*Config, *HostConfig, error) {
decoder := json.NewDecoder(src)