Skip to content

Commit 6761afe

Browse files
committed
Safer xstring copy functions
1 parent d5977b1 commit 6761afe

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/data/str.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ int xstrncpyarg(char *pDest, size_t nSize, const char *pFmt, va_list args)
211211
{
212212
if (pDest == NULL || !nSize) return XSTDERR;
213213
size_t nLength = 0;
214+
xstrnul(pDest);
214215

215216
int nBytes = vsnprintf(pDest, nSize, pFmt, args);
216217
if (nBytes > 0) nLength = XSTD_MIN((size_t)nBytes, nSize - 1);
@@ -331,14 +332,8 @@ size_t xstrxcpyf(char **pDst, const char *pFmt, ...)
331332

332333
size_t xstrncpy(char *pDst, size_t nSize, const char* pSrc)
333334
{
334-
if (pDst == NULL || !nSize) return 0;
335-
336-
if (!xstrused(pSrc))
337-
{
338-
pDst[0] = XSTR_NUL;
339-
return 0;
340-
}
341-
335+
xstrnul(pDst);
336+
if (pDst == NULL || !nSize || !xstrused(pSrc)) return 0;
342337
size_t nCopySize = strnlen(pSrc, nSize - 1);
343338
if (nCopySize) memcpy(pDst, pSrc, nCopySize);
344339
pDst[nCopySize] = XSTR_NUL;
@@ -347,6 +342,7 @@ size_t xstrncpy(char *pDst, size_t nSize, const char* pSrc)
347342

348343
size_t xstrncpys(char *pDst, size_t nDstSize, const char *pSrc, size_t nSrcLen)
349344
{
345+
xstrnul(pDst);
350346
if (pDst == NULL || pSrc == NULL || !nDstSize) return 0;
351347
size_t nCopySize = XSTD_MIN(nSrcLen, nDstSize - 1);
352348
if (nCopySize) memcpy(pDst, pSrc, nCopySize);
@@ -386,6 +382,7 @@ size_t xstrnlcpyf(char *pDst, size_t nSize, size_t nFLen, char cFChar, const cha
386382
if (pDst == NULL || !nSize) return 0;
387383
if (nFLen > nSize) nFLen = nSize - 1;
388384

385+
xstrnul(pDst);
389386
size_t nBytes = 0;
390387
va_list args;
391388

@@ -573,7 +570,9 @@ size_t xstrnclr(char *pDst, size_t nSize, const char* pClr, const char* pStr, ..
573570
if (pDst == NULL || !nSize) return 0;
574571
char sBuffer[XSTR_STACK];
575572
char *pBuffer = &sBuffer[0];
573+
576574
uint8_t nAlloc = 0;
575+
xstrnul(pDst);
577576

578577
if (nSize > XSTR_STACK)
579578
{
@@ -1377,7 +1376,7 @@ int XString_Color(xstring_t *pString, const char* pClr, size_t nPosit, size_t nS
13771376
if (pTempStr == NULL) return XSTDERR;
13781377

13791378
if ((XString_Add(pTempStr, pString->pData, nFirstPart) == XSTDERR) ||
1380-
(XString_Add(pTempStr, pClr, strlen(pClr)) == XSTDERR) ||
1379+
(XString_Add(pTempStr, pClr, strnlen(pClr, XSTR_MICRO)) == XSTDERR) ||
13811380
(XString_Add(pTempStr, &pString->pData[nFirstPart], nSize) == XSTDERR) ||
13821381
(XString_Add(pTempStr, XSTR_FMT_RESET, strlen(XSTR_FMT_RESET)) == XSTDERR) ||
13831382
(XString_Add(pTempStr, &pString->pData[nLastPart], nLastSize) == XSTDERR))

src/xver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#define XUTILS_VERSION_MAX 2
1414
#define XUTILS_VERSION_MIN 6
15-
#define XUTILS_BUILD_NUMBER 34
15+
#define XUTILS_BUILD_NUMBER 35
1616

1717
#ifdef __cplusplus
1818
extern "C" {

0 commit comments

Comments
 (0)