[fclose] ensure no invalid pointers are passed.

fclose has undefined behaviour for NULL pointers, so check for these.
This commit is contained in:
akallabeth 2023-04-27 09:23:51 +02:00 committed by akallabeth
parent 09aa4e63a9
commit 516668d02b
16 changed files with 49 additions and 38 deletions

View File

@ -90,21 +90,15 @@ static UINT printer_cups_write_printjob(rdpPrintJob* printjob, const BYTE* data,
#ifndef _CUPS_API_1_4
{
FILE* fp;
fp = winpr_fopen((const char*)cups_printjob->printjob_object, "a+b");
FILE* fp = winpr_fopen((const char*)cups_printjob->printjob_object, "a+b");
if (!fp)
return ERROR_INTERNAL_ERROR;
if (fwrite(data, 1, size, fp) < size)
{
fclose(fp);
return ERROR_INTERNAL_ERROR;
// FIXME once this function doesn't return void anymore!
}
const size_t r = fwrite(data, 1, size, fp);
fclose(fp);
if (r < size)
return ERROR_INTERNAL_ERROR;
}
#else

View File

@ -480,12 +480,12 @@ static BOOL tsmf_sample_playback_video(TSMF_SAMPLE* sample)
extract the Y values to create a grayscale image. */
static int frame_id = 0;
char buf[100];
FILE* fp;
if ((frame_id % 30) == 0)
{
sprintf_s(buf, sizeof(buf), "/tmp/FreeRDP_Frame_%d.ppm", frame_id);
fp = fopen(buf, "wb");
FILE* fp = fopen(buf, "wb");
if (fp) {
fwrite("P5\n", 1, 3, fp);
sprintf_s(buf, sizeof(buf), "%"PRIu32" %"PRIu32"\n", sample->stream->width,
sample->stream->height);
@ -495,6 +495,7 @@ static BOOL tsmf_sample_playback_video(TSMF_SAMPLE* sample)
fflush(fp);
fclose(fp);
}
}
frame_id++;
#endif

View File

@ -1537,23 +1537,20 @@ static void cliprdr_local_file_try_close(CliprdrLocalFile* file, UINT res, UINT6
WINPR_ASSERT(file->context);
WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s after error %" PRIu32,
file->name, res);
fclose(file->fp);
file->fp = NULL;
}
else if (((file->size > 0) && (offset + size >= file->size)))
{
WINPR_ASSERT(file->context);
WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s after read", file->name);
fclose(file->fp);
file->fp = NULL;
}
else
{
// TODO: we need to keep track of open files to avoid running out of file descriptors
// TODO: for the time being just close again.
}
if (file->fp)
fclose(file->fp);
file->fp = NULL;
}
}
static UINT cliprdr_file_context_server_file_size_request(

View File

@ -877,8 +877,10 @@ int TestFreeRDPCodecRemoteFX(int argc, char* argv[])
#if 0
FILE *f = fopen("/tmp/windows.data", "w");
if (f) {
fwrite(dest, IMG_WIDTH * IMG_HEIGHT, FORMAT_SIZE, f);
fclose(f);
}
#endif
if (!fuzzyCompareImage(srefImage, dest, IMG_WIDTH * IMG_HEIGHT))

View File

@ -187,7 +187,7 @@ fail:
SSIZE_T stream_dump_append(const rdpContext* context, UINT32 flags, wStream* s, size_t* offset)
{
SSIZE_T rc = -1;
FILE* fp;
FILE* fp = NULL;
const UINT32 mask = STREAM_MSG_SRV_RX | STREAM_MSG_SRV_TX;
CONNECTION_STATE state = freerdp_get_state(context);
int r;
@ -219,7 +219,8 @@ SSIZE_T stream_dump_append(const rdpContext* context, UINT32 flags, wStream* s,
goto fail;
*offset = (size_t)rc;
fail:
fclose(fp);
if (fp)
fclose(fp);
return rc;
}
@ -227,7 +228,7 @@ SSIZE_T stream_dump_get(const rdpContext* context, UINT32* flags, wStream* s, si
UINT64* pts)
{
SSIZE_T rc = -1;
FILE* fp;
FILE* fp = NULL;
int r;
if (!context || !s || !offset)
@ -244,7 +245,8 @@ SSIZE_T stream_dump_get(const rdpContext* context, UINT32* flags, wStream* s, si
rc = _ftelli64(fp);
fail:
fclose(fp);
if (fp)
fclose(fp);
return rc;
}

View File

@ -83,7 +83,8 @@ static BOOL test_entry_read_write(void)
fail:
Stream_Free(sr, TRUE);
Stream_Free(sw, TRUE);
fclose(fp);
if (fp)
fclose(fp);
if (name)
DeleteFileA(name);
free(name);

View File

@ -124,7 +124,8 @@ BOOL freerdp_certificate_store_save_data(rdpCertificateStore* store, const rdpCe
rc = TRUE;
fail:
fclose(fp);
if (fp)
fclose(fp);
free(path);
return rc;
}

View File

@ -229,7 +229,8 @@ fail:
WLog_WARN(TAG, "Failed to read PEM from file '%s' [%s]", filename,
winpr_strerror(errno, buffer, sizeof(buffer)));
}
fclose(fp);
if (fp)
fclose(fp);
free(pem);
return NULL;
}

View File

@ -680,9 +680,12 @@ static void SSLCTX_keylog_cb(const SSL* ssl, const char* line)
if (dfile)
{
FILE* f = winpr_fopen(dfile, "a+");
fwrite(line, strlen(line), 1, f);
fwrite("\n", 1, 1, f);
fclose(f);
if (f)
{
fwrite(line, strlen(line), 1, f);
fwrite("\n", 1, 1, f);
fclose(f);
}
}
}

View File

@ -206,7 +206,8 @@ error:
tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags);
if (terminal_fildes != STDIN_FILENO)
{
fclose(fp);
if (fp)
fclose(fp);
}
errno = saved_errno;
return NULL;

View File

@ -298,7 +298,7 @@ out:
static BOOL test_peer_load_icon(freerdp_peer* client)
{
testPeerContext* context;
FILE* fp;
FILE* fp = NULL;
int i;
char line[50] = { 0 };
BYTE* rgb_data = NULL;
@ -363,7 +363,8 @@ static BOOL test_peer_load_icon(freerdp_peer* client)
out_fail:
free(rgb_data);
context->bg_data = NULL;
fclose(fp);
if (fp)
fclose(fp);
return FALSE;
}

View File

@ -158,7 +158,7 @@ int winpr_bitmap_write(const char* filename, const BYTE* data, size_t width, siz
int winpr_bitmap_write_ex(const char* filename, const BYTE* data, size_t stride, size_t width,
size_t height, size_t bpp)
{
FILE* fp;
FILE* fp = NULL;
BYTE* bmp_header = NULL;
const size_t bpp_stride = width * (bpp / 8);
@ -190,7 +190,8 @@ int winpr_bitmap_write_ex(const char* filename, const BYTE* data, size_t stride,
ret = 1;
fail:
fclose(fp);
if (fp)
fclose(fp);
free(bmp_header);
return ret;
}

View File

@ -157,7 +157,8 @@ static BOOL IniFile_Load_File(wIniFile* ini, const char* filename)
if (fread(ini->buffer, (size_t)fileSize, 1, ini->fp) != 1)
goto out_buffer;
fclose(ini->fp);
if (ini->fp)
fclose(ini->fp);
ini->fp = NULL;
ini->buffer[fileSize] = '\n';
ini->buffer[fileSize + 1] = '\0';
@ -167,7 +168,8 @@ out_buffer:
free(ini->buffer);
ini->buffer = NULL;
out_file:
fclose(ini->fp);
if (ini->fp)
fclose(ini->fp);
ini->fp = NULL;
return FALSE;
}
@ -778,7 +780,8 @@ int IniFile_WriteFile(wIniFile* ini, const char* filename)
if (fwrite((void*)buffer, length, 1, ini->fp) != 1)
ret = -1;
fclose(ini->fp);
if (ini->fp)
fclose(ini->fp);
free(buffer);
return ret;
}

View File

@ -359,7 +359,8 @@ void SamClose(WINPR_SAM* sam)
{
if (sam != NULL)
{
fclose(sam->fp);
if (sam->fp)
fclose(sam->fp);
free(sam);
}
}

View File

@ -94,7 +94,8 @@ static BOOL WLog_BinaryAppender_Close(wLog* log, wLogAppender* appender)
if (!binaryAppender->FileDescriptor)
return TRUE;
fclose(binaryAppender->FileDescriptor);
if (binaryAppender->FileDescriptor)
fclose(binaryAppender->FileDescriptor);
binaryAppender->FileDescriptor = NULL;

View File

@ -232,7 +232,8 @@ wPcap* Pcap_Open(char* name, BOOL write)
return pcap;
out_fail:
fclose(pcap_fp);
if (pcap_fp)
fclose(pcap_fp);
free(pcap);
return NULL;
}