From be2c8140d014178792dadb3f55f0f113dbce21c6 Mon Sep 17 00:00:00 2001 From: pionere Date: Sun, 22 Sep 2024 10:56:54 +0200 Subject: [PATCH] make engine.h more robust - use size_t instead of DWORD in the templates - use standard sized types (uint32_t/uint16_t) instead of DWORD/WORD everywhere else - do not use std::min --- Source/engine.h | 41 ++++++++++++++++++++++------------------- tools/patcher/engine.h | 41 ++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/Source/engine.h b/Source/engine.h index dea2d882341..0e780294b3e 100644 --- a/Source/engine.h +++ b/Source/engine.h @@ -21,19 +21,19 @@ DEVILUTION_BEGIN_NAMESPACE inline const BYTE* CelGetFrameStart(const BYTE* pCelBuff, int nCel) { - const DWORD* pFrameTable; + const uint32_t* pFrameTable; - pFrameTable = (const DWORD*)pCelBuff; + pFrameTable = (const uint32_t*)pCelBuff; return &pCelBuff[SwapLE32(pFrameTable[nCel])]; } inline const BYTE* CelGetFrame(const BYTE* pCelBuff, int nCel, int* nDataSize) { - const DWORD* pFrameTable; - DWORD nCellStart; + const uint32_t* pFrameTable; + uint32_t nCellStart; - pFrameTable = (const DWORD*)&pCelBuff[nCel * 4]; + pFrameTable = (const uint32_t*)&pCelBuff[nCel * 4]; nCellStart = SwapLE32(pFrameTable[0]); *nDataSize = SwapLE32(pFrameTable[1]) - nCellStart; return &pCelBuff[nCellStart]; @@ -41,11 +41,11 @@ inline const BYTE* CelGetFrame(const BYTE* pCelBuff, int nCel, int* nDataSize) inline const BYTE* CelGetFrameClipped(const BYTE* pCelBuff, int nCel, int* nDataSize) { - const WORD* pFrameTable; - WORD nDataStart; + const uint16_t* pFrameTable; + uint16_t nDataStart; const BYTE* pRLEBytes = CelGetFrame(pCelBuff, nCel, nDataSize); - pFrameTable = (const WORD*)&pRLEBytes[0]; + pFrameTable = (const uint16_t*)&pRLEBytes[0]; nDataStart = SwapLE16(pFrameTable[0]); *nDataSize -= nDataStart; @@ -54,11 +54,11 @@ inline const BYTE* CelGetFrameClipped(const BYTE* pCelBuff, int nCel, int* nData inline const BYTE* CelGetFrameClippedAt(const BYTE* pCelBuff, int nCel, int block, int* nDataSize) { - const WORD* pFrameTable; - WORD nDataStart; + const uint16_t* pFrameTable; + uint16_t nDataStart; const BYTE* pRLEBytes = CelGetFrame(pCelBuff, nCel, nDataSize); - pFrameTable = (const WORD*)&pRLEBytes[0]; + pFrameTable = (const uint16_t*)&pRLEBytes[0]; nDataStart = SwapLE16(pFrameTable[block]); // assert(nDataStart != 0); *nDataSize -= nDataStart; @@ -99,14 +99,14 @@ BYTE* LoadFileInMem(const char* pszName, size_t* pdwFileLen = NULL); void LoadFileWithMem(const char* pszName, BYTE* p); char** LoadTxtFile(const char* name, int lines); -/* Load .CEL file and overwrite the first (unused) DWORD with nWidth */ -inline CelImageBuf* CelLoadImage(const char* name, DWORD nWidth) +/* Load .CEL file and overwrite the first (unused) uint32_t with nWidth */ +inline CelImageBuf* CelLoadImage(const char* name, uint32_t nWidth) { CelImageBuf* res; res = (CelImageBuf*)LoadFileInMem(name); #if DEBUG_MODE - res->ciFrameCnt = SwapLE32(*((DWORD*)res)); + res->ciFrameCnt = SwapLE32(*((uint32_t*)res)); #endif res->ciWidth = nWidth; return res; @@ -157,7 +157,7 @@ inline int RandRangeLow(int minVal, int maxVal) * Copy string from src to dest. * The NULL terminated content of src is copied to dest. */ -template +template inline void copy_str(char (&dest)[N1], char (&src)[N2]) { static_assert(N1 >= N2, "String does not fit the destination."); @@ -170,11 +170,13 @@ inline void copy_str(char (&dest)[N1], char (&src)[N2]) * Copy constant string from src to dest. * The whole (padded) length of the src array is copied. */ -template +template inline void copy_cstr(char (&dest)[N1], const char (&src)[N2]) { static_assert(N1 >= N2, "String does not fit the destination."); - memcpy(dest, src, std::min(N1, (DWORD)(((N2 + sizeof(int) - 1) / sizeof(int)) * sizeof(int)))); + constexpr size_t src_len = ((N2 + sizeof(int) - 1) / sizeof(int)) * sizeof(int); + constexpr size_t len = N1 >= src_len ? src_len : N1; + memcpy(dest, src, len); } /* @@ -201,13 +203,14 @@ inline void copy_pod(T& dest, const T& src) template inline void cat_str(char (&dest)[N], int& pos, const char* fmt, ...) { - int n; + int n, res; va_list va; va_start(va, fmt); n = N - pos; - pos += std::min(vsnprintf(&dest[pos], n, fmt, va), n - 1); + res = vsnprintf(&dest[pos], n, fmt, va); + pos += (res <= n - 1) ? res : n - 1; va_end(va); } diff --git a/tools/patcher/engine.h b/tools/patcher/engine.h index 5ef0dd0143e..e87f6c9a40f 100644 --- a/tools/patcher/engine.h +++ b/tools/patcher/engine.h @@ -22,19 +22,19 @@ DEVILUTION_BEGIN_NAMESPACE inline const BYTE* CelGetFrameStart(const BYTE* pCelBuff, int nCel) { - const DWORD* pFrameTable; + const uint32_t* pFrameTable; - pFrameTable = (const DWORD*)pCelBuff; + pFrameTable = (const uint32_t*)pCelBuff; return &pCelBuff[SwapLE32(pFrameTable[nCel])]; } inline const BYTE* CelGetFrame(const BYTE* pCelBuff, int nCel, int* nDataSize) { - const DWORD* pFrameTable; - DWORD nCellStart; + const uint32_t* pFrameTable; + uint32_t nCellStart; - pFrameTable = (const DWORD*)&pCelBuff[nCel * 4]; + pFrameTable = (const uint32_t*)&pCelBuff[nCel * 4]; nCellStart = SwapLE32(pFrameTable[0]); *nDataSize = SwapLE32(pFrameTable[1]) - nCellStart; return &pCelBuff[nCellStart]; @@ -42,11 +42,11 @@ inline const BYTE* CelGetFrame(const BYTE* pCelBuff, int nCel, int* nDataSize) inline const BYTE* CelGetFrameClipped(const BYTE* pCelBuff, int nCel, int* nDataSize) { - const WORD* pFrameTable; - WORD nDataStart; + const uint16_t* pFrameTable; + uint16_t nDataStart; const BYTE* pRLEBytes = CelGetFrame(pCelBuff, nCel, nDataSize); - pFrameTable = (const WORD*)&pRLEBytes[0]; + pFrameTable = (const uint16_t*)&pRLEBytes[0]; nDataStart = SwapLE16(pFrameTable[0]); *nDataSize -= nDataStart; @@ -55,11 +55,11 @@ inline const BYTE* CelGetFrameClipped(const BYTE* pCelBuff, int nCel, int* nData inline const BYTE* CelGetFrameClippedAt(const BYTE* pCelBuff, int nCel, int block, int* nDataSize) { - const WORD* pFrameTable; - WORD nDataStart; + const uint16_t* pFrameTable; + uint16_t nDataStart; const BYTE* pRLEBytes = CelGetFrame(pCelBuff, nCel, nDataSize); - pFrameTable = (const WORD*)&pRLEBytes[0]; + pFrameTable = (const uint16_t*)&pRLEBytes[0]; nDataStart = SwapLE16(pFrameTable[block]); // assert(nDataStart != 0); *nDataSize -= nDataStart; @@ -88,14 +88,14 @@ BYTE* LoadFileInMem(const char* pszName, size_t* pdwFileLen = NULL); void LoadFileWithMem(const char* pszName, BYTE* p); char** LoadTxtFile(const char* name, int lines); -/* Load .CEL file and overwrite the first (unused) DWORD with nWidth */ -inline CelImageBuf* CelLoadImage(const char* name, DWORD nWidth) +/* Load .CEL file and overwrite the first (unused) uint32_t with nWidth */ +inline CelImageBuf* CelLoadImage(const char* name, uint32_t nWidth) { CelImageBuf* res; res = (CelImageBuf*)LoadFileInMem(name); #if DEBUG_MODE - res->ciFrameCnt = SwapLE32(*((DWORD*)res)); + res->ciFrameCnt = SwapLE32(*((uint32_t*)res)); #endif res->ciWidth = nWidth; return res; @@ -134,7 +134,7 @@ BYTE* CelMerge(BYTE* celA, size_t nDataSizeA, BYTE* celB, size_t nDataSizeB); * Copy string from src to dest. * The NULL terminated content of src is copied to dest. */ -template +template inline void copy_str(char (&dest)[N1], char (&src)[N2]) { static_assert(N1 >= N2, "String does not fit the destination."); @@ -147,11 +147,13 @@ inline void copy_str(char (&dest)[N1], char (&src)[N2]) * Copy constant string from src to dest. * The whole (padded) length of the src array is copied. */ -template +template inline void copy_cstr(char (&dest)[N1], const char (&src)[N2]) { static_assert(N1 >= N2, "String does not fit the destination."); - memcpy(dest, src, std::min(N1, (DWORD)(((N2 + sizeof(int) - 1) / sizeof(int)) * sizeof(int)))); + constexpr size_t src_len = ((N2 + sizeof(int) - 1) / sizeof(int)) * sizeof(int); + constexpr size_t len = N1 >= src_len ? src_len : N1; + memcpy(dest, src, len); } /* @@ -178,13 +180,14 @@ inline void copy_pod(T& dest, const T& src) template inline void cat_str(char (&dest)[N], int& pos, const char* fmt, ...) { - int n; + int n, res; va_list va; va_start(va, fmt); n = N - pos; - pos += std::min(vsnprintf(&dest[pos], n, fmt, va), n - 1); + res = vsnprintf(&dest[pos], n, fmt, va); + pos += (res <= n - 1) ? res : n - 1; va_end(va); }