Skip to content

Commit

Permalink
Reset offset when closing a channel
Browse files Browse the repository at this point in the history
If offset keeps its value when the channel is closed, it is possible to
`seek_in` backward which will only decrease `channel->curr` and therefore
mark some buffer bytes as valid: further calls to `input_byte` would
succeed on the closed channel

Fix issue ocaml#11878
  • Loading branch information
shym committed Jan 27, 2023
1 parent b19ebb0 commit 04afce9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
3 changes: 3 additions & 0 deletions runtime/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,9 @@ CAMLprim value caml_ml_close_channel(value vchannel)
immediate caml_flush_partial or caml_refill, thus raising a Sys_error
exception */
channel->curr = channel->max = channel->end;
/* Prevent any seek backward that would mark the last bytes of the
* channel buffer as valid */
channel->offset = 0;

/* If already closed, we are done */
if (channel->fd != -1) {
Expand Down
4 changes: 0 additions & 4 deletions testsuite/tests/lib-channels/close_in.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ let () =
seek_in ic nb_bytes;
close_in ic;
seek_in ic 0;
for _ = 1 to nb_bytes do
(* the bytes we get here were never initialised *)
ignore (input_byte ic)
done;
assert (
try
ignore (input_byte ic);
Expand Down

0 comments on commit 04afce9

Please sign in to comment.