Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent overflow and panic on large HTTP responses #1351

Merged
merged 1 commit into from Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions http.go
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
"math"
"mime/multipart"
"net"
"os"
Expand Down Expand Up @@ -2278,5 +2279,10 @@ func round2(n int) int {
x |= x >> 8
x |= x >> 16

// Make sure we don't return 0 due to overflow, even on 32 bit systems
if x >= uint32(math.MaxInt32) {
return math.MaxInt32
}

return int(x + 1)
}
2 changes: 2 additions & 0 deletions http_test.go
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"io/ioutil"
"math"
"mime/multipart"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -1963,6 +1964,7 @@ func TestRound2(t *testing.T) {
testRound2(t, 8, 8)
testRound2(t, 9, 16)
testRound2(t, 0x10001, 0x20000)
testRound2(t, math.MaxInt32-1, math.MaxInt32)
}

func testRound2(t *testing.T, n, expectedRound2 int) {
Expand Down