]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.r57] http: fix FormFile nil pointer dereference on missing multipart...
authorRuss Cox <rsc@golang.org>
Wed, 4 May 2011 03:53:31 +0000 (23:53 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 4 May 2011 03:53:31 +0000 (23:53 -0400)
««« CL 4463042 / 6294ca33c042
http: fix FormFile nil pointer dereference on missing  multipart form

R=rsc
CC=golang-dev
https://golang.org/cl/4463042
»»»

R=adg
CC=golang-dev
https://golang.org/cl/4467042

src/pkg/http/request.go
src/pkg/http/request_test.go

index b8e9a21423f6a53a1b4823258caa4f7b8c3b2b8a..4852ca3e1f017ad159e647e99e11c49cf137b5e1 100644 (file)
@@ -715,9 +715,11 @@ func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, o
                        return nil, nil, err
                }
        }
-       if fhs := r.MultipartForm.File[key]; len(fhs) > 0 {
-               f, err := fhs[0].Open()
-               return f, fhs[0], err
+       if r.MultipartForm != nil && r.MultipartForm.File != nil {
+               if fhs := r.MultipartForm.File[key]; len(fhs) > 0 {
+                       f, err := fhs[0].Open()
+                       return f, fhs[0], err
+               }
        }
        return nil, nil, ErrMissingFile
 }
index f982471d8da73a2361680b99a5a1a45fc31caccf..f79d3a24240c390e8a70101c7d1dc9bb92bd09e7 100644 (file)
@@ -200,6 +200,29 @@ func TestMultipartRequestAuto(t *testing.T) {
        validateTestMultipartContents(t, req, true)
 }
 
+func TestEmptyMultipartRequest(t *testing.T) {
+       // Test that FormValue and FormFile automatically invoke
+       // ParseMultipartForm and return the right values.
+       req, err := NewRequest("GET", "/", nil)
+       if err != nil {
+               t.Errorf("NewRequest err = %q", err)
+       }
+       testMissingFile(t, req)
+}
+
+func testMissingFile(t *testing.T, req *Request) {
+       f, fh, err := req.FormFile("missing")
+       if f != nil {
+               t.Errorf("FormFile file = %q, want nil", f, nil)
+       }
+       if fh != nil {
+               t.Errorf("FormFile file header = %q, want nil", fh, nil)
+       }
+       if err != ErrMissingFile {
+               t.Errorf("FormFile err = %q, want nil", err, ErrMissingFile)
+       }
+}
+
 func newTestMultipartRequest(t *testing.T) *Request {
        b := bytes.NewBufferString(strings.Replace(message, "\n", "\r\n", -1))
        req, err := NewRequest("POST", "/", b)
@@ -218,6 +241,9 @@ func validateTestMultipartContents(t *testing.T, req *Request, allMem bool) {
        if g, e := req.FormValue("texta"), textaValue; g != e {
                t.Errorf("texta value = %q, want %q", g, e)
        }
+       if g := req.FormValue("missing"); g != "" {
+               t.Errorf("missing value = %q, want empty string", g)
+       }
 
        assertMem := func(n string, fd multipart.File) {
                if _, ok := fd.(*os.File); ok {
@@ -234,6 +260,8 @@ func validateTestMultipartContents(t *testing.T, req *Request, allMem bool) {
                        t.Errorf("fileb has unexpected underlying type %T", fd)
                }
        }
+
+       testMissingFile(t, req)
 }
 
 func testMultipartFile(t *testing.T, req *Request, key, expectFilename, expectContent string) multipart.File {