Skip to content

Commit d61ae9d

Browse files
hopehookgopherbot
authored andcommitted
mime/multipart: fix Reader.ReadForm(math.MaxInt64) overflow
Because "CopyN" will read one more byte, which will cause us to overflow when calling "Reader.ReadForm(math.MaxInt64)". So we should check if the parameter exceeds "math.MaxInt64" to avoid returning no data. Fixes #58384. Change-Id: I30088ce6468176b21e4a9a0b8b6080f2986dda23 Reviewed-on: https://go-review.googlesource.com/c/go/+/467557 TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: hopehook <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent eee2697 commit d61ae9d

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/mime/multipart/formdata.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ func (r *Reader) readForm(maxMemory int64) (_ *Form, err error) {
7777
// unconfigurable 10 MB added on to maxMemory, is unfortunate but difficult to change
7878
// within the constraints of the API as documented.
7979
maxFileMemoryBytes := maxMemory
80+
if maxFileMemoryBytes == math.MaxInt64 {
81+
maxFileMemoryBytes--
82+
}
8083
maxMemoryBytes := maxMemory + int64(10<<20)
8184
if maxMemoryBytes <= 0 {
8285
if maxMemory < 0 {

src/mime/multipart/formdata_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ func TestReadFormWithNamelessFile(t *testing.T) {
5555
}
5656
}
5757

58+
// Issue 58384: Handle ReadForm(math.MaxInt64)
59+
func TestReadFormWitFileNameMaxMemoryOverflow(t *testing.T) {
60+
b := strings.NewReader(strings.ReplaceAll(messageWithFileName, "\n", "\r\n"))
61+
r := NewReader(b, boundary)
62+
f, err := r.ReadForm(math.MaxInt64)
63+
if err != nil {
64+
t.Fatalf("ReadForm(MaxInt64): %v", err)
65+
}
66+
defer f.RemoveAll()
67+
68+
fd := testFile(t, f.File["filea"][0], "filea.txt", fileaContents)
69+
if _, ok := fd.(*os.File); ok {
70+
t.Error("file is *os.File, should not be")
71+
}
72+
fd.Close()
73+
}
74+
5875
// Issue 40430: Handle ReadForm(math.MaxInt64)
5976
func TestReadFormMaxMemoryOverflow(t *testing.T) {
6077
b := strings.NewReader(strings.ReplaceAll(messageWithTextContentType, "\n", "\r\n"))
@@ -66,6 +83,11 @@ func TestReadFormMaxMemoryOverflow(t *testing.T) {
6683
if f == nil {
6784
t.Fatal("ReadForm(MaxInt64): missing form")
6885
}
86+
defer f.RemoveAll()
87+
88+
if g, e := f.Value["texta"][0], textaValue; g != e {
89+
t.Errorf("texta value = %q, want %q", g, e)
90+
}
6991
}
7092

7193
func TestReadFormWithTextContentType(t *testing.T) {
@@ -122,6 +144,15 @@ Content-Type: text/plain
122144
--MyBoundary--
123145
`
124146

147+
const messageWithFileName = `
148+
--MyBoundary
149+
Content-Disposition: form-data; name="filea"; filename="filea.txt"
150+
Content-Type: text/plain
151+
152+
` + fileaContents + `
153+
--MyBoundary--
154+
`
155+
125156
const messageWithTextContentType = `
126157
--MyBoundary
127158
Content-Disposition: form-data; name="texta"

0 commit comments

Comments
 (0)