Don't crash after a buffered file failed to close.

[INTERPRETER]
* BUG: Don't crash after a buffered file failed to close.
This commit is contained in:
gambas 2018-10-11 12:30:45 +02:00
parent 901b84a36c
commit ad8ea52b64
2 changed files with 32 additions and 11 deletions

View file

@ -87,14 +87,14 @@ static int stream_open(STREAM *stream, const char *path, int mode)
static int stream_close(STREAM *stream)
{
if (FD == NULL)
return FALSE;
FILE *f = FD;
if (fclose(FD) < 0)
if (!f)
return TRUE;
FD = NULL;
return FALSE;
return fclose(f) < 0;
}
@ -102,6 +102,9 @@ 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;
}

View file

@ -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