Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upFix substraction with overflow in range request #23036
Conversation
highfive
commented
Mar 14, 2019
|
Heads up! This PR modifies the following files:
|
highfive
commented
Mar 14, 2019
|
@jdm r? Sorry I had missed your self-assignment to the issue... |
|
@bors-servo try=wpt |
Checked arithmetics in cache range request <!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: --> - [ ] `./mach build -d` does not report any errors - [ ] `./mach test-tidy` does not report any errors - [ ] These changes fix #23030 (GitHub issue number if applicable) <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because ___ <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23036) <!-- Reviewable:end -->
|
|
c0f2cec
to
9ae43e8
|
Ok I think I've addressed all potential overflowing operations, maybe a bit over the top for some, but better safe than sorry! |
|
These changes achieve the goal of removing over/underflow hazards, but lots of checked operations make the code harder to follow in my opinion. I think we can simplify some of the changes by establishing preconditions when extracting the ContentRange values like this: match (range.bytes_range(), range.bytes_len()) {
(Some(bytes_range), Some(total)) if total > 0 => (bytes_range.0, bytes_range.1, total),
_ => continue,
} |
| _ => continue, | ||
| } | ||
| }; | ||
| if res_beginning_minus_one < beginning && res_end_plus_one > end { |
This comment has been minimized.
This comment has been minimized.
jdm
Mar 14, 2019
Member
Instead of this block, can't we do if res_beginning <= beginning && res_end >= end?
| let resource_body = &*partial_resource.body.lock().unwrap(); | ||
| let requested = match resource_body { | ||
| &ResponseBody::Done(ref body) => { | ||
| let b = beginning as usize - res_beginning as usize; | ||
| let e = end as usize - res_beginning as usize + 1; | ||
| let (b, e) = { |
This comment has been minimized.
This comment has been minimized.
jdm
Mar 14, 2019
Member
Since we have already established that res_beginning <= beginning we do not need a checked_sub. Similarly, if we establish that end >= res_beginning then we don't need to check that operation either.
| @@ -474,11 +498,22 @@ fn handle_range_request( | |||
| } else { | |||
| continue; | |||
| }; | |||
| if res_beginning < beginning && res_end == total - 1 { | |||
| let total_minus_one = { | |||
This comment has been minimized.
This comment has been minimized.
jdm
Mar 14, 2019
Member
If we check that total != 0 earlier then we don't need a checked subtraction here.
| let resource_body = &*partial_resource.body.lock().unwrap(); | ||
| let requested = match resource_body { | ||
| &ResponseBody::Done(ref body) => { | ||
| let from_byte = beginning as usize - res_beginning as usize; | ||
| let from_byte = { | ||
| match beginning.checked_sub(res_beginning) { |
This comment has been minimized.
This comment has been minimized.
jdm
Mar 14, 2019
Member
Since we have established that res_beginning < beginning there is no need to check this operation.
| @@ -519,7 +554,23 @@ fn handle_range_request( | |||
| } else { | |||
| continue; | |||
| }; | |||
| if (total - res_beginning) > (offset - 1) && (total - res_end) < offset + 1 { | |||
| let (total_minus_res_beginning, total_minus_res_end) = { | |||
This comment has been minimized.
This comment has been minimized.
jdm
Mar 14, 2019
Member
If we check that total >= res_beginning and total >= res_end then we don't need these checked operations.
0d8753b
to
21470e0
|
@jdm Ok, much simpler indeed! |
|
@bors-servo r+ |
|
|
Fix substraction with overflow in range request <!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: --> - [ ] `./mach build -d` does not report any errors - [ ] `./mach test-tidy` does not report any errors - [ ] These changes fix #23030 (GitHub issue number if applicable) <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because ___ <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23036) <!-- Reviewable:end -->
|
|
gterzian commentedMar 14, 2019
•
edited by SimonSapin
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsThis change is