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

In_channel.input_all: fix bug #10978

Merged
merged 3 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,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