]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: fix data race on countReader.n
authorDave Cheney <dave@cheney.net>
Thu, 11 Oct 2012 22:17:56 +0000 (09:17 +1100)
committerDave Cheney <dave@cheney.net>
Thu, 11 Oct 2012 22:17:56 +0000 (09:17 +1100)
Fixes #4220.

R=dvyukov, bradfitz
CC=golang-dev
https://golang.org/cl/6638053

src/pkg/net/http/serve_test.go

index 71b7b3fb6b7309c55a0c1136b00ae662a00510ff..a286edc72d464728c2d24a269570016bacb0c7dd 100644 (file)
@@ -1063,7 +1063,7 @@ type countReader struct {
 
 func (cr countReader) Read(p []byte) (n int, err error) {
        n, err = cr.r.Read(p)
-       *cr.n += int64(n)
+       atomic.AddInt64(cr.n, int64(n))
        return
 }
 
@@ -1081,8 +1081,8 @@ func TestRequestBodyLimit(t *testing.T) {
        }))
        defer ts.Close()
 
-       nWritten := int64(0)
-       req, _ := NewRequest("POST", ts.URL, io.LimitReader(countReader{neverEnding('a'), &nWritten}, limit*200))
+       nWritten := new(int64)
+       req, _ := NewRequest("POST", ts.URL, io.LimitReader(countReader{neverEnding('a'), nWritten}, limit*200))
 
        // Send the POST, but don't care it succeeds or not.  The
        // remote side is going to reply and then close the TCP
@@ -1095,7 +1095,7 @@ func TestRequestBodyLimit(t *testing.T) {
        // the remote side hung up on us before we wrote too much.
        _, _ = DefaultClient.Do(req)
 
-       if nWritten > limit*100 {
+       if atomic.LoadInt64(nWritten) > limit*100 {
                t.Errorf("handler restricted the request body to %d bytes, but client managed to write %d",
                        limit, nWritten)
        }