Browse code

Check for nil before using HostConfig to adjustCpuShares

Fix #14915. Add unit test for #14915.
Thanks @runcom for the test case: when the client calls 1.18 api
version w/o hostconfig it results in a nil pointer dereference.

Signed-off-by: Stephen Rust <srust@blockbridge.com>

Stephen Rust authored on 2015/07/25 20:39:13
Showing 2 changed files
... ...
@@ -109,7 +109,7 @@ func allocateDaemonPort(addr string) error {
109 109
 
110 110
 func adjustCpuShares(version version.Version, hostConfig *runconfig.HostConfig) {
111 111
 	if version.LessThan("1.19") {
112
-		if hostConfig.CpuShares > 0 {
112
+		if hostConfig != nil && hostConfig.CpuShares > 0 {
113 113
 			// Handle unsupported CpuShares
114 114
 			if hostConfig.CpuShares < linuxMinCpuShares {
115 115
 				logrus.Warnf("Changing requested CpuShares of %d to minimum allowed of %d", hostConfig.CpuShares, linuxMinCpuShares)
... ...
@@ -1687,3 +1687,13 @@ func (s *DockerSuite) TestPostContainersStartWithLinksInHostConfigIdLinked(c *ch
1687 1687
 	c.Assert(res.StatusCode, check.Equals, http.StatusNoContent)
1688 1688
 	b.Close()
1689 1689
 }
1690
+
1691
+// #14915
1692
+func (s *DockerSuite) TestContainersApiCreateNoHostConfig118(c *check.C) {
1693
+	config := struct {
1694
+		Image string
1695
+	}{"busybox"}
1696
+	status, _, err := sockRequest("POST", "/v1.18/containers/create", config)
1697
+	c.Assert(err, check.IsNil)
1698
+	c.Assert(status, check.Equals, http.StatusCreated)
1699
+}