From a0f8b40d5a369979d02a4706f4dce0e89c77910b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Sun, 29 Sep 2024 16:08:20 +0200 Subject: [PATCH] [CABMAN] Fix GCC13 buffer format overflow warning (#7408) CORE-19724 sdk/tools/cabman/dfp.cxx:1136:36: warning: 'sprintf' may write a terminating nul past the end of the destination [-Wformat-overflow=] 1136 | sprintf(InfLine, "%s=%s", GetFileName(SrcName).c_str(), DstName); | ^ sdk/tools/cabman/dfp.cxx:1136:20: note: 'sprintf' output 2 or more bytes (assuming 4097) into a destination of size 4096 --- sdk/tools/cabman/cabinet.h | 5 +++++ sdk/tools/cabman/dfp.cxx | 9 +++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/sdk/tools/cabman/cabinet.h b/sdk/tools/cabman/cabinet.h index 1697588bbfc..b3516ff2c8c 100644 --- a/sdk/tools/cabman/cabinet.h +++ b/sdk/tools/cabman/cabinet.h @@ -36,10 +36,15 @@ #define C_ASSERT(expr) extern char (*c_assert(void)) [(expr) ? 1 : -1] #endif +#ifndef _countof +#define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0])) +#endif + #if defined(_WIN32) #define DIR_SEPARATOR_CHAR '\\' #define DIR_SEPARATOR_STRING "\\" +#define snprintf _snprintf #define strcasecmp _stricmp #define strdup _strdup #else diff --git a/sdk/tools/cabman/dfp.cxx b/sdk/tools/cabman/dfp.cxx index 575b48f0816..498c81ff7b8 100644 --- a/sdk/tools/cabman/dfp.cxx +++ b/sdk/tools/cabman/dfp.cxx @@ -1031,7 +1031,7 @@ ULONG CDFParser::PerformFileCopy() char ch; char SrcName[PATH_MAX]; char DstName[PATH_MAX]; - char InfLine[PATH_MAX]; + char InfLine[PATH_MAX*2+1]; // To hold: GetFileName(SrcName) "=" DstName char Options[128]; char BaseFilename[PATH_MAX]; @@ -1076,7 +1076,7 @@ ULONG CDFParser::PerformFileCopy() } // options (it may be empty) - SkipSpaces (); + SkipSpaces(); if (CurrentToken != TokenEnd) { @@ -1133,12 +1133,13 @@ ULONG CDFParser::PerformFileCopy() switch (Status) { case CAB_STATUS_SUCCESS: - sprintf(InfLine, "%s=%s", GetFileName(SrcName).c_str(), DstName); + snprintf(InfLine, _countof(InfLine) - 1, + "%s=%s", GetFileName(SrcName).c_str(), DstName); WriteInfLine(InfLine); break; case CAB_STATUS_CANNOT_OPEN: - if (strstr(Options,"optional")) + if (strstr(Options, "optional")) { Status = CAB_STATUS_SUCCESS; printf("Optional file skipped (does not exist): %s.\n", SrcName);