[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 #ifndef _CUPS_API_1_4
{ {
FILE* fp; FILE* fp = winpr_fopen((const char*)cups_printjob->printjob_object, "a+b");
fp = winpr_fopen((const char*)cups_printjob->printjob_object, "a+b");
if (!fp) if (!fp)
return ERROR_INTERNAL_ERROR; return ERROR_INTERNAL_ERROR;
if (fwrite(data, 1, size, fp) < size) const size_t r = fwrite(data, 1, size, fp);
{
fclose(fp); fclose(fp);
if (r < size)
return ERROR_INTERNAL_ERROR; return ERROR_INTERNAL_ERROR;
// FIXME once this function doesn't return void anymore!
}
fclose(fp);
} }
#else #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. */ extract the Y values to create a grayscale image. */
static int frame_id = 0; static int frame_id = 0;
char buf[100]; char buf[100];
FILE* fp;
if ((frame_id % 30) == 0) if ((frame_id % 30) == 0)
{ {
sprintf_s(buf, sizeof(buf), "/tmp/FreeRDP_Frame_%d.ppm", frame_id); 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); fwrite("P5\n", 1, 3, fp);
sprintf_s(buf, sizeof(buf), "%"PRIu32" %"PRIu32"\n", sample->stream->width, sprintf_s(buf, sizeof(buf), "%"PRIu32" %"PRIu32"\n", sample->stream->width,
sample->stream->height); sample->stream->height);
@ -495,6 +495,7 @@ static BOOL tsmf_sample_playback_video(TSMF_SAMPLE* sample)
fflush(fp); fflush(fp);
fclose(fp); fclose(fp);
} }
}
frame_id++; frame_id++;
#endif #endif

View File

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

View File

@ -877,8 +877,10 @@ int TestFreeRDPCodecRemoteFX(int argc, char* argv[])
#if 0 #if 0
FILE *f = fopen("/tmp/windows.data", "w"); FILE *f = fopen("/tmp/windows.data", "w");
if (f) {
fwrite(dest, IMG_WIDTH * IMG_HEIGHT, FORMAT_SIZE, f); fwrite(dest, IMG_WIDTH * IMG_HEIGHT, FORMAT_SIZE, f);
fclose(f); fclose(f);
}
#endif #endif
if (!fuzzyCompareImage(srefImage, dest, IMG_WIDTH * IMG_HEIGHT)) 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 stream_dump_append(const rdpContext* context, UINT32 flags, wStream* s, size_t* offset)
{ {
SSIZE_T rc = -1; SSIZE_T rc = -1;
FILE* fp; FILE* fp = NULL;
const UINT32 mask = STREAM_MSG_SRV_RX | STREAM_MSG_SRV_TX; const UINT32 mask = STREAM_MSG_SRV_RX | STREAM_MSG_SRV_TX;
CONNECTION_STATE state = freerdp_get_state(context); CONNECTION_STATE state = freerdp_get_state(context);
int r; int r;
@ -219,6 +219,7 @@ SSIZE_T stream_dump_append(const rdpContext* context, UINT32 flags, wStream* s,
goto fail; goto fail;
*offset = (size_t)rc; *offset = (size_t)rc;
fail: fail:
if (fp)
fclose(fp); fclose(fp);
return rc; return rc;
} }
@ -227,7 +228,7 @@ SSIZE_T stream_dump_get(const rdpContext* context, UINT32* flags, wStream* s, si
UINT64* pts) UINT64* pts)
{ {
SSIZE_T rc = -1; SSIZE_T rc = -1;
FILE* fp; FILE* fp = NULL;
int r; int r;
if (!context || !s || !offset) if (!context || !s || !offset)
@ -244,6 +245,7 @@ SSIZE_T stream_dump_get(const rdpContext* context, UINT32* flags, wStream* s, si
rc = _ftelli64(fp); rc = _ftelli64(fp);
fail: fail:
if (fp)
fclose(fp); fclose(fp);
return rc; return rc;
} }

View File

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

View File

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

View File

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

View File

@ -680,10 +680,13 @@ static void SSLCTX_keylog_cb(const SSL* ssl, const char* line)
if (dfile) if (dfile)
{ {
FILE* f = winpr_fopen(dfile, "a+"); FILE* f = winpr_fopen(dfile, "a+");
if (f)
{
fwrite(line, strlen(line), 1, f); fwrite(line, strlen(line), 1, f);
fwrite("\n", 1, 1, f); fwrite("\n", 1, 1, f);
fclose(f); fclose(f);
} }
}
} }
#if OPENSSL_VERSION_NUMBER >= 0x010000000L #if OPENSSL_VERSION_NUMBER >= 0x010000000L

View File

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

View File

@ -298,7 +298,7 @@ out:
static BOOL test_peer_load_icon(freerdp_peer* client) static BOOL test_peer_load_icon(freerdp_peer* client)
{ {
testPeerContext* context; testPeerContext* context;
FILE* fp; FILE* fp = NULL;
int i; int i;
char line[50] = { 0 }; char line[50] = { 0 };
BYTE* rgb_data = NULL; BYTE* rgb_data = NULL;
@ -363,6 +363,7 @@ static BOOL test_peer_load_icon(freerdp_peer* client)
out_fail: out_fail:
free(rgb_data); free(rgb_data);
context->bg_data = NULL; context->bg_data = NULL;
if (fp)
fclose(fp); fclose(fp);
return FALSE; 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, int winpr_bitmap_write_ex(const char* filename, const BYTE* data, size_t stride, size_t width,
size_t height, size_t bpp) size_t height, size_t bpp)
{ {
FILE* fp; FILE* fp = NULL;
BYTE* bmp_header = NULL; BYTE* bmp_header = NULL;
const size_t bpp_stride = width * (bpp / 8); const size_t bpp_stride = width * (bpp / 8);
@ -190,6 +190,7 @@ int winpr_bitmap_write_ex(const char* filename, const BYTE* data, size_t stride,
ret = 1; ret = 1;
fail: fail:
if (fp)
fclose(fp); fclose(fp);
free(bmp_header); free(bmp_header);
return ret; return ret;

View File

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

View File

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

View File

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

View File

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