From a0230af6f5bd3ee44336ed9b07bdcfdcf0e18310 Mon Sep 17 00:00:00 2001 From: Magnus Olsen Date: Thu, 7 Jun 2007 09:43:50 +0000 Subject: [PATCH] fixing a stuoied bug, when malloc fail, gcc genrated code will crash. This fix will provent ddraw alloc crash. svn path=/trunk/; revision=27037 --- .../dll/directx/ddraw/Surface/createsurface.c | 3 + reactos/dll/directx/ddraw/main.c | 12 ++-- reactos/dll/directx/ddraw/rosdraw.h | 28 ++++++++-- reactos/dll/directx/ddraw/startup.c | 55 ++++++++++--------- 4 files changed, 60 insertions(+), 38 deletions(-) diff --git a/reactos/dll/directx/ddraw/Surface/createsurface.c b/reactos/dll/directx/ddraw/Surface/createsurface.c index 5dddd5825a7..8fa72b08438 100644 --- a/reactos/dll/directx/ddraw/Surface/createsurface.c +++ b/reactos/dll/directx/ddraw/Surface/createsurface.c @@ -9,6 +9,9 @@ */ #include "rosdraw.h" +/* PSEH for SEH Support */ +#include + /* * all param have been checked if they are vaild before they are call to * Internal_CreateSurface, if not please fix the code in the functions diff --git a/reactos/dll/directx/ddraw/main.c b/reactos/dll/directx/ddraw/main.c index 2cedf32fd4a..b83931b3975 100644 --- a/reactos/dll/directx/ddraw/main.c +++ b/reactos/dll/directx/ddraw/main.c @@ -65,7 +65,7 @@ DirectDrawCreateEx(LPGUID lpGUID, */ DX_WINDBG_trace(); - _SEH_TRY + _SEH_TRY { /* check see if pUnkOuter is null or not */ if (pUnkOuter) @@ -243,15 +243,15 @@ DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) switch(ul_reason_for_call) { case DLL_PROCESS_DETACH: - DeleteCriticalSection( &ddcs ); + //DeleteCriticalSection( &ddcs ); retStatus = TRUE; break; case DLL_PROCESS_ATTACH: - DisableThreadLibraryCalls( hModule ); - InitializeCriticalSection( &ddcs ); - EnterCriticalSection( &ddcs ); - LeaveCriticalSection( &ddcs ); + //DisableThreadLibraryCalls( hModule ); + //InitializeCriticalSection( &ddcs ); + //EnterCriticalSection( &ddcs ); + //LeaveCriticalSection( &ddcs ); retStatus = FALSE; break; diff --git a/reactos/dll/directx/ddraw/rosdraw.h b/reactos/dll/directx/ddraw/rosdraw.h index 40742f6c612..0b5468524ea 100644 --- a/reactos/dll/directx/ddraw/rosdraw.h +++ b/reactos/dll/directx/ddraw/rosdraw.h @@ -41,13 +41,31 @@ VOID Cleanup(LPDIRECTDRAW7 iface); #define DxHeapMemFree(p) HeapFree(GetProcessHeap(), 0, p); \ p = NULL; */ +//#define DxHeapMemAlloc(p, m) { \ +// p = malloc(m); \ +// if (p != NULL) \ +// { \ +// ZeroMemory(p,m); \ +// } \ +// } + +/* a stupied bug in GCC it crash when malloc return NULL */ #define DxHeapMemAlloc(p, m) { \ - p = malloc(m); \ - if (p != NULL) \ - { \ - ZeroMemory(p,m); \ - } \ + _SEH_TRY \ + { \ + p = malloc(m); \ + if (p != NULL) \ + { \ + ZeroMemory(p,m); \ + } \ + } \ + _SEH_HANDLE \ + { \ + p = NULL; \ + } \ + _SEH_END; \ } + #define DxHeapMemFree(p) { \ free(p); \ p = NULL; \ diff --git a/reactos/dll/directx/ddraw/startup.c b/reactos/dll/directx/ddraw/startup.c index ade20259be2..0232e1e4773 100644 --- a/reactos/dll/directx/ddraw/startup.c +++ b/reactos/dll/directx/ddraw/startup.c @@ -13,6 +13,9 @@ #include "d3dhal.h" #include "ddrawgdi.h" +/* PSEH for SEH Support */ +#include + DDRAWI_DIRECTDRAW_GBL ddgbl; DDRAWI_DDRAWSURFACE_GBL ddSurfGbl; @@ -34,6 +37,8 @@ Create_DirectDraw (LPGUID pGUID, LPDIRECTDRAW* pIface, return DDERR_INVALIDPARAMS; } + DX_STUB_str("1 me\n"); + This = (LPDDRAWI_DIRECTDRAW_INT)*pIface; #if 0 @@ -107,18 +112,29 @@ Create_DirectDraw (LPGUID pGUID, LPDIRECTDRAW* pIface, LPDDRAWI_DIRECTDRAW_INT memThis; DxHeapMemAlloc(memThis, sizeof(DDRAWI_DIRECTDRAW_INT)); - This = memThis; - if (This == NULL) - { - if (memThis != NULL) - DxHeapMemFree(memThis); + if (memThis != NULL) + { + This = memThis; + if (This == NULL) + { + if (memThis != NULL) + DxHeapMemFree(memThis); + + DX_STUB_str("DDERR_OUTOFMEMORY"); + return DDERR_OUTOFMEMORY; + } + } + else + { DX_STUB_str("DDERR_OUTOFMEMORY"); return DDERR_OUTOFMEMORY; } /* Fixme release memory alloc if we fail */ + DxHeapMemAlloc(This->lpLcl, sizeof(DDRAWI_DIRECTDRAW_INT)); + if (This->lpLcl == NULL) { DX_STUB_str("DDERR_OUTOFMEMORY"); @@ -126,6 +142,8 @@ Create_DirectDraw (LPGUID pGUID, LPDIRECTDRAW* pIface, } #endif + DX_STUB_str("6 me\n"); + This->lpLcl->lpGbl = &ddgbl; *pIface = (LPDIRECTDRAW)This; @@ -472,17 +490,6 @@ StartDirectDrawHal(LPDIRECTDRAW iface, BOOL reenable) // FIXME Close DX fristcall and second call return DD_FALSE; } - - - { - char buffer[2048]; - sprintf ( buffer, "test %d %d\n", mpFourCC, mHALInfo.ddCaps.dwNumFourCCCodes); - OutputDebugStringA(buffer); - } - - // count = mHALInfo.ddCaps.dwNumFourCCCodes; - - DX_STUB_str("Here\n"); /* Alloc mpFourCC */ if (This->lpLcl->lpGbl->lpdwFourCC != NULL) @@ -490,24 +497,18 @@ StartDirectDrawHal(LPDIRECTDRAW iface, BOOL reenable) DxHeapMemFree(This->lpLcl->lpGbl->lpdwFourCC); } - // if (mHALInfo.ddCaps.dwNumFourCCCodes > 0 ) - // { - //mpFourCC = (DWORD *) DxHeapMemAlloc( sizeof(DWORD) * 21); - /* DrFred uncomet line 499 see if u getting werid crash in - * u computer, run the ddraw_test around 3-4 times - */ - //DxHeapMemAlloc(mpFourCC, sizeof(DWORD) * 21); + if (mHALInfo.ddCaps.dwNumFourCCCodes > 0 ) + { + DxHeapMemAlloc(mpFourCC, (sizeof(DWORD) * (mHALInfo.ddCaps.dwNumFourCCCodes)) + sizeof(DWORD) ); - // mpFourCC = (DWORD *) DxHeapMemAlloc(sizeof(DWORD) * (mHALInfo.ddCaps.dwNumFourCCCodes + 2)); - /* if (mpFourCC == NULL) { DxHeapMemFree(ddgbl.lpDDCBtmp); // FIXME Close DX fristcall and second call return DD_FALSE; } - */ - // } + + } DX_STUB_str("Here\n");