From e7e681d2efdc0a9265987b57efbeb323eef67c09 Mon Sep 17 00:00:00 2001 From: Samuel Hym Date: Thu, 26 Jan 2023 14:53:50 +0100 Subject: [PATCH] Reset offset when closing a channel 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 #11878 --- runtime/io.c | 3 +++ testsuite/tests/lib-channels/close_in.ml | 4 ---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/runtime/io.c b/runtime/io.c index 3e7d5e89046f..d4160dbbbb8b 100644 --- a/runtime/io.c +++ b/runtime/io.c @@ -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) { diff --git a/testsuite/tests/lib-channels/close_in.ml b/testsuite/tests/lib-channels/close_in.ml index 129fd203f154..1c7cbbdf65c0 100644 --- a/testsuite/tests/lib-channels/close_in.ml +++ b/testsuite/tests/lib-channels/close_in.ml @@ -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);