Index: ddraw/ddraw.cpp |
— | — | @@ -16,6 +16,7 @@ |
17 | 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
18 | 18 |
|
19 | 19 | #include "common.h"
|
| 20 | +#include "util.h"
|
20 | 21 | #include "ddraw.h"
|
21 | 22 | #include "texture.h"
|
22 | 23 | #include "glutil.h"
|
— | — | @@ -39,40 +40,6 @@ |
40 | 41 | glRenderer *renderer = NULL;
|
41 | 42 | glDirectDraw7 *dxglinterface = NULL;
|
42 | 43 |
|
43 | | -/**
|
44 | | - * Tests if a pointer is valid for reading from. Compile in Visual C++ with /EHa
|
45 | | - * enabled Structed Exception Handling in C++ code, to prevent crashes on invalid
|
46 | | - * pointers.
|
47 | | - * @param ptr
|
48 | | - * Pointer to test for validity.
|
49 | | - * @return
|
50 | | - * Returns false if the pointer is valid, or true if an error occurs.
|
51 | | - */
|
52 | | -bool IsBadReadPointer(void *ptr)
|
53 | | -{
|
54 | | - TRACE_ENTER(1,14,ptr);
|
55 | | - char a;
|
56 | | - try
|
57 | | - {
|
58 | | - a = *(char*)ptr;
|
59 | | - if(a == *(char*)ptr)
|
60 | | - {
|
61 | | - TRACE_EXIT(21,0);
|
62 | | - return false;
|
63 | | - }
|
64 | | - else
|
65 | | - {
|
66 | | - TRACE_EXIT(21,1);
|
67 | | - return true;
|
68 | | - }
|
69 | | - }
|
70 | | - catch(...)
|
71 | | - {
|
72 | | - TRACE_EXIT(21,1);
|
73 | | - return true;
|
74 | | - }
|
75 | | -}
|
76 | | -
|
77 | 44 | void InitGL(int width, int height, int bpp, bool fullscreen, HWND hWnd, glDirectDraw7 *glDD7)
|
78 | 45 | {
|
79 | 46 | TRACE_ENTER(6,11,width,11,height,11,bpp,21,fullscreen,13,hWnd,14,glDD7);
|
— | — | @@ -191,6 +158,7 @@ |
192 | 159 | HRESULT WINAPI DirectDrawCreate(GUID FAR *lpGUID, LPDIRECTDRAW FAR *lplpDD, IUnknown FAR *pUnkOuter)
|
193 | 160 | {
|
194 | 161 | TRACE_ENTER(3,24,lpGUID,14,lplpDD,14,pUnkOuter);
|
| 162 | + if(!lplpDD) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
195 | 163 | HRESULT ret;
|
196 | 164 | if(gllock || IsCallerOpenGL(_ReturnAddress()))
|
197 | 165 | {
|
— | — | @@ -292,6 +260,7 @@ |
293 | 261 | HRESULT WINAPI DirectDrawCreateEx(GUID FAR *lpGUID, LPVOID *lplpDD, REFIID iid, IUnknown FAR *pUnkOuter)
|
294 | 262 | {
|
295 | 263 | TRACE_ENTER(4,24,lpGUID,14,lplpDD,24,&iid,14,pUnkOuter);
|
| 264 | + if(!lplpDD) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
296 | 265 | if(dxglinterface)
|
297 | 266 | {
|
298 | 267 | TRACE_EXIT(23,DDERR_DIRECTDRAWALREADYCREATED);
|
Index: ddraw/ddraw.h |
— | — | @@ -58,6 +58,7 @@ |
59 | 59 |
|
60 | 60 | extern DXGLCFG dxglcfg;
|
61 | 61 | extern bool gllock;
|
| 62 | +extern const GUID device_template;
|
62 | 63 | extern DWORD timer;
|
63 | 64 | extern int vsyncstatus;
|
64 | 65 | class glRenderer;
|
Index: ddraw/ddraw.vcxproj |
— | — | @@ -298,6 +298,7 @@ |
299 | 299 | <ClInclude Include="shaders.h" />
|
300 | 300 | <ClInclude Include="texture.h" />
|
301 | 301 | <ClInclude Include="trace.h" />
|
| 302 | + <ClInclude Include="util.h" />
|
302 | 303 | </ItemGroup>
|
303 | 304 | <ItemGroup>
|
304 | 305 | <ClCompile Include="ddraw.cpp" />
|
— | — | @@ -361,6 +362,7 @@ |
362 | 363 | <ClCompile Include="shaders.cpp" />
|
363 | 364 | <ClCompile Include="texture.cpp" />
|
364 | 365 | <ClCompile Include="trace.cpp" />
|
| 366 | + <ClCompile Include="util.cpp" />
|
365 | 367 | </ItemGroup>
|
366 | 368 | <ItemGroup>
|
367 | 369 | <ResourceCompile Include="ddraw.rc" />
|
Index: ddraw/ddraw.vcxproj.filters |
— | — | @@ -128,6 +128,9 @@ |
129 | 129 | <ClInclude Include="trace.h">
|
130 | 130 | <Filter>Header Files</Filter>
|
131 | 131 | </ClInclude>
|
| 132 | + <ClInclude Include="util.h">
|
| 133 | + <Filter>Header Files</Filter>
|
| 134 | + </ClInclude>
|
132 | 135 | </ItemGroup>
|
133 | 136 | <ItemGroup>
|
134 | 137 | <ClCompile Include="ddraw.cpp">
|
— | — | @@ -214,6 +217,9 @@ |
215 | 218 | <ClCompile Include="trace.cpp">
|
216 | 219 | <Filter>Source Files</Filter>
|
217 | 220 | </ClCompile>
|
| 221 | + <ClCompile Include="util.cpp">
|
| 222 | + <Filter>Source Files</Filter>
|
| 223 | + </ClCompile>
|
218 | 224 | </ItemGroup>
|
219 | 225 | <ItemGroup>
|
220 | 226 | <ResourceCompile Include="ddraw.rc">
|
Index: ddraw/glDirectDraw.cpp |
— | — | @@ -16,6 +16,7 @@ |
17 | 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
18 | 18 |
|
19 | 19 | #include "common.h"
|
| 20 | +#include "util.h"
|
20 | 21 | #include "shaders.h"
|
21 | 22 | #include "ddraw.h"
|
22 | 23 | #include "glDirect3D.h"
|
— | — | @@ -569,6 +570,24 @@ |
570 | 571 | TRACE_ENTER(3,14,this,24,lpGUID,14,pUnkOuter);
|
571 | 572 | dxglinterface = this;
|
572 | 573 | initialized = false;
|
| 574 | + if(((ULONG_PTR)lpGUID > 2) && IsBadReadPointer(lpGUID))
|
| 575 | + {
|
| 576 | + error = DDERR_INVALIDPARAMS ;
|
| 577 | + TRACE_EXIT(-1,0);
|
| 578 | + return;
|
| 579 | + }
|
| 580 | + GUID guid;
|
| 581 | + if((ULONG_PTR)lpGUID > 2)
|
| 582 | + {
|
| 583 | + guid = *lpGUID;
|
| 584 | + guid.Data1 &= 0xFFFFFF00;
|
| 585 | + if(guid != device_template)
|
| 586 | + {
|
| 587 | + error = DDERR_INVALIDDIRECTDRAWGUID;
|
| 588 | + TRACE_EXIT(-1,0);
|
| 589 | + return;
|
| 590 | + }
|
| 591 | + }
|
573 | 592 | if(pUnkOuter)
|
574 | 593 | {
|
575 | 594 | error = DDERR_INVALIDPARAMS ;
|
— | — | @@ -585,28 +604,31 @@ |
586 | 605 | {
|
587 | 606 | TRACE_ENTER(1,14,this);
|
588 | 607 | dxglinterface = NULL;
|
589 | | - if(glD3D7) glD3D7->Release();
|
590 | | - RestoreDisplayMode();
|
591 | | - if(clippers)
|
| 608 | + if(initialized)
|
592 | 609 | {
|
593 | | - for(int i = 0; i < clippercount; i++)
|
| 610 | + if(glD3D7) glD3D7->Release();
|
| 611 | + RestoreDisplayMode();
|
| 612 | + if(clippers)
|
594 | 613 | {
|
595 | | - if(clippers[i]) clippers[i]->Release();
|
596 | | - clippers[i] = NULL;
|
| 614 | + for(int i = 0; i < clippercount; i++)
|
| 615 | + {
|
| 616 | + if(clippers[i]) clippers[i]->Release();
|
| 617 | + clippers[i] = NULL;
|
| 618 | + }
|
| 619 | + free(clippers);
|
597 | 620 | }
|
598 | | - free(clippers);
|
599 | | - }
|
600 | | - if(surfaces)
|
601 | | - {
|
602 | | - for(int i = 0; i < surfacecount; i++)
|
| 621 | + if(surfaces)
|
603 | 622 | {
|
604 | | - if(surfaces[i]) delete surfaces[i];
|
605 | | - surfaces[i] = NULL;
|
| 623 | + for(int i = 0; i < surfacecount; i++)
|
| 624 | + {
|
| 625 | + if(surfaces[i]) delete surfaces[i];
|
| 626 | + surfaces[i] = NULL;
|
| 627 | + }
|
| 628 | + free(surfaces);
|
606 | 629 | }
|
607 | | - free(surfaces);
|
| 630 | + if(renderer) delete renderer;
|
| 631 | + renderer = NULL;
|
608 | 632 | }
|
609 | | - if(renderer) delete renderer;
|
610 | | - renderer = NULL;
|
611 | 633 | TRACE_EXIT(-1,0);
|
612 | 634 | }
|
613 | 635 |
|
— | — | @@ -802,6 +824,7 @@ |
803 | 825 | TRACE_EXIT(23,DD_OK);
|
804 | 826 | return DD_OK;
|
805 | 827 | }
|
| 828 | +
|
806 | 829 | HRESULT WINAPI glDirectDraw7::CreateSurface(LPDDSURFACEDESC2 lpDDSurfaceDesc2, LPDIRECTDRAWSURFACE7 FAR *lplpDDSurface, IUnknown FAR *pUnkOuter)
|
807 | 830 | {
|
808 | 831 | TRACE_ENTER(4,14,this,14,lpDDSurfaceDesc2,14,lplpDDSurface,14,pUnkOuter);
|
— | — | @@ -808,6 +831,17 @@ |
809 | 832 | if(!this) TRACE_RET(HRESULT,23,DDERR_INVALIDOBJECT);
|
810 | 833 | if(!lpDDSurfaceDesc2) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
811 | 834 | if(pUnkOuter) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
| 835 | + if(lpDDSurfaceDesc2->dwSize < sizeof(DDSURFACEDESC2)) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
| 836 | + TRACE_RET(HRESULT,23,CreateSurface2(lpDDSurfaceDesc2,lplpDDSurface,pUnkOuter));
|
| 837 | +}
|
| 838 | +
|
| 839 | +
|
| 840 | +HRESULT glDirectDraw7::CreateSurface2(LPDDSURFACEDESC2 lpDDSurfaceDesc2, LPDIRECTDRAWSURFACE7 FAR *lplpDDSurface, IUnknown FAR *pUnkOuter)
|
| 841 | +{
|
| 842 | + TRACE_ENTER(4,14,this,14,lpDDSurfaceDesc2,14,lplpDDSurface,14,pUnkOuter);
|
| 843 | + if(!this) TRACE_RET(HRESULT,23,DDERR_INVALIDOBJECT);
|
| 844 | + if(!lpDDSurfaceDesc2) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
| 845 | + if(pUnkOuter) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
812 | 846 | if(primary && (lpDDSurfaceDesc2->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && (renderer->hRC == primary->hRC) )
|
813 | 847 | {
|
814 | 848 | if(primarylost)
|
— | — | @@ -1658,8 +1692,10 @@ |
1659 | 1693 | {
|
1660 | 1694 | TRACE_ENTER(4,14,this,14,lpDDSurfaceDesc,14,lplpDDSurface,14,pUnkOuter);
|
1661 | 1695 | if(!this) TRACE_RET(HRESULT,23,DDERR_INVALIDOBJECT);
|
| 1696 | + if(!lpDDSurfaceDesc) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
| 1697 | + if(lpDDSurfaceDesc->dwSize < sizeof(DDSURFACEDESC)) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
1662 | 1698 | LPDIRECTDRAWSURFACE7 lpDDS7;
|
1663 | | - HRESULT err = glDD7->CreateSurface((LPDDSURFACEDESC2)lpDDSurfaceDesc,&lpDDS7,pUnkOuter);
|
| 1699 | + HRESULT err = glDD7->CreateSurface2((LPDDSURFACEDESC2)lpDDSurfaceDesc,&lpDDS7,pUnkOuter);
|
1664 | 1700 | if(err == DD_OK)
|
1665 | 1701 | {
|
1666 | 1702 | lpDDS7->QueryInterface(IID_IDirectDrawSurface,(LPVOID*) lplpDDSurface);
|
— | — | @@ -1844,8 +1880,10 @@ |
1845 | 1881 | TRACE_ENTER(4,14,this,14,lpDDSurfaceDesc,14,lplpDDSurface,14,pUnkOuter);
|
1846 | 1882 | if(!this) TRACE_RET(HRESULT,23,DDERR_INVALIDOBJECT);
|
1847 | 1883 | if(!lplpDDSurface) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
| 1884 | + if(!lpDDSurfaceDesc) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
| 1885 | + if(lpDDSurfaceDesc->dwSize < sizeof(DDSURFACEDESC)) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
1848 | 1886 | LPDIRECTDRAWSURFACE7 lpDDS7;
|
1849 | | - HRESULT err = glDD7->CreateSurface((LPDDSURFACEDESC2)lpDDSurfaceDesc,&lpDDS7,pUnkOuter);
|
| 1887 | + HRESULT err = glDD7->CreateSurface2((LPDDSURFACEDESC2)lpDDSurfaceDesc,&lpDDS7,pUnkOuter);
|
1850 | 1888 | if(err == DD_OK)
|
1851 | 1889 | {
|
1852 | 1890 | lpDDS7->QueryInterface(IID_IDirectDrawSurface,(LPVOID*) lplpDDSurface);
|
— | — | @@ -2053,8 +2091,10 @@ |
2054 | 2092 | TRACE_ENTER(4,14,this,14,lpDDSurfaceDesc,14,lplpDDSurface,14,pUnkOuter);
|
2055 | 2093 | if(!this) TRACE_RET(HRESULT,23,DDERR_INVALIDOBJECT);
|
2056 | 2094 | if(!lplpDDSurface) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
| 2095 | + if(!lpDDSurfaceDesc) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
| 2096 | + if(lpDDSurfaceDesc->dwSize < sizeof(DDSURFACEDESC2)) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
|
2057 | 2097 | LPDIRECTDRAWSURFACE7 lpDDS7;
|
2058 | | - HRESULT err = glDD7->CreateSurface((LPDDSURFACEDESC2)lpDDSurfaceDesc,&lpDDS7,pUnkOuter);
|
| 2098 | + HRESULT err = glDD7->CreateSurface2((LPDDSURFACEDESC2)lpDDSurfaceDesc,&lpDDS7,pUnkOuter);
|
2059 | 2099 | if(err == DD_OK)
|
2060 | 2100 | {
|
2061 | 2101 | lpDDS7->QueryInterface(IID_IDirectDrawSurface4,(LPVOID*) lplpDDSurface);
|
Index: ddraw/glDirectDraw.h |
— | — | @@ -70,6 +70,7 @@ |
71 | 71 | HRESULT WINAPI EvaluateMode(DWORD dwFlags, DWORD *pSecondsUntilTimeout);
|
72 | 72 |
|
73 | 73 | // internal functions
|
| 74 | + HRESULT CreateSurface2(LPDDSURFACEDESC2 lpDDSurfaceDesc2, LPDIRECTDRAWSURFACE7 FAR *lplpDDSurface, IUnknown FAR *pUnkOuter);
|
74 | 75 | HRESULT err() {return error;}
|
75 | 76 | void RemoveSurface(glDirectDrawSurface7 *surface);
|
76 | 77 | void GetSizes(LONG *sizes);
|
Index: ddraw/util.cpp |
— | — | @@ -0,0 +1,58 @@ |
| 2 | +// DXGL
|
| 3 | +// Copyright (C) 2013 William Feely
|
| 4 | +
|
| 5 | +// This library is free software; you can redistribute it and/or
|
| 6 | +// modify it under the terms of the GNU Lesser General Public
|
| 7 | +// License as published by the Free Software Foundation; either
|
| 8 | +// version 2.1 of the License, or (at your option) any later version.
|
| 9 | +
|
| 10 | +// This library is distributed in the hope that it will be useful,
|
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 13 | +// Lesser General Public License for more details.
|
| 14 | +
|
| 15 | +// You should have received a copy of the GNU Lesser General Public
|
| 16 | +// License along with this library; if not, write to the Free Software
|
| 17 | +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
| 18 | +
|
| 19 | +#include "common.h"
|
| 20 | +#include "util.h"
|
| 21 | +
|
| 22 | +/**
|
| 23 | + * Tests if a pointer is valid for reading from. Compile in Visual C++ with /EHa
|
| 24 | + * enabled Structed Exception Handling in C++ code, to prevent crashes on invalid
|
| 25 | + * pointers.
|
| 26 | + * @param ptr
|
| 27 | + * Pointer to test for validity.
|
| 28 | + * @return
|
| 29 | + * Returns false if the pointer is valid, or true if an error occurs.
|
| 30 | + */
|
| 31 | +bool IsBadReadPointer(void *ptr)
|
| 32 | +{
|
| 33 | + TRACE_ENTER(1,14,ptr);
|
| 34 | + if(!ptr)
|
| 35 | + {
|
| 36 | + TRACE_EXIT(21,1);
|
| 37 | + return true;
|
| 38 | + }
|
| 39 | + char a;
|
| 40 | + try
|
| 41 | + {
|
| 42 | + a = *(char*)ptr;
|
| 43 | + if(a == *(char*)ptr)
|
| 44 | + {
|
| 45 | + TRACE_EXIT(21,0);
|
| 46 | + return false;
|
| 47 | + }
|
| 48 | + else
|
| 49 | + {
|
| 50 | + TRACE_EXIT(21,1);
|
| 51 | + return true;
|
| 52 | + }
|
| 53 | + }
|
| 54 | + catch(...)
|
| 55 | + {
|
| 56 | + TRACE_EXIT(21,1);
|
| 57 | + return true;
|
| 58 | + }
|
| 59 | +}
|
Index: ddraw/util.h |
— | — | @@ -0,0 +1,25 @@ |
| 2 | +// DXGL
|
| 3 | +// Copyright (C) 2013 William Feely
|
| 4 | +
|
| 5 | +// This library is free software; you can redistribute it and/or
|
| 6 | +// modify it under the terms of the GNU Lesser General Public
|
| 7 | +// License as published by the Free Software Foundation; either
|
| 8 | +// version 2.1 of the License, or (at your option) any later version.
|
| 9 | +
|
| 10 | +// This library is distributed in the hope that it will be useful,
|
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 13 | +// Lesser General Public License for more details.
|
| 14 | +
|
| 15 | +// You should have received a copy of the GNU Lesser General Public
|
| 16 | +// License along with this library; if not, write to the Free Software
|
| 17 | +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
| 18 | +
|
| 19 | +#pragma once
|
| 20 | +#ifndef _UTIL_H
|
| 21 | +#define _UTIL_H
|
| 22 | +
|
| 23 | +
|
| 24 | +bool IsBadReadPointer(void *ptr);
|
| 25 | +
|
| 26 | +#endif |
\ No newline at end of file |