From 18c9b762fccecee079d6c3ec6b3e4ee6924ceb39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Sat, 6 Jul 2013 19:00:41 +0000 Subject: [PATCH] [INTERPRETER] * BUG: A stream redirected through the 'Begin' method checks that stream is ready for writing only when the 'Send' method is called. Between the 'Begin' and 'Send' calls, the PRINT and WRITE instructions will always succeed. git-svn-id: svn://localhost/gambas/trunk@5723 867c0c6c-44f3-4631-809d-bfa615b0a4ec --- main/gbx/gbx_stream.c | 22 ++++++++++++---------- main/gbx/gbx_stream.h | 1 + main/gbx/gbx_subr_file.c | 22 +++++++++++++++++----- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/main/gbx/gbx_stream.c b/main/gbx/gbx_stream.c index 986282880..eb37e7112 100644 --- a/main/gbx/gbx_stream.c +++ b/main/gbx/gbx_stream.c @@ -281,9 +281,11 @@ void STREAM_close(STREAM *stream) void STREAM_flush(STREAM *stream) { + STREAM_end(stream); + if (!stream->type) THROW(E_CLOSED); - + (*(stream->type->flush))(stream); } @@ -306,7 +308,7 @@ void STREAM_read(STREAM *stream, void *addr, int len) { STREAM_eff_read = 0; - if (!stream->type) + if (STREAM_is_closed(stream)) THROW(E_CLOSED); if (len <= 0) @@ -437,7 +439,7 @@ int STREAM_read_max(STREAM *stream, void *addr, int len) void STREAM_write(STREAM *stream, void *addr, int len) { - if (!stream->type) + if (STREAM_is_closed_for_writing(stream)) THROW(E_CLOSED); if (len <= 0) @@ -475,7 +477,7 @@ void STREAM_write_zeros(STREAM *stream, int len) void STREAM_write_eol(STREAM *stream) { - if (!stream->type) + if (STREAM_is_closed_for_writing(stream)) THROW(E_CLOSED); switch(stream->common.eol) @@ -491,7 +493,7 @@ int64_t STREAM_tell(STREAM *stream) { int64_t pos; - if (!stream->type) + if (STREAM_is_closed(stream)) THROW(E_CLOSED); if (stream->type->tell(stream, &pos)) @@ -504,7 +506,7 @@ int64_t STREAM_tell(STREAM *stream) void STREAM_seek(STREAM *stream, int64_t pos, int whence) { - if (!stream->type) + if (STREAM_is_closed(stream)) THROW(E_CLOSED); if (stream->type->seek(stream, pos, whence)) @@ -1423,7 +1425,7 @@ void STREAM_unmap(char *addr, int len) int STREAM_handle(STREAM *stream) { - if (!stream->type) + if (STREAM_is_closed(stream)) THROW(E_CLOSED); if (stream->type->handle) @@ -1438,7 +1440,7 @@ void STREAM_lock(STREAM *stream) int64_t pos; int fd; - if (!stream->type) + if (STREAM_is_closed(stream)) THROW(E_CLOSED); fd = STREAM_handle(stream); @@ -1484,7 +1486,7 @@ void STREAM_lof(STREAM *stream, int64_t *len) int fd; int ilen; - if (!stream->type) + if (STREAM_is_closed(stream)) THROW(E_CLOSED); if (stream->type->lof) @@ -1503,7 +1505,7 @@ void STREAM_lof(STREAM *stream, int64_t *len) bool STREAM_eof(STREAM *stream) { - if (!stream->type) + if (STREAM_is_closed(stream)) THROW(E_CLOSED); if (stream->common.buffer && stream->common.buffer_pos < stream->common.buffer_len) diff --git a/main/gbx/gbx_stream.h b/main/gbx/gbx_stream.h index c4014b871..735fbad84 100644 --- a/main/gbx/gbx_stream.h +++ b/main/gbx/gbx_stream.h @@ -241,6 +241,7 @@ int STREAM_write_direct(int fd, char *buffer, int len); void STREAM_lock(STREAM *stream); #define STREAM_is_closed(_stream) ((_stream)->type == NULL) +#define STREAM_is_closed_for_writing(_stream) (STREAM_is_closed(_stream) && !(_stream)->common.redirected) void STREAM_blocking(STREAM *stream, bool block); #define STREAM_is_blocking(_stream) ((_stream)->common.blocking) diff --git a/main/gbx/gbx_subr_file.c b/main/gbx/gbx_subr_file.c index 5697b85f4..10398ff6a 100755 --- a/main/gbx/gbx_subr_file.c +++ b/main/gbx/gbx_subr_file.c @@ -114,8 +114,7 @@ static STREAM *get_default(intptr_t val) return stream; } -#define get_stream(_value, _can_default) \ -({ \ +#define _get_stream(_value, _can_default) \ STREAM *stream; \ \ VARIANT_undo(_value); \ @@ -134,7 +133,11 @@ static STREAM *get_default(intptr_t val) VALUE_conv_object((_value), (TYPE)CLASS_Stream); \ stream = NULL; \ } \ - } \ + } + +#define get_stream(_value, _can_default) \ +({ \ + _get_stream(_value, _can_default); \ \ if (STREAM_is_closed(stream)) \ THROW(E_CLOSED); \ @@ -142,6 +145,15 @@ static STREAM *get_default(intptr_t val) stream; \ }) +#define get_stream_for_writing(_value, _can_default) \ +({ \ + _get_stream(_value, _can_default); \ + \ + if (STREAM_is_closed_for_writing(stream)) \ + THROW(E_CLOSED); \ + \ + stream; \ +}) static char *get_path(VALUE *param) { @@ -223,7 +235,7 @@ void SUBR_print(ushort code) if (NPARAM < 1) THROW(E_NEPARAM); - _stream = get_stream(PARAM, TRUE); + _stream = get_stream_for_writing(PARAM, TRUE); //PRINT_init(print_it, FALSE); @@ -455,7 +467,7 @@ void SUBR_write(ushort code) SUBR_ENTER_PARAM(3); - stream = get_stream(PARAM, TRUE); + stream = get_stream_for_writing(PARAM, TRUE); if (code & 0x3F) {