import (
"bytes"
"errors"
- "fmt"
"io"
"io/ioutil"
+ "math"
"net/textproto"
"os"
)
// Reserve an additional 10 MB for non-file parts.
maxValueBytes := maxMemory + int64(10<<20)
if maxValueBytes <= 0 {
- return nil, fmt.Errorf("multipart: integer overflow from maxMemory(%d) + 10MiB for non-file parts", maxMemory)
+ if maxMemory < 0 {
+ maxValueBytes = 0
+ } else {
+ maxValueBytes = math.MaxInt64
+ }
}
for {
p, err := r.NextPart()
}
}
-// Issue 40430: Ensure that we report integer overflows in additions of maxMemory,
-// instead of silently and subtly failing without indication.
+// Issue 40430: Handle ReadForm(math.MaxInt64)
func TestReadFormMaxMemoryOverflow(t *testing.T) {
b := strings.NewReader(strings.ReplaceAll(messageWithTextContentType, "\n", "\r\n"))
r := NewReader(b, boundary)
f, err := r.ReadForm(math.MaxInt64)
- if err == nil {
- t.Fatal("Unexpected a non-nil error")
- }
- if f != nil {
- t.Fatalf("Unexpected returned a non-nil form: %v\n", f)
+ if err != nil {
+ t.Fatalf("ReadForm(MaxInt64): %v", err)
}
- if g, w := err.Error(), "integer overflow from maxMemory"; !strings.Contains(g, w) {
- t.Errorf(`Error mismatch\n%q\ndid not contain\n%q`, g, w)
+ if f == nil {
+ t.Fatal("ReadForm(MaxInt64): missing form")
}
}
t.Fatal(err)
}
res.Body.Close()
- if g, w := res.StatusCode, StatusBadRequest; g != w {
+ if g, w := res.StatusCode, StatusOK; g != w {
t.Fatalf("Status code mismatch: got %d, want %d", g, w)
}
}