Browse code

api: exec resize: improve errors for invalid width/height

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2024/10/16 23:17:48
Showing 2 changed files
... ...
@@ -5,7 +5,6 @@ import (
5 5
 	"fmt"
6 6
 	"io"
7 7
 	"net/http"
8
-	"strconv"
9 8
 
10 9
 	"github.com/containerd/log"
11 10
 	"github.com/docker/docker/api/server/httputils"
... ...
@@ -15,6 +14,7 @@ import (
15 15
 	"github.com/docker/docker/api/types/versions"
16 16
 	"github.com/docker/docker/errdefs"
17 17
 	"github.com/docker/docker/pkg/stdcopy"
18
+	"github.com/pkg/errors"
18 19
 )
19 20
 
20 21
 func (c *containerRouter) getExecByID(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
... ...
@@ -158,14 +158,14 @@ func (c *containerRouter) postContainerExecResize(ctx context.Context, w http.Re
158 158
 	if err := httputils.ParseForm(r); err != nil {
159 159
 		return err
160 160
 	}
161
-	height, err := strconv.Atoi(r.Form.Get("h"))
161
+	height, err := httputils.Uint32Value(r, "h")
162 162
 	if err != nil {
163
-		return errdefs.InvalidParameter(err)
163
+		return errdefs.InvalidParameter(errors.Wrapf(err, "invalid resize height %q", r.Form.Get("h")))
164 164
 	}
165
-	width, err := strconv.Atoi(r.Form.Get("w"))
165
+	width, err := httputils.Uint32Value(r, "w")
166 166
 	if err != nil {
167
-		return errdefs.InvalidParameter(err)
167
+		return errdefs.InvalidParameter(errors.Wrapf(err, "invalid resize width %q", r.Form.Get("w")))
168 168
 	}
169 169
 
170
-	return c.backend.ContainerExecResize(vars["name"], height, width)
170
+	return c.backend.ContainerExecResize(vars["name"], int(height), int(width))
171 171
 }
... ...
@@ -159,35 +159,59 @@ func TestExecResize(t *testing.T) {
159 159
 				doc:    "unset height",
160 160
 				height: valueNotSet,
161 161
 				width:  "100",
162
-				expErr: `strconv.Atoi: parsing "": invalid syntax`,
162
+				expErr: `invalid resize height "": invalid syntax`,
163 163
 			},
164 164
 			{
165 165
 				doc:    "unset width",
166 166
 				height: "100",
167 167
 				width:  valueNotSet,
168
-				expErr: `strconv.Atoi: parsing "": invalid syntax`,
168
+				expErr: `invalid resize width "": invalid syntax`,
169 169
 			},
170 170
 			{
171 171
 				doc:    "empty height",
172 172
 				width:  "100",
173
-				expErr: `strconv.Atoi: parsing "": invalid syntax`,
173
+				expErr: `invalid resize height "": invalid syntax`,
174 174
 			},
175 175
 			{
176 176
 				doc:    "empty width",
177 177
 				height: "100",
178
-				expErr: `strconv.Atoi: parsing "": invalid syntax`,
178
+				expErr: `invalid resize width "": invalid syntax`,
179 179
 			},
180 180
 			{
181 181
 				doc:    "non-numeric height",
182 182
 				height: "not-a-number",
183 183
 				width:  "100",
184
-				expErr: `strconv.Atoi: parsing "not-a-number": invalid syntax`,
184
+				expErr: `invalid resize height "not-a-number": invalid syntax`,
185 185
 			},
186 186
 			{
187 187
 				doc:    "non-numeric width",
188 188
 				height: "100",
189 189
 				width:  "not-a-number",
190
-				expErr: `strconv.Atoi: parsing "not-a-number": invalid syntax`,
190
+				expErr: `invalid resize width "not-a-number": invalid syntax`,
191
+			},
192
+			{
193
+				doc:    "negative height",
194
+				height: "-100",
195
+				width:  "100",
196
+				expErr: `invalid resize height "-100": value out of range`,
197
+			},
198
+			{
199
+				doc:    "negative width",
200
+				height: "100",
201
+				width:  "-100",
202
+				expErr: `invalid resize width "-100": value out of range`,
203
+			},
204
+			{
205
+				doc:    "out of range height",
206
+				height: "4294967296", // math.MaxUint32+1
207
+				width:  "100",
208
+				expErr: `invalid resize height "4294967296": value out of range`,
209
+			},
210
+			{
211
+				doc:    "out of range width",
212
+				height: "100",
213
+				width:  "4294967296", // math.MaxUint32+1
214
+				expErr: `invalid resize width "4294967296": value out of range`,
191 215
 			},
192 216
 		}
193 217
 		for _, tc := range sizes {