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

Fix body_size_limit negative handling #838

Merged
merged 1 commit into from
Oct 19, 2021

Conversation

mem
Copy link
Contributor

@mem mem commented Oct 19, 2021

When body_size_limit is 0 (either explicitly or implicitly because it
wasn't specified) or less, we are setting it to math.MaxInt64. It turns
out that the implementation in http.MaxBytesReader tries to add 1 to the
specified value, and it wraps around. After that, it tries to use the
result to index an slice, causing it to panic.

Work around this by setting the limit to math.MaxInt64 - 1.

Also, if body_size_limit is exactly 0, leave it like that. That causes
the code to avoid setting up the limiter, saving some extra processing.

Closes: #837

Signed-off-by: Marcelo E. Magallon [email protected]

When body_size_limit is 0 (either explicitly or implicitly because it
wasn't specified) or less, we are setting it to math.MaxInt64. It turns
out that the implementation in http.MaxBytesReader tries to add 1 to the
specified value, and it wraps around. After that, it tries to use the
result to index an slice, causing it to panic.

Work around this by setting the limit to math.MaxInt64 - 1.

Also, if body_size_limit is exactly 0, leave it like that. That causes
the code to avoid setting up the limiter, saving some extra processing.

Signed-off-by: Marcelo E. Magallon <[email protected]>
@mem mem merged commit 45d2cd6 into master Oct 19, 2021
@mem mem deleted the mem/off_by_one_error_in_body_size_limit branch October 19, 2021 22:37
@roidelapluie
Copy link
Member

Merge without review?

@mem
Copy link
Contributor Author

mem commented Oct 19, 2021

Merge without review?

Sorry, I did not want to leave a panic in the code hanging around like that. I'm more than glad to make any changes.

@mem
Copy link
Contributor Author

mem commented Oct 19, 2021

For the record, I'm looking at why the existing tests did not catch this, as the path that was failing is the default one. I in particular missed it when testing by hand because I did not connect the dots in my head that when I changed < to <= in the original review I was changing the behavior for the default case, and I felt safe seeing that the tests were all passing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unset body_size_limit option causes panic
2 participants