Skip to content

Commit

Permalink
net/http: don't panic on very large MaxBytesReaderLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
cuiweixie committed Aug 12, 2022
1 parent 8cb350d commit a33fe7e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/net/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,8 @@ func (l *maxBytesReader) Read(p []byte) (n int, err error) {
// If they asked for a 32KB byte read but only 5 bytes are
// remaining, no need to read 32KB. 6 bytes will answer the
// question of the whether we hit the limit or go past it.
if int64(len(p)) > l.n+1 {
// 0 < len(p) < 2^63
if int64(len(p))-1 > l.n {
p = p[:l.n+1]
}
n, err = l.r.Read(p)
Expand Down
6 changes: 6 additions & 0 deletions src/net/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,12 @@ func TestMaxBytesReaderDifferentLimits(t *testing.T) {
wantN: len(testStr),
wantErr: false,
},
10: { /* Issue 54408 */
limit: int64(1<<63-1),
lenP: len(testStr),
wantN: len(testStr),
wantErr: false,
},
}
for i, tt := range tests {
rc := MaxBytesReader(nil, io.NopCloser(strings.NewReader(testStr)), tt.limit)
Expand Down

0 comments on commit a33fe7e

Please sign in to comment.