Author: tretiakov
Date: Tue Jul 18 16:19:07 2006
New Revision: 23138
URL:
http://svn.reactos.org/svn/reactos?rev=23138&view=rev
Log:
Add missing return value checks. Finally fix bug #1526
Modified:
trunk/reactos/base/services/eventlog/file.c
Modified: trunk/reactos/base/services/eventlog/file.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/services/eventlog/fil…
==============================================================================
--- trunk/reactos/base/services/eventlog/file.c (original)
+++ trunk/reactos/base/services/eventlog/file.c Tue Jul 18 16:19:07 2006
@@ -30,11 +30,15 @@
LogFile->Header.NextRecord = 1;
LogFile->Header.Signature = LOGFILE_SIGNATURE;
- WriteFile(LogFile->hFile,
+ if(!WriteFile(LogFile->hFile,
&LogFile->Header,
sizeof(FILE_HEADER),
&dwWritten,
- NULL);
+ NULL))
+ {
+ DPRINT1("WriteFile failed:%d!\n", GetLastError());
+ return FALSE;
+ }
EofRec.Ones = 0x11111111;
EofRec.Twos = 0x22222222;
@@ -47,9 +51,21 @@
EofRec.StartOffset = LogFile->Header.FirstRecordOffset;
EofRec.EndOffset = LogFile->Header.EofOffset;
- WriteFile(LogFile->hFile, &EofRec, sizeof(EOF_RECORD), &dwWritten, NULL);
-
- FlushFileBuffers(LogFile->hFile);
+ if(!WriteFile(LogFile->hFile,
+ &EofRec,
+ sizeof(EOF_RECORD),
+ &dwWritten,
+ NULL))
+ {
+ DPRINT1("WriteFile failed:%d!\n", GetLastError());
+ return FALSE;
+ }
+
+ if(!FlushFileBuffers(LogFile->hFile))
+ {
+ DPRINT1("FlushFileBuffers failed:%d!\n", GetLastError());
+ return FALSE;
+ }
return TRUE;
}
@@ -62,11 +78,21 @@
PDWORD pdwRecSize2;
PEVENTLOGRECORD RecBuf;
- SetFilePointer(LogFile->hFile, 0, NULL, FILE_BEGIN);
- ReadFile(LogFile->hFile,
+ if(SetFilePointer(LogFile->hFile, 0, NULL, FILE_BEGIN) ==
+ INVALID_SET_FILE_POINTER)
+ {
+ DPRINT1("SetFilePointer failed! %d\n", GetLastError());
+ return FALSE;
+ }
+
+ if(!ReadFile(LogFile->hFile,
&LogFile->Header,
sizeof(FILE_HEADER),
- &dwRead, NULL);
+ &dwRead, NULL))
+ {
+ DPRINT1("ReadFile failed! %d\n", GetLastError());
+ return FALSE;
+ }
if(dwRead != sizeof(FILE_HEADER))
{
@@ -103,21 +129,35 @@
0,
NULL,
FILE_CURRENT);
-
- ReadFile(LogFile->hFile,
+
+ if(dwFilePointer == INVALID_SET_FILE_POINTER)
+ {
+ DPRINT1("SetFilePointer failed! %d\n", GetLastError());
+ return FALSE;
+ }
+
+ if(!ReadFile(LogFile->hFile,
&dwRecSize,
sizeof(dwRecSize),
&dwRead,
- NULL);
+ NULL))
+ {
+ DPRINT1("ReadFile failed! %d\n", GetLastError());
+ return FALSE;
+ }
if(dwRead != sizeof(dwRecSize))
break;
- ReadFile(LogFile->hFile,
+ if(!ReadFile(LogFile->hFile,
&dwRecSign,
sizeof(dwRecSign),
&dwRead,
- NULL);
+ NULL))
+ {
+ DPRINT1("ReadFile() failed! %d\n", GetLastError());
+ return FALSE;
+ }
if(dwRead != sizeof(dwRecSize))
break;
@@ -129,13 +169,33 @@
break;
}
- SetFilePointer(LogFile->hFile, -((LONG)sizeof(DWORD)*2), NULL, FILE_CURRENT);
+ if(SetFilePointer(LogFile->hFile,
+ -((LONG)sizeof(DWORD)*2),
+ NULL,
+ FILE_CURRENT) == INVALID_SET_FILE_POINTER)
+ {
+ DPRINT1("SetFilePointer() failed! %d", GetLastError());
+ return FALSE;
+ }
+
RecBuf = (PEVENTLOGRECORD) HeapAlloc(MyHeap, 0, dwRecSize);
- ReadFile(LogFile->hFile,
+
+ if(!RecBuf)
+ {
+ DPRINT1("Can't allocate heap!\n");
+ return FALSE;
+ }
+
+ if(!ReadFile(LogFile->hFile,
RecBuf,
dwRecSize,
&dwRead,
- NULL);
+ NULL))
+ {
+ DPRINT1("ReadFile() failed! %d\n", GetLastError());
+ HeapFree(MyHeap, 0, RecBuf);
+ return FALSE;
+ }
if(dwRead != dwRecSize)
{
@@ -146,7 +206,7 @@
pdwRecSize2 = (PDWORD)(((PBYTE)RecBuf)+dwRecSize-4);
if(*pdwRecSize2 != dwRecSize)
{
- DPRINT("EventLog: Invalid size2 of record %d (%x) in %s\n",
+ DPRINT1("Invalid size2 of record %d (%x) in %s\n",
dwRecordsNumber,
*pdwRecSize2,
LogFile->LogName);
@@ -158,6 +218,7 @@
if(!LogfAddOffsetInformation(LogFile, RecBuf->RecordNumber, dwFilePointer))
{
+ DPRINT1("LogfAddOffsetInformation() failed!\n");
HeapFree(MyHeap, 0, RecBuf);
return FALSE;
}
@@ -168,13 +229,28 @@
LogFile->Header.NextRecord = dwRecordsNumber+1;
LogFile->Header.OldestRecord = dwRecordsNumber ? 1 : 0; //FIXME
- SetFilePointer(LogFile->hFile, 0, 0, FILE_CURRENT);
- WriteFile(LogFile->hFile,
+ if(!SetFilePointer(LogFile->hFile, 0, NULL, FILE_CURRENT) ==
+ INVALID_SET_FILE_POINTER)
+ {
+ DPRINT1("SetFilePointer() failed! %d\n", GetLastError());
+ return FALSE;
+ }
+
+ if(!WriteFile(LogFile->hFile,
&LogFile->Header,
sizeof(FILE_HEADER),
&dwRead,
- NULL);
- FlushFileBuffers(LogFile->hFile);
+ NULL))
+ {
+ DPRINT1("WriteFile failed! %d\n", GetLastError());
+ return FALSE;
+ }
+
+ if(!FlushFileBuffers(LogFile->hFile))
+ {
+ DPRINT1("FlushFileBuffers failed! %d\n", GetLastError());
+ return FALSE;
+ }
return TRUE;
}
@@ -190,7 +266,7 @@
sizeof(LOGFILE));
if(!LogFile)
{
- DbgPrint("EventLog: Can't allocate heap\n");
+ DPRINT1("Can't allocate heap!\n");
return NULL;
}
@@ -205,51 +281,43 @@
if(LogFile->hFile == INVALID_HANDLE_VALUE)
{
- DPRINT("Can't create file %S.\n", FileName);
+ DPRINT1("Can't create file %S.\n", FileName);
HeapFree(MyHeap, 0, LogFile);
return NULL;
}
- bCreateNew = GetLastError() == ERROR_ALREADY_EXISTS ? FALSE : TRUE;
+ bCreateNew = (GetLastError() == ERROR_ALREADY_EXISTS) ? FALSE : TRUE;
LogFile->LogName = HeapAlloc(MyHeap,
- HEAP_ZERO_MEMORY,
- (lstrlenW(LogName)+1)*sizeof(WCHAR));
- if(LogFile->LogName)
- lstrcpyW(LogFile->LogName, LogName);
+ HEAP_ZERO_MEMORY,
+ (lstrlenW(LogName)+1)*sizeof(WCHAR));
+
+ if(LogFile->LogName) lstrcpyW(LogFile->LogName, LogName);
else
{
- DPRINT("EventLog: Can't allocate heap\n");
+ DPRINT1("Can't allocate heap\n");
HeapFree(MyHeap, 0, LogFile);
return NULL;
}
LogFile->FileName = HeapAlloc(MyHeap,
- HEAP_ZERO_MEMORY,
- (lstrlenW(FileName)+1)*sizeof(WCHAR));
- if(LogFile->FileName)
- lstrcpyW(LogFile->FileName, FileName);
+ HEAP_ZERO_MEMORY,
+ (lstrlenW(FileName)+1)*sizeof(WCHAR));
+
+ if(LogFile->FileName) lstrcpyW(LogFile->FileName, FileName);
else
{
- DPRINT("EventLog: Can't allocate heap\n");
- HeapFree(MyHeap, 0, LogFile->LogName);
- HeapFree(MyHeap, 0, LogFile);
- return NULL;
- }
-
+ DPRINT1("Can't allocate heap\n");
+ goto fail;
+ }
LogFile->OffsetInfo = (PEVENT_OFFSET_INFO)
- HeapAlloc(MyHeap,
- HEAP_ZERO_MEMORY,
- sizeof(EVENT_OFFSET_INFO)*64);
+ HeapAlloc(MyHeap, HEAP_ZERO_MEMORY, sizeof(EVENT_OFFSET_INFO)*64);
if(!LogFile->OffsetInfo)
{
- DPRINT("EventLog: Can't allocate heap\n");
- HeapFree(MyHeap, 0, LogFile->FileName);
- HeapFree(MyHeap, 0, LogFile->LogName);
- HeapFree(MyHeap, 0, LogFile);
- return NULL;
+ DPRINT1("Can't allocate heap\n");
+ goto fail;
}
LogFile->OffsetInfoSize = 64;
@@ -258,18 +326,21 @@
bResult = LogfInitializeNew(LogFile);
else bResult = LogfInitializeExisting(LogFile);
- if(!bResult)
- {
- HeapFree(MyHeap, 0, LogFile->OffsetInfo);
- HeapFree(MyHeap, 0, LogFile->FileName);
- HeapFree(MyHeap, 0, LogFile->LogName);
- HeapFree(MyHeap, 0, LogFile);
- return NULL;
- }
+ if(!bResult) goto fail;
InitializeCriticalSection(&LogFile->cs);
LogfListAddItem(LogFile);
return LogFile;
+
+fail:
+ if(LogFile)
+ {
+ if(LogFile->OffsetInfo) HeapFree(MyHeap, 0, LogFile->OffsetInfo);
+ if(LogFile->FileName) HeapFree(MyHeap, 0, LogFile->FileName);
+ if(LogFile->LogName) HeapFree(MyHeap, 0, LogFile->LogName);
+ HeapFree(MyHeap, 0, LogFile);
+ }
+ return NULL;
}
VOID LogfClose(PLOGFILE LogFile)
@@ -445,8 +516,21 @@
return FALSE;
}
- SetFilePointer(LogFile->hFile, dwOffset, NULL, FILE_BEGIN);
- ReadFile(LogFile->hFile, &dwRecSize, sizeof(DWORD), &dwRead, NULL);
+ if(SetFilePointer(LogFile->hFile, dwOffset, NULL, FILE_BEGIN)
+ == INVALID_SET_FILE_POINTER)
+ {
+ DPRINT1("SetFilePointer() failed! %d\n", GetLastError());
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
+
+ if(!ReadFile(LogFile->hFile, &dwRecSize, sizeof(DWORD), &dwRead, NULL))
+ {
+ DPRINT1("ReadFile() failed! %d\n", GetLastError());
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
+
if(dwRecSize > BufSize)
{
*BytesRead = 0;
@@ -456,12 +540,23 @@
return FALSE;
}
- SetFilePointer(LogFile->hFile,
+ if(SetFilePointer(LogFile->hFile,
-((LONG)sizeof(DWORD)),
NULL,
- FILE_CURRENT);
-
- ReadFile(LogFile->hFile, Buffer, dwRecSize, &dwRead, NULL);
+ FILE_CURRENT) == INVALID_SET_FILE_POINTER)
+ {
+ DPRINT1("SetFilePointer() failed! %d\n", GetLastError());
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
+
+ if(!ReadFile(LogFile->hFile, Buffer, dwRecSize, &dwRead, NULL))
+ {
+ DPRINT1("ReadFile() failed! %d\n", GetLastError());
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
+
dwBufferUsage+=dwRead;
while(dwBufferUsage<BufSize)
@@ -473,20 +568,43 @@
dwOffset = LogfOffsetByNumber(LogFile, dwRecNum);
if(!dwOffset) break;
- SetFilePointer(LogFile->hFile, dwOffset, NULL, FILE_BEGIN);
- ReadFile(LogFile->hFile, &dwRecSize, sizeof(DWORD), &dwRead, NULL);
+ if(SetFilePointer(LogFile->hFile, dwOffset, NULL, FILE_BEGIN)
+ == INVALID_SET_FILE_POINTER)
+ {
+ DPRINT1("SetFilePointer() failed! %d\n", GetLastError());
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
+
+ if(!ReadFile(LogFile->hFile, &dwRecSize, sizeof(DWORD), &dwRead, NULL))
+ {
+ DPRINT1("ReadFile() failed! %d\n", GetLastError());
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
+
if(dwBufferUsage+dwRecSize>BufSize)break;
- SetFilePointer(LogFile->hFile,
+ if(SetFilePointer(LogFile->hFile,
-((LONG)sizeof(DWORD)),
NULL,
- FILE_CURRENT);
-
- ReadFile(LogFile->hFile,
+ FILE_CURRENT) == INVALID_SET_FILE_POINTER)
+ {
+ DPRINT1("SetFilePointer() failed! %d\n", GetLastError());
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
+
+ if(!ReadFile(LogFile->hFile,
Buffer+dwBufferUsage,
dwRecSize,
&dwRead,
- NULL);
+ NULL))
+ {
+ DPRINT1("ReadFile() failed! %d\n", GetLastError());
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
dwBufferUsage+=dwRead;
}
@@ -503,7 +621,6 @@
DWORD dwWritten;
SYSTEMTIME st;
EOF_RECORD EofRec;
- BOOL bResult;
if(!Buffer)
{
@@ -515,11 +632,18 @@
EnterCriticalSection(&LogFile->cs);
- SetFilePointer(LogFile->hFile, LogFile->Header.EofOffset, NULL, FILE_BEGIN);
- WriteFile(LogFile->hFile, Buffer, BufSize, &dwWritten, NULL);
-
- if(BufSize != dwWritten)
- {
+ if(SetFilePointer(LogFile->hFile,
+ LogFile->Header.EofOffset,
+ NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
+ {
+ DPRINT1("SetFilePointer() failed! %d\n", GetLastError());
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
+
+ if(!WriteFile(LogFile->hFile, Buffer, BufSize, &dwWritten, NULL))
+ {
+ DPRINT1("WriteFile() failed! %d\n", GetLastError());
LeaveCriticalSection(&LogFile->cs);
return FALSE;
}
@@ -549,24 +673,41 @@
EofRec.StartOffset = LogFile->Header.FirstRecordOffset;
EofRec.EndOffset = LogFile->Header.EofOffset;
- WriteFile(LogFile->hFile, &EofRec, sizeof(EOF_RECORD), &dwWritten, NULL);
-
- SetFilePointer(LogFile->hFile, 0, NULL, FILE_BEGIN);
- bResult = WriteFile(LogFile->hFile,
+ if(!WriteFile(LogFile->hFile,
+ &EofRec,
+ sizeof(EOF_RECORD),
+ &dwWritten, NULL))
+ {
+ DPRINT1("WriteFile() failed! %d\n", GetLastError());
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
+
+ if(SetFilePointer(LogFile->hFile, 0, NULL, FILE_BEGIN)
+ == INVALID_SET_FILE_POINTER)
+ {
+ DPRINT1("SetFilePointer() failed! %d\n", GetLastError());
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
+
+ if(!WriteFile(LogFile->hFile,
&LogFile->Header,
sizeof(FILE_HEADER),
&dwWritten,
- NULL);
-
- if(!bResult)
- {
- DPRINT("WriteFile failed! LastError = %d\n", GetLastError());
+ NULL))
+ {
+ DPRINT1("WriteFile failed! LastError = %d\n", GetLastError());
LeaveCriticalSection(&LogFile->cs);
return FALSE;
}
if(!FlushFileBuffers(LogFile->hFile))
- DPRINT("FlushFileBuffers() failed!\n");
+ {
+ LeaveCriticalSection(&LogFile->cs);
+ DPRINT1("FlushFileBuffers() failed! %d\n", GetLastError());
+ return FALSE;
+ }
LeaveCriticalSection(&LogFile->cs);
return TRUE;
@@ -603,7 +744,7 @@
sizeof(EVENT_OFFSET_INFO));
if(!NewOffsetInfo)
{
- DbgPrint("EventLog: Can't reallocate heap.\n");
+ DPRINT1("Can't reallocate heap.\n");
return FALSE;
}