net/http: multipart ReadForm close file after copy

Always close the file regardless of whether the copy succeeds or fails.
Pass along the close error if the copy succeeds

Fixes #16296

Change-Id: Ib394655b91d25750f029f17b3846d985f673fb50
Reviewed-on: https://go-review.googlesource.com/30410
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Michael Fraenkel 2016-10-05 11:27:34 -04:00 committed by Brad Fitzpatrick
parent f69991c17d
commit 7478ea5dba

View file

@ -79,8 +79,10 @@ func (r *Reader) readForm(maxMemory int64) (_ *Form, err error) {
if err != nil {
return nil, err
}
defer file.Close()
_, err = io.Copy(file, io.MultiReader(&b, p))
if cerr := file.Close(); err == nil {
err = cerr
}
if err != nil {
os.Remove(file.Name())
return nil, err