Chenjp commented on code in PR #782:
URL: https://github.com/apache/tomcat/pull/782#discussion_r1857791659
##########
java/org/apache/catalina/servlets/DefaultServlet.java:
##########
@@ -1231,10 +1231,25 @@ private static boolean validate(ContentRange range) {
(range.getEnd() >= 0) && (range.getStart() <= range.getEnd())
&& (range.getLength() > 0);
}
- private static boolean validate(Ranges.Entry range, long length) {
- long start = getStart(range, length);
- long end = getEnd(range, length);
- return (start >= 0) && (end >= 0) && (start <= end);
+ private static boolean validate(Ranges ranges, long length) {
+ List<long[]> rangeContext = new ArrayList<long[]>();
+ for (Ranges.Entry range : ranges.getEntries()) {
+ long start = getStart(range, length);
+ long end = getEnd(range, length);
+ if (start < 0 || start > end) {
+ // illegal entry
+ return false;
+ }
+ // see rfc9110 #status.416
+ for (long[] e : rangeContext) {
+ if (Long.min(end, e[1]) - Long.max(start, e[0]) >= 0) {
Review Comment:
Replace with another ***more readable way***:
```java
long start = getStart(range, length);
long end = getEnd(range, length);
if (start < 0 || start > end) {
// illegal entry
return false;
}
// see rfc9110 #status.416
// invalid if range entries is overlap (equivalent to intersection is
not empty).
for (long[] r : rangeContext) {
long s2 = r[0];
long e2 = r[1];
// Given [s1,e1] and [s2,e2]: if intersection is empty, then { s1>e2
|| s2>e1 }
// logically equivalent to: If not { s1>e2 || s2>e1 }, then
intersection is not empty.
if (start <= e2 && s2 <= end) {
// isOverlap
return false;
}
}
```
```math
$$\begin{align}
[s_1, e_1] \cap [s_2, e_2] = \emptyset \to {(s_1 \gt e_2 \lor s_2 \gt
e_1)} \\\\
\iff \lnot{(s_1 \gt e_2 \lor s_2 \gt e_1)} \to [s_1, e_1] \cap [s_2, e_2]
\neq \emptyset \\\\
\iff {s_1 \le e_2 \land s_2 \le e_1} \to [s_1, e_1] \cap [s_2, e_2] \neq
\emptyset \\\\
\iff s_1 \le e_2 \land s_2 \le e_1 \to {isOverlap}
\end{align}$$
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]