Skip to content

Commit 6e3fbe1

Browse files
Merge pull request #60 from jeong-sik/fix/read-all-short-reads
Read the plugin request until end of input, not the first short read
2 parents 4f9a68e + ce9996d commit 6e3fbe1

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

‎src/plugin/protoc_gen_ocaml.ml‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ open StdLabels
22
module Descriptor = Spec.Descriptor.Google.Protobuf
33
module Plugin = Spec.Plugin.Google.Protobuf.Compiler
44

5+
(* [input] may return fewer bytes than requested before the end of input;
6+
only 0 means end of file. A pipe can hand over a short first chunk
7+
(512 bytes on macOS), so stopping at the first short read would cut
8+
the request that protoc sends. *)
59
let read_all in_channel =
6-
let rec inner buffer =
7-
let b = Bytes.create 1024 in
8-
match input in_channel b 0 1024 with
9-
| 1024 ->
10-
Buffer.add_bytes buffer b;
11-
inner buffer
10+
let buffer = Buffer.create 1024 in
11+
let chunk = Bytes.create 1024 in
12+
let rec inner () =
13+
match input in_channel chunk 0 (Bytes.length chunk) with
14+
| 0 -> Buffer.contents buffer
1215
| read ->
13-
Buffer.add_subbytes buffer b 0 read;
14-
Buffer.contents buffer
16+
Buffer.add_subbytes buffer chunk 0 read;
17+
inner ()
1518
in
16-
inner (Buffer.create 1024)
19+
inner ()
1720

1821
(* Read from stdin *)
1922
let read () =

0 commit comments

Comments
 (0)