From 424efbe67a7e995dc7f9dc9a54dd584c3e48b36d Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Thu, 22 Jan 2015 18:10:46 +0900 Subject: [PATCH] Closing a port should reset the offset and force an error on further access. --- sexp.c | 4 ++++ vm.c | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/sexp.c b/sexp.c index 771e2ed9..61af879b 100644 --- a/sexp.c +++ b/sexp.c @@ -172,6 +172,8 @@ sexp sexp_finalize_port (sexp ctx, sexp self, sexp_sint_t n, sexp port) { #endif ) free(sexp_port_buf(port)); + sexp_port_offset(port) = 0; + sexp_port_size(port) = 0; } return res; } @@ -1444,6 +1446,8 @@ int sexp_buffered_read_char (sexp ctx, sexp p) { int res = 0; if (sexp_port_offset(p) < sexp_port_size(p)) { return ((unsigned char*)sexp_port_buf(p))[sexp_port_offset(p)++]; + } else if (!sexp_port_openp(p)) { + return EOF; } else if (sexp_port_stream(p)) { res = fread(sexp_port_buf(p), 1, SEXP_PORT_BUFFER_SIZE, sexp_port_stream(p)); if (res >= 0) { diff --git a/vm.c b/vm.c index 829d4aaf..8b8ec5ed 100644 --- a/vm.c +++ b/vm.c @@ -1936,6 +1936,9 @@ sexp sexp_apply (sexp ctx, sexp proc, sexp args) { #endif i = sexp_write_char(ctx, sexp_unbox_character(_ARG1), _ARG2); if (i == EOF) { + if (!sexp_port_openp(_ARG2)) + sexp_raise("write-char: port is closed", _ARG2); + else #if SEXP_USE_GREEN_THREADS if ((sexp_port_stream(_ARG2) ? ferror(sexp_port_stream(_ARG2)) : 1) && (errno == EAGAIN)) { @@ -1973,6 +1976,8 @@ sexp sexp_apply (sexp ctx, sexp proc, sexp args) { sexp_raise("write-string: not a valid string count", sexp_list2(ctx, tmp1, _ARG2)); if (! sexp_oportp(_ARG3)) sexp_raise("write-string: not an output-port", sexp_list1(ctx, _ARG3)); + if (!sexp_port_openp(_ARG3)) + sexp_raise("write-string: port is closed", _ARG3); sexp_context_top(ctx) = top; #if SEXP_USE_GREEN_THREADS errno = 0; @@ -2015,6 +2020,9 @@ sexp sexp_apply (sexp ctx, sexp proc, sexp args) { else #endif if (i == EOF) { + if (!sexp_port_openp(_ARG1)) + sexp_raise("peek-char: port is closed", _ARG1); + else #if SEXP_USE_GREEN_THREADS if ((sexp_port_stream(_ARG1) ? ferror(sexp_port_stream(_ARG1)) : 1) && (errno == EAGAIN)) { @@ -2042,6 +2050,9 @@ sexp sexp_apply (sexp ctx, sexp proc, sexp args) { #endif i = sexp_read_char(ctx, _ARG1); if (i == EOF) { + if (!sexp_port_openp(_ARG1)) + sexp_raise("read-char: port is closed", _ARG1); + else #if SEXP_USE_GREEN_THREADS if ((sexp_port_stream(_ARG1) ? ferror(sexp_port_stream(_ARG1)) : 1) && (errno == EAGAIN)) {