From abf25617a08d6b37e0bde69b2dc8cc1963de4380 Mon Sep 17 00:00:00 2001 From: "seer-by-sentry[bot]" <157164994+seer-by-sentry[bot]@users.noreply.github.com> Date: Sun, 25 Jan 2026 22:09:00 +0000 Subject: [PATCH] Enhance DX8 texture and surface null/error checks --- .../Source/WWVegas/WW3D2/missingtexture.cpp | 15 +++++++++--- .../Source/WWVegas/WW3D2/dx8wrapper.cpp | 23 ++++++++++++++++++- .../Source/WWVegas/WW3D2/dx8wrapper.cpp | 23 ++++++++++++++++++- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/Core/Libraries/Source/WWVegas/WW3D2/missingtexture.cpp b/Core/Libraries/Source/WWVegas/WW3D2/missingtexture.cpp index 0a2867b12b..3403f00cff 100644 --- a/Core/Libraries/Source/WWVegas/WW3D2/missingtexture.cpp +++ b/Core/Libraries/Source/WWVegas/WW3D2/missingtexture.cpp @@ -98,9 +98,18 @@ void MissingTexture::_Init() DX8_ErrorCode(tex->UnlockRect(0)); for (unsigned i=1;iGetLevelCount();++i) { - IDirect3DSurface8 *src,*dst; - DX8_ErrorCode(tex->GetSurfaceLevel(i-1,&src)); - DX8_ErrorCode(tex->GetSurfaceLevel(i,&dst)); + IDirect3DSurface8 *src = nullptr; + IDirect3DSurface8 *dst = nullptr; + HRESULT hr_src = tex->GetSurfaceLevel(i-1,&src); + HRESULT hr_dst = tex->GetSurfaceLevel(i,&dst); + + // Validate both GetSurfaceLevel calls succeeded and surfaces are valid + if (FAILED(hr_src) || src == nullptr || FAILED(hr_dst) || dst == nullptr) { + WWDEBUG_SAY(("Error: GetSurfaceLevel failed or returned NULL surface in MissingTexture::_Init")); + if (src != nullptr) src->Release(); + if (dst != nullptr) dst->Release(); + continue; + } DX8_ErrorCode(D3DXLoadSurfaceFromSurface( dst, diff --git a/Generals/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp b/Generals/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp index bf89ed36b4..0690bffd8d 100644 --- a/Generals/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp +++ b/Generals/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp @@ -2388,6 +2388,13 @@ IDirect3DTexture8 * DX8Wrapper::_Create_DX8_Texture { DX8_THREAD_ASSERT(); DX8_Assert(); + + // Validate surface parameter + if (surface == nullptr) { + WWDEBUG_SAY(("Error: NULL surface passed to _Create_DX8_Texture")); + return MissingTexture::_Get_Missing_Texture(); + } + IDirect3DTexture8 *texture = nullptr; D3DSURFACE_DESC surface_desc; @@ -2398,10 +2405,24 @@ IDirect3DTexture8 * DX8Wrapper::_Create_DX8_Texture // not in a supported texture format. WW3DFormat format=D3DFormat_To_WW3DFormat(surface_desc.Format); texture = _Create_DX8_Texture(surface_desc.Width, surface_desc.Height, format, mip_level_count); + + // Validate texture was created successfully + if (texture == nullptr) { + WWDEBUG_SAY(("Error: Failed to create texture in _Create_DX8_Texture")); + return MissingTexture::_Get_Missing_Texture(); + } // Copy the surface to the texture IDirect3DSurface8 *tex_surface = nullptr; - texture->GetSurfaceLevel(0, &tex_surface); + HRESULT hr = texture->GetSurfaceLevel(0, &tex_surface); + + // Validate GetSurfaceLevel succeeded and tex_surface is valid + if (FAILED(hr) || tex_surface == nullptr) { + WWDEBUG_SAY(("Error: GetSurfaceLevel failed or returned NULL surface in _Create_DX8_Texture")); + texture->Release(); + return MissingTexture::_Get_Missing_Texture(); + } + DX8_ErrorCode(D3DXLoadSurfaceFromSurface(tex_surface, nullptr, nullptr, surface, nullptr, nullptr, D3DX_FILTER_BOX, 0)); tex_surface->Release(); diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp index 01047f436a..296a0ac219 100644 --- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp +++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp @@ -2646,6 +2646,13 @@ IDirect3DTexture8 * DX8Wrapper::_Create_DX8_Texture { DX8_THREAD_ASSERT(); DX8_Assert(); + + // Validate surface parameter + if (surface == nullptr) { + WWDEBUG_SAY(("Error: NULL surface passed to _Create_DX8_Texture")); + return MissingTexture::_Get_Missing_Texture(); + } + IDirect3DTexture8 *texture = nullptr; D3DSURFACE_DESC surface_desc; @@ -2656,10 +2663,24 @@ IDirect3DTexture8 * DX8Wrapper::_Create_DX8_Texture // not in a supported texture format. WW3DFormat format=D3DFormat_To_WW3DFormat(surface_desc.Format); texture = _Create_DX8_Texture(surface_desc.Width, surface_desc.Height, format, mip_level_count); + + // Validate texture was created successfully + if (texture == nullptr) { + WWDEBUG_SAY(("Error: Failed to create texture in _Create_DX8_Texture")); + return MissingTexture::_Get_Missing_Texture(); + } // Copy the surface to the texture IDirect3DSurface8 *tex_surface = nullptr; - texture->GetSurfaceLevel(0, &tex_surface); + HRESULT hr = texture->GetSurfaceLevel(0, &tex_surface); + + // Validate GetSurfaceLevel succeeded and tex_surface is valid + if (FAILED(hr) || tex_surface == nullptr) { + WWDEBUG_SAY(("Error: GetSurfaceLevel failed or returned NULL surface in _Create_DX8_Texture")); + texture->Release(); + return MissingTexture::_Get_Missing_Texture(); + } + DX8_ErrorCode(D3DXLoadSurfaceFromSurface(tex_surface, nullptr, nullptr, surface, nullptr, nullptr, D3DX_FILTER_BOX, 0)); tex_surface->Release();