From ad8ea52b64b43e0a8ca7f680cea8e3e99feef014 Mon Sep 17 00:00:00 2001 From: gambas Date: Thu, 11 Oct 2018 12:30:45 +0200 Subject: [PATCH] Don't crash after a buffered file failed to close. [INTERPRETER] * BUG: Don't crash after a buffered file failed to close. --- main/gbx/gbx_stream_buffer.c | 35 ++++++++++++++++++++++++++++------- main/share/gb_file_share.h | 8 ++++---- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/main/gbx/gbx_stream_buffer.c b/main/gbx/gbx_stream_buffer.c index 5fbbdd8d9..af5bbe1ab 100644 --- a/main/gbx/gbx_stream_buffer.c +++ b/main/gbx/gbx_stream_buffer.c @@ -87,20 +87,23 @@ static int stream_open(STREAM *stream, const char *path, int mode) static int stream_close(STREAM *stream) { - if (FD == NULL) - return FALSE; - - if (fclose(FD) < 0) + FILE *f = FD; + + if (!f) return TRUE; FD = NULL; - return FALSE; + + return fclose(f) < 0; } static int stream_read(STREAM *stream, char *buffer, int len) { int eff; + + if (!FD) + return TRUE; eff = (int)fread(buffer, 1, len, FD); if (eff < len) @@ -143,12 +146,18 @@ static int stream_read(STREAM *stream, char *buffer, int len) static int stream_flush(STREAM *stream) { + if (!FD) + return TRUE; + return (fflush(FD) != 0); } static int stream_write(STREAM *stream, char *buffer, int len) { + if (!FD) + return TRUE; + return fwrite(buffer, 1, len, FD); /*while (len > 0) @@ -175,12 +184,18 @@ static int stream_write(STREAM *stream, char *buffer, int len) static int stream_seek(STREAM *stream, int64_t pos, int whence) { + if (!FD) + return TRUE; + return (fseek(FD, (off_t)pos, whence) != 0); } static int stream_tell(STREAM *stream, int64_t *pos) { + if (!FD) + return TRUE; + *pos = (int64_t)ftell(FD); return (*pos < 0); } @@ -190,6 +205,9 @@ static int stream_eof(STREAM *stream) { int c; + if (!FD) + return TRUE; + c = fgetc(FD); if (c == EOF) return TRUE; @@ -206,7 +224,7 @@ static int stream_lof(STREAM *stream, int64_t *len) if (!stream->common.available_now) return TRUE; - if (fstat(fileno(FD), &info) < 0) + if (!FD || fstat(fileno(FD), &info) < 0) return TRUE; *len = info.st_size; @@ -216,7 +234,10 @@ static int stream_lof(STREAM *stream, int64_t *len) static int stream_handle(STREAM *stream) { - return fileno(FD); + if (FD) + return fileno(FD); + else + return -1; } diff --git a/main/share/gb_file_share.h b/main/share/gb_file_share.h index f6edf481b..89b0a9a11 100644 --- a/main/share/gb_file_share.h +++ b/main/share/gb_file_share.h @@ -52,10 +52,10 @@ typedef #define GB_STAT_GROUP 1 #define GB_STAT_OTHER 2 -#define FILE_TEMP_PREFIX "/tmp/gambas.%d" -#define FILE_TEMP_DIR "/tmp/gambas.%d/%d" -#define FILE_TEMP_FILE "/tmp/gambas.%d/%d/%d.tmp" -#define FILE_TEMP_PATTERN "/tmp/gambas.%d/%d/%s.tmp" +#define FILE_TEMP_PREFIX "/tmp/gambas.%d" +#define FILE_TEMP_DIR FILE_TEMP_PREFIX "/%d" +#define FILE_TEMP_FILE FILE_TEMP_DIR "/%d.tmp" +#define FILE_TEMP_PATTERN FILE_TEMP_DIR "/%s.tmp" #endif