Skip to content

Commit

Permalink
In_channel.input_all: fix bug (#10978)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojb committed Feb 1, 2022
1 parent 9453a90 commit f222f7c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ OCaml 4.14.0
Daniel Bünzli, Naëla Courant, Craig Ferguson, Wiktor Kuchta, Xavier
Leroy, Guillaume Munch-Maccagnoni, Raphaël Proust, and Gabriel Scherer)

- #10596: Add with_open_bin, with_open_text and with_open_gen to In_channel and
Out_channel. Also, add In_channel.input_all.
- #10596, #10978: Add with_open_bin, with_open_text and with_open_gen to
In_channel and Out_channel. Also, add In_channel.input_all.
(Nicolás Ojeda Bär, review by Daniel Bünzli, Jérémie Dimino, Damien Doligez
and Xavier Leroy)

Expand Down
2 changes: 1 addition & 1 deletion stdlib/in_channel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ let ensure buf ofs n =
in
let new_buf = Bytes.create new_len in
Bytes.blit buf 0 new_buf 0 ofs;
buf
new_buf
end

let input_all ic =
Expand Down
26 changes: 26 additions & 0 deletions testsuite/tests/lib-channels/input_all.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(* TEST
include systhreads
readonly_files = "input_all.ml"
*)

Expand Down Expand Up @@ -86,3 +87,28 @@ let check midpoint =

let () =
List.iter check [0; 1; String.length raw_contents]

let random_char () =
Char.chr (Random.int 256)

let test_pipe n =
let buf = Bytes.init n (fun _ -> random_char ()) in
let toread, towrite = Unix.pipe () in
let producer () =
let rec loop pos rem =
let n = Unix.write towrite buf pos rem in
if n = rem then Unix.close towrite
else loop (pos + n) (rem - n)
in
loop 0 (Bytes.length buf)
in
let read_buf = ref "" in
let consumer () = read_buf := In_channel.input_all (Unix.in_channel_of_descr toread) in
let producer = Thread.create producer () in
let consumer = Thread.create consumer () in
Thread.join producer;
Thread.join consumer;
assert (!read_buf = Bytes.unsafe_to_string buf)

let () =
test_pipe 655397

0 comments on commit f222f7c

Please sign in to comment.