Skip to content

Commit

Permalink
Add assertions to ensure texture width and height >= 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Robyt3 committed Jul 20, 2023
1 parent 2e39c23 commit 5afc412
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/engine/client/graphics_threaded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ static bool ConvertToRGBA(uint8_t *pDest, const uint8_t *pSrc, size_t SrcWidth,

int CGraphics_Threaded::LoadTextureRawSub(CTextureHandle TextureID, int x, int y, int Width, int Height, int Format, const void *pData)
{
dbg_assert(Width > 0, "LoadTextureRawSub: Width is negative or zero");
dbg_assert(Height > 0, "LoadTextureRawSub: Height is negative or zero");

CCommandBuffer::SCommand_Texture_Update Cmd;
Cmd.m_Slot = TextureID.Id();
Cmd.m_X = x;
Expand All @@ -353,6 +356,8 @@ int CGraphics_Threaded::LoadTextureRawSub(CTextureHandle TextureID, int x, int y

IGraphics::CTextureHandle CGraphics_Threaded::LoadSpriteTextureImpl(CImageInfo &FromImageInfo, int x, int y, int w, int h)
{
dbg_assert(w > 0, "LoadSpriteTextureImpl: Width is negative or zero");
dbg_assert(h > 0, "LoadSpriteTextureImpl: Height is negative or zero");
int bpp = ImageFormatToPixelSize(FromImageInfo.m_Format);

m_vSpriteHelper.resize((size_t)w * h * bpp);
Expand Down Expand Up @@ -421,6 +426,9 @@ bool CGraphics_Threaded::IsSpriteTextureFullyTransparent(CImageInfo &FromImageIn

IGraphics::CTextureHandle CGraphics_Threaded::LoadTextureRaw(int Width, int Height, int Format, const void *pData, int StoreFormat, int Flags, const char *pTexName)
{
dbg_assert(Width > 0, "LoadTextureRaw: Width is negative or zero");
dbg_assert(Height > 0, "LoadTextureRaw: Height is negative or zero");

// don't waste memory on texture if we are stress testing
#ifdef CONF_DEBUG
if(g_Config.m_DbgStress && m_InvalidTexture.IsValid())
Expand Down Expand Up @@ -533,6 +541,9 @@ IGraphics::CTextureHandle CGraphics_Threaded::InvalidTexture() const

bool CGraphics_Threaded::LoadTextTextures(int Width, int Height, CTextureHandle &TextTexture, CTextureHandle &TextOutlineTexture, void *pTextData, void *pTextOutlineData)
{
dbg_assert(Width > 0, "LoadTextTextures: Width is negative or zero");
dbg_assert(Height > 0, "LoadTextTextures: Height is negative or zero");

if(Width == 0 || Height == 0)
return false;

Expand Down Expand Up @@ -608,6 +619,9 @@ bool CGraphics_Threaded::UnloadTextTextures(CTextureHandle &TextTexture, CTextur

bool CGraphics_Threaded::UpdateTextTexture(CTextureHandle TextureID, int x, int y, int Width, int Height, const void *pData)
{
dbg_assert(Width > 0, "UpdateTextTexture: Width is negative or zero");
dbg_assert(Height > 0, "UpdateTextTexture: Height is negative or zero");

CCommandBuffer::SCommand_TextTexture_Update Cmd;
Cmd.m_Slot = TextureID.Id();
Cmd.m_X = x;
Expand Down Expand Up @@ -779,6 +793,11 @@ bool CGraphics_Threaded::IsImageFormatRGBA(const char *pFileName, CImageInfo &Im

void CGraphics_Threaded::CopyTextureBufferSub(uint8_t *pDestBuffer, uint8_t *pSourceBuffer, int FullWidth, int FullHeight, int ColorChannelCount, int SubOffsetX, int SubOffsetY, int SubCopyWidth, int SubCopyHeight)
{
dbg_assert(FullWidth > 0, "CopyTextureBufferSub: FullWidth is negative or zero");
dbg_assert(FullHeight > 0, "CopyTextureBufferSub: FullHeight is negative or zero");
dbg_assert(SubCopyWidth > 0, "CopyTextureBufferSub: SubCopyWidth is negative or zero");
dbg_assert(SubCopyHeight > 0, "CopyTextureBufferSub: SubCopyHeight is negative or zero");

for(int Y = 0; Y < SubCopyHeight; ++Y)
{
int ImgOffset = ((SubOffsetY + Y) * FullWidth * ColorChannelCount) + (SubOffsetX * ColorChannelCount);
Expand All @@ -789,6 +808,15 @@ void CGraphics_Threaded::CopyTextureBufferSub(uint8_t *pDestBuffer, uint8_t *pSo

void CGraphics_Threaded::CopyTextureFromTextureBufferSub(uint8_t *pDestBuffer, int DestWidth, int DestHeight, uint8_t *pSourceBuffer, int SrcWidth, int SrcHeight, int ColorChannelCount, int SrcSubOffsetX, int SrcSubOffsetY, int SrcSubCopyWidth, int SrcSubCopyHeight)
{
dbg_assert(DestWidth > 0, "CopyTextureBufferSub: DestWidth is negative or zero");
dbg_assert(DestHeight > 0, "CopyTextureBufferSub: DestHeight is negative or zero");
dbg_assert(SrcWidth > 0, "CopyTextureBufferSub: SrcWidth is negative or zero");
dbg_assert(SrcHeight > 0, "CopyTextureBufferSub: SrcHeight is negative or zero");
dbg_assert(SrcSubOffsetX > 0, "CopyTextureBufferSub: SrcSubOffsetX is negative or zero");
dbg_assert(SrcSubOffsetY > 0, "CopyTextureBufferSub: SrcSubOffsetY is negative or zero");
dbg_assert(SrcSubCopyWidth > 0, "CopyTextureBufferSub: SrcSubCopyWidth is negative or zero");
dbg_assert(SrcSubCopyHeight > 0, "CopyTextureBufferSub: SrcSubCopyHeight is negative or zero");

for(int Y = 0; Y < SrcSubCopyHeight; ++Y)
{
int SrcImgOffset = ((SrcSubOffsetY + Y) * SrcWidth * ColorChannelCount) + (SrcSubOffsetX * ColorChannelCount);
Expand Down

0 comments on commit 5afc412

Please sign in to comment.