Skip to content

Commit

Permalink
Merge pull request #1017 from mefyl/fix/to-string-trim
Browse files Browse the repository at this point in the history
Fix assertion in `Parser.Source.to_string_trim`.
  • Loading branch information
mseri committed Feb 1, 2024
2 parents 3cd08e5 + 512d69c commit 9d22421
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
@@ -1,3 +1,7 @@
## Unreleased

- http: Fix assertion in `Source.to_string_trim` when `pos <> 0` (mefyl #1017)

## v6.0.0~beta2 (2024-01-05)

- cohttp-eio: Don't blow up Server on client disconnections. (mefyl #1011)
Expand Down
4 changes: 2 additions & 2 deletions http/src/http.ml
Expand Up @@ -944,7 +944,7 @@ module Parser = struct
"Http_parser.Source.substring: Index out of bounds., Requested \
off: %d, len: %d"
pos len);
let last = ref (t.pos + len - 1) in
let last = ref (t.pos + pos + len - 1) in
let pos = ref (t.pos + pos) in
while is_space (String.unsafe_get t.buffer !pos) do
incr pos
Expand All @@ -953,7 +953,7 @@ module Parser = struct
decr last
done;
let len = !last - !pos + 1 in
String.sub t.buffer !pos len
if len < 0 then "" else String.sub t.buffer !pos len

let rec index_rec t ch idx len =
if idx = len then -1
Expand Down
4 changes: 3 additions & 1 deletion http/test/test_parser.ml
Expand Up @@ -15,6 +15,7 @@ let req =
Cookie: wp_ozh_wsa_visits=2; wp_ozh_wsa_visit_lasttime=xxxxxxxxxx; \
__utma=xxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.x; \
__utmz=xxxxxxxxx.xxxxxxxxxx.x.x.utmccn=(referral)|utmcsr=reader.livedoor.com|utmcct=/reader/|utmcmd=referral\r\n\
Empty: \r\n\
\r\n"

let assert_req_success ~here ~expected_req ~expected_consumed ?pos ?len buf =
Expand Down Expand Up @@ -69,13 +70,14 @@ let req_expected =
__utma=xxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.x; \
__utmz=xxxxxxxxx.xxxxxxxxxx.x.x.utmccn=(referral)|utmcsr=reader.livedoor.com|utmcct=/reader/|utmcmd=referral"
);
("Empty", "");
])
`GET "/wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg"

let parse_single_request () =
assert_req_success
~here:[ [%here] ]
~expected_req:req_expected ~expected_consumed:706 req
~expected_req:req_expected ~expected_consumed:718 req

let reject_headers_with_space_before_colon () =
let req =
Expand Down

0 comments on commit 9d22421

Please sign in to comment.