Fixed missing argument checks in logger, fixed crashes if file not created.

This commit is contained in:
Armin Novak 2014-08-14 10:38:02 +02:00
parent e700dc6818
commit 77ae3bc8b3
4 changed files with 73 additions and 20 deletions

View File

@ -21,6 +21,8 @@
#include "config.h"
#endif
#include <assert.h>
#include <winpr/crt.h>
#include <winpr/file.h>
#include <winpr/path.h>
@ -71,6 +73,9 @@ int WLog_BinaryAppender_Open(wLog* log, wLogBinaryAppender* appender)
ProcessId = GetCurrentProcessId();
if (!log || !appender)
return -1;
if (!appender->FilePath)
{
appender->FilePath = GetKnownSubPath(KNOWN_PATH_TEMP, "wlog");
@ -122,6 +127,9 @@ int WLog_BinaryAppender_WriteMessage(wLog* log, wLogBinaryAppender* appender, wL
int FunctionNameLength;
int TextStringLength;
if (!log || !appender || !message)
return -1;
fp = appender->FileDescriptor;
if (!fp)

View File

@ -91,7 +91,7 @@ int WLog_ConsoleAppender_WriteMessage(wLog* log, wLogConsoleAppender* appender,
#ifdef ANDROID
(void)fp;
android_LogPriority level;
switch(log->Level)
switch(message->Level)
{
case WLOG_TRACE:
level = ANDROID_LOG_VERBOSE;
@ -181,8 +181,9 @@ int WLog_ConsoleAppender_WritePacketMessage(wLog* log, wLogConsoleAppender* appe
free(FullFileName);
}
WLog_PacketMessage_Write((wPcap*) appender->PacketMessageContext,
message->PacketData, message->PacketLength, message->PacketFlags);
if (appender->PacketMessageContext)
WLog_PacketMessage_Write((wPcap*) appender->PacketMessageContext,
message->PacketData, message->PacketLength, message->PacketFlags);
return PacketId;
}

View File

@ -70,6 +70,9 @@ int WLog_FileAppender_Open(wLog* log, wLogFileAppender* appender)
ProcessId = GetCurrentProcessId();
if (!log || !appender)
return -1;
if (!appender->FilePath)
{
appender->FilePath = GetKnownSubPath(KNOWN_PATH_TEMP, "wlog");
@ -102,6 +105,9 @@ int WLog_FileAppender_Open(wLog* log, wLogFileAppender* appender)
int WLog_FileAppender_Close(wLog* log, wLogFileAppender* appender)
{
if (!log || !appender)
return -1;
if (!appender->FileDescriptor)
return 0;
@ -117,6 +123,9 @@ int WLog_FileAppender_WriteMessage(wLog* log, wLogFileAppender* appender, wLogMe
FILE* fp;
char prefix[WLOG_MAX_PREFIX_SIZE];
if (!log || !appender || !message)
return -1;
fp = appender->FileDescriptor;
if (!fp)
@ -139,6 +148,9 @@ int WLog_FileAppender_WriteDataMessage(wLog* log, wLogFileAppender* appender, wL
int DataId;
char* FullFileName;
if (!log || !appender || !message)
return -1;
DataId = g_DataId++;
FullFileName = WLog_Message_GetOutputFileName(DataId, "dat");
@ -156,6 +168,9 @@ int WLog_FileAppender_WriteImageMessage(wLog* log, wLogFileAppender* appender, w
int ImageId;
char* FullFileName;
if (!log || !appender || !message)
return -1;
ImageId = g_ImageId++;
FullFileName = WLog_Message_GetOutputFileName(ImageId, "bmp");

View File

@ -47,35 +47,43 @@ static int gettimeofday(struct timeval* tp, void* tz)
void Pcap_Read_Header(wPcap* pcap, wPcapHeader* header)
{
fread((void*) header, sizeof(wPcapHeader), 1, pcap->fp);
if (pcap && pcap->fp)
fread((void*) header, sizeof(wPcapHeader), 1, pcap->fp);
}
void Pcap_Write_Header(wPcap* pcap, wPcapHeader* header)
{
fwrite((void*) header, sizeof(wPcapHeader), 1, pcap->fp);
if (pcap && pcap->fp)
fwrite((void*) header, sizeof(wPcapHeader), 1, pcap->fp);
}
void Pcap_Read_RecordHeader(wPcap* pcap, wPcapRecordHeader* record)
{
fread((void*) record, sizeof(wPcapRecordHeader), 1, pcap->fp);
if (pcap && pcap->fp)
fread((void*) record, sizeof(wPcapRecordHeader), 1, pcap->fp);
}
void Pcap_Write_RecordHeader(wPcap* pcap, wPcapRecordHeader* record)
{
fwrite((void*) record, sizeof(wPcapRecordHeader), 1, pcap->fp);
if (pcap && pcap->fp)
fwrite((void*) record, sizeof(wPcapRecordHeader), 1, pcap->fp);
}
void Pcap_Write_RecordContent(wPcap* pcap, wPcapRecord* record)
{
fwrite(record->data, record->length, 1, pcap->fp);
if (pcap && pcap->fp)
fwrite(record->data, record->length, 1, pcap->fp);
}
void Pcap_Read_Record(wPcap* pcap, wPcapRecord* record)
{
Pcap_Read_RecordHeader(pcap, &record->header);
record->length = record->header.incl_len;
record->data = malloc(record->length);
fread(record->data, record->length, 1, pcap->fp);
if (pcap && pcap->fp)
{
Pcap_Read_RecordHeader(pcap, &record->header);
record->length = record->header.incl_len;
record->data = malloc(record->length);
fread(record->data, record->length, 1, pcap->fp);
}
}
void Pcap_Write_Record(wPcap* pcap, wPcapRecord* record)
@ -141,8 +149,12 @@ BOOL Pcap_GetNext_RecordHeader(wPcap* pcap, wPcapRecord* record)
BOOL Pcap_GetNext_RecordContent(wPcap* pcap, wPcapRecord* record)
{
fread(record->data, record->length, 1, pcap->fp);
return TRUE;
if (pcap && pcap->fp)
{
fread(record->data, record->length, 1, pcap->fp);
return TRUE;
}
return FALSE;
}
BOOL Pcap_GetNext_Record(wPcap* pcap, wPcapRecord* record)
@ -189,7 +201,7 @@ wPcap* Pcap_Open(char* name, BOOL write)
pcap->header.network = 1; /* ethernet */
Pcap_Write_Header(pcap, &pcap->header);
}
else
else if (pcap->fp)
{
fseek(pcap->fp, 0, SEEK_END);
pcap->file_size = (int) ftell(pcap->fp);
@ -203,22 +215,26 @@ wPcap* Pcap_Open(char* name, BOOL write)
void Pcap_Flush(wPcap* pcap)
{
if (!pcap || !pcap->fp)
return;
while (pcap->record)
{
Pcap_Write_Record(pcap, pcap->record);
pcap->record = pcap->record->next;
}
if (pcap->fp)
fflush(pcap->fp);
fflush(pcap->fp);
}
void Pcap_Close(wPcap* pcap)
{
if (!pcap || !pcap->fp)
return;
Pcap_Flush(pcap);
if (pcap->fp)
fclose(pcap->fp);
fclose(pcap->fp);
free(pcap);
}
@ -228,6 +244,9 @@ int WLog_PacketMessage_Write_EthernetHeader(wPcap* pcap, wEthernetHeader* ethern
wStream* s;
BYTE buffer[14];
if (!pcap || !pcap->fp || !ethernet)
return -1;
s = Stream_New(buffer, 14);
Stream_Write(s, ethernet->Destination, 6);
@ -268,6 +287,9 @@ int WLog_PacketMessage_Write_IPv4Header(wPcap* pcap, wIPv4Header* ipv4)
wStream* s;
BYTE buffer[20];
if (!pcap || !pcap->fp || !ipv4)
return -1;
s = Stream_New(buffer, 20);
Stream_Write_UINT8(s, (ipv4->Version << 4) | ipv4->InternetHeaderLength);
@ -298,6 +320,9 @@ int WLog_PacketMessage_Write_TcpHeader(wPcap* pcap, wTcpHeader* tcp)
wStream* s;
BYTE buffer[20];
if (!pcap || !pcap->fp || !tcp)
return -1;
s = Stream_New(buffer, 20);
Stream_Write_UINT16_BE(s, tcp->SourcePort);
@ -310,7 +335,8 @@ int WLog_PacketMessage_Write_TcpHeader(wPcap* pcap, wTcpHeader* tcp)
Stream_Write_UINT16_BE(s, tcp->Checksum);
Stream_Write_UINT16_BE(s, tcp->UrgentPointer);
fwrite(buffer, 20, 1, pcap->fp);
if (pcap->fp)
fwrite(buffer, 20, 1, pcap->fp);
Stream_Free(s, FALSE);
@ -330,6 +356,9 @@ int WLog_PacketMessage_Write(wPcap* pcap, void* data, DWORD length, DWORD flags)
ethernet.Type = 0x0800;
if (!pcap || !pcap->fp)
return -1;
if (flags & WLOG_PACKET_OUTBOUND)
{
/* 00:15:5D:01:64:04 */