Source: Replace uses of sprintf with safer snprintf in CMake 3.24 branch

Backport commit d5694e4623 (Source: Replace uses of sprintf with safer
snprintf, 2022-06-17, v3.25.0-rc1~587^2) to the CMake 3.24 branch. This
is needed to compile without warnings using Xcode 14.1's macOS 13.0 SDK.
This commit is contained in:
Sean McBride 2022-06-17 19:57:11 -04:00 committed by Brad King
parent eea23d21a3
commit 9684a589ca
8 changed files with 30 additions and 23 deletions

View File

@ -85,7 +85,7 @@ void cmCursesLongMessageForm::UpdateStatusBar()
for (size_t i = 0; i < sideSpace; i++) { for (size_t i = 0; i < sideSpace; i++) {
version[i] = ' '; version[i] = ' ';
} }
sprintf(version + sideSpace, "%s", vertmp); snprintf(version + sideSpace, sizeof(version) - sideSpace, "%s", vertmp);
version[width] = '\0'; version[width] = '\0';
char fmt_s[] = "%s"; char fmt_s[] = "%s";

View File

@ -117,7 +117,7 @@ static bool Check_Integer_Field(FIELD * field, const void * argp)
{ {
if (val<low || val>high) return FALSE; if (val<low || val>high) return FALSE;
} }
sprintf(buf,"%.*ld",(prec>0?prec:0),val); snprintf(buf,sizeof(buf),"%.*ld",(prec>0?prec:0),val);
set_field_buffer(field,0,buf); set_field_buffer(field,0,buf);
return TRUE; return TRUE;
} }

View File

@ -140,7 +140,7 @@ static bool Check_Numeric_Field(FIELD * field, const void * argp)
{ {
if (val<low || val>high) return FALSE; if (val<low || val>high) return FALSE;
} }
sprintf(buf,"%.*f",(prec>0?prec:0),val); snprintf(buf,sizeof(buf),"%.*f",(prec>0?prec:0),val);
set_field_buffer(field,0,buf); set_field_buffer(field,0,buf);
return TRUE; return TRUE;
} }

View File

@ -822,13 +822,13 @@ void cmFindPackageCommand::SetVersionVariables(
char buf[64]; char buf[64];
snprintf(buf, sizeof(buf), "%u", major); snprintf(buf, sizeof(buf), "%u", major);
addDefinition(prefix + "_MAJOR", buf); addDefinition(prefix + "_MAJOR", buf);
sprintf(buf, "%u", minor); snprintf(buf, sizeof(buf), "%u", minor);
addDefinition(prefix + "_MINOR", buf); addDefinition(prefix + "_MINOR", buf);
sprintf(buf, "%u", patch); snprintf(buf, sizeof(buf), "%u", patch);
addDefinition(prefix + "_PATCH", buf); addDefinition(prefix + "_PATCH", buf);
sprintf(buf, "%u", tweak); snprintf(buf, sizeof(buf), "%u", tweak);
addDefinition(prefix + "_TWEAK", buf); addDefinition(prefix + "_TWEAK", buf);
sprintf(buf, "%u", count); snprintf(buf, sizeof(buf), "%u", count);
addDefinition(prefix + "_COUNT", buf); addDefinition(prefix + "_COUNT", buf);
} }

View File

@ -143,7 +143,8 @@ bool HandleHexCommand(std::vector<std::string> const& args,
std::string::size_type hexIndex = 0; std::string::size_type hexIndex = 0;
for (auto const& c : instr) { for (auto const& c : instr) {
sprintf(&output[hexIndex], "%.2x", static_cast<unsigned char>(c) & 0xFF); snprintf(&output[hexIndex], 3, "%.2x",
static_cast<unsigned char>(c) & 0xFF);
hexIndex += 2; hexIndex += 2;
} }

View File

@ -1256,7 +1256,7 @@ std::string cmSystemTools::ComputeCertificateThumbprint(
certContext, CERT_HASH_PROP_ID, hashData, &hashLength)) { certContext, CERT_HASH_PROP_ID, hashData, &hashLength)) {
for (DWORD i = 0; i < hashLength; i++) { for (DWORD i = 0; i < hashLength; i++) {
// Convert each byte to hexadecimal // Convert each byte to hexadecimal
sprintf(pHashPrint, "%02X", hashData[i]); snprintf(pHashPrint, 3, "%02X", hashData[i]);
pHashPrint += 2; pHashPrint += 2;
} }
*pHashPrint = '\0'; *pHashPrint = '\0';

View File

@ -75,10 +75,10 @@ static int CCONV InitialPass(void* inf, void* mf, int argc, char* argv[])
info->CAPI->DisplaySatus(mf, info->CAPI->GetStartOutputDirectory(mf)); info->CAPI->DisplaySatus(mf, info->CAPI->GetStartOutputDirectory(mf));
info->CAPI->DisplaySatus(mf, info->CAPI->GetCurrentDirectory(mf)); info->CAPI->DisplaySatus(mf, info->CAPI->GetCurrentDirectory(mf));
info->CAPI->DisplaySatus(mf, info->CAPI->GetCurrentOutputDirectory(mf)); info->CAPI->DisplaySatus(mf, info->CAPI->GetCurrentOutputDirectory(mf));
sprintf(buffer, "Cache version: %d.%d, CMake version: %d.%d", snprintf(
info->CAPI->GetCacheMajorVersion(mf), buffer, sizeof(buffer), "Cache version: %d.%d, CMake version: %d.%d",
info->CAPI->GetCacheMinorVersion(mf), info->CAPI->GetCacheMajorVersion(mf), info->CAPI->GetCacheMinorVersion(mf),
info->CAPI->GetMajorVersion(mf), info->CAPI->GetMinorVersion(mf)); info->CAPI->GetMajorVersion(mf), info->CAPI->GetMinorVersion(mf));
info->CAPI->DisplaySatus(mf, buffer); info->CAPI->DisplaySatus(mf, buffer);
if (info->CAPI->CommandExists(mf, "SET")) { if (info->CAPI->CommandExists(mf, "SET")) {
info->CAPI->DisplaySatus(mf, "Command SET exists"); info->CAPI->DisplaySatus(mf, "Command SET exists");
@ -91,10 +91,12 @@ static int CCONV InitialPass(void* inf, void* mf, int argc, char* argv[])
source_file = info->CAPI->CreateNewSourceFile(mf); source_file = info->CAPI->CreateNewSourceFile(mf);
cstr = info->CAPI->SourceFileGetSourceName(source_file); cstr = info->CAPI->SourceFileGetSourceName(source_file);
sprintf(buffer, "Should be empty (source file name): [%s]", cstr); snprintf(buffer, sizeof(buffer), "Should be empty (source file name): [%s]",
cstr);
info->CAPI->DisplaySatus(mf, buffer); info->CAPI->DisplaySatus(mf, buffer);
cstr = info->CAPI->SourceFileGetFullPath(source_file); cstr = info->CAPI->SourceFileGetFullPath(source_file);
sprintf(buffer, "Should be empty (source file full path): [%s]", cstr); snprintf(buffer, sizeof(buffer),
"Should be empty (source file full path): [%s]", cstr);
info->CAPI->DisplaySatus(mf, buffer); info->CAPI->DisplaySatus(mf, buffer);
info->CAPI->DefineSourceFileProperty(mf, "SOME_PROPERTY", "unused old prop", info->CAPI->DefineSourceFileProperty(mf, "SOME_PROPERTY", "unused old prop",
"This property is no longer used", 0); "This property is no longer used", 0);
@ -106,7 +108,8 @@ static int CCONV InitialPass(void* inf, void* mf, int argc, char* argv[])
"This property is for testing.", 0); "This property is for testing.", 0);
info->CAPI->SourceFileSetProperty(source_file, "SOME_PROPERTY2", "HERE"); info->CAPI->SourceFileSetProperty(source_file, "SOME_PROPERTY2", "HERE");
cstr = info->CAPI->SourceFileGetProperty(source_file, "ABSTRACT"); cstr = info->CAPI->SourceFileGetProperty(source_file, "ABSTRACT");
sprintf(buffer, "Should be 0 (source file abstract property): [%p]", cstr); snprintf(buffer, sizeof(buffer),
"Should be 0 (source file abstract property): [%p]", cstr);
info->CAPI->DisplaySatus(mf, buffer); info->CAPI->DisplaySatus(mf, buffer);
info->CAPI->DestroySourceFile(source_file); info->CAPI->DestroySourceFile(source_file);

View File

@ -75,10 +75,10 @@ static int CCONV InitialPass(void* inf, void* mf, int argc, char* argv[])
info->CAPI->DisplaySatus(mf, info->CAPI->GetStartOutputDirectory(mf)); info->CAPI->DisplaySatus(mf, info->CAPI->GetStartOutputDirectory(mf));
info->CAPI->DisplaySatus(mf, info->CAPI->GetCurrentDirectory(mf)); info->CAPI->DisplaySatus(mf, info->CAPI->GetCurrentDirectory(mf));
info->CAPI->DisplaySatus(mf, info->CAPI->GetCurrentOutputDirectory(mf)); info->CAPI->DisplaySatus(mf, info->CAPI->GetCurrentOutputDirectory(mf));
sprintf(buffer, "Cache version: %d.%d, CMake version: %d.%d", snprintf(
info->CAPI->GetCacheMajorVersion(mf), buffer, sizeof(buffer), "Cache version: %d.%d, CMake version: %d.%d",
info->CAPI->GetCacheMinorVersion(mf), info->CAPI->GetCacheMajorVersion(mf), info->CAPI->GetCacheMinorVersion(mf),
info->CAPI->GetMajorVersion(mf), info->CAPI->GetMinorVersion(mf)); info->CAPI->GetMajorVersion(mf), info->CAPI->GetMinorVersion(mf));
info->CAPI->DisplaySatus(mf, buffer); info->CAPI->DisplaySatus(mf, buffer);
if (info->CAPI->CommandExists(mf, "SET")) { if (info->CAPI->CommandExists(mf, "SET")) {
info->CAPI->DisplaySatus(mf, "Command SET exists"); info->CAPI->DisplaySatus(mf, "Command SET exists");
@ -91,10 +91,12 @@ static int CCONV InitialPass(void* inf, void* mf, int argc, char* argv[])
source_file = info->CAPI->CreateNewSourceFile(mf); source_file = info->CAPI->CreateNewSourceFile(mf);
cstr = info->CAPI->SourceFileGetSourceName(source_file); cstr = info->CAPI->SourceFileGetSourceName(source_file);
sprintf(buffer, "Should be empty (source file name): [%s]", cstr); snprintf(buffer, sizeof(buffer), "Should be empty (source file name): [%s]",
cstr);
info->CAPI->DisplaySatus(mf, buffer); info->CAPI->DisplaySatus(mf, buffer);
cstr = info->CAPI->SourceFileGetFullPath(source_file); cstr = info->CAPI->SourceFileGetFullPath(source_file);
sprintf(buffer, "Should be empty (source file full path): [%s]", cstr); snprintf(buffer, sizeof(buffer),
"Should be empty (source file full path): [%s]", cstr);
info->CAPI->DisplaySatus(mf, buffer); info->CAPI->DisplaySatus(mf, buffer);
info->CAPI->DefineSourceFileProperty(mf, "SOME_PROPERTY", "unused old prop", info->CAPI->DefineSourceFileProperty(mf, "SOME_PROPERTY", "unused old prop",
"This property is no longer used", 0); "This property is no longer used", 0);
@ -106,7 +108,8 @@ static int CCONV InitialPass(void* inf, void* mf, int argc, char* argv[])
"This property is for testing.", 0); "This property is for testing.", 0);
info->CAPI->SourceFileSetProperty(source_file, "SOME_PROPERTY2", "HERE"); info->CAPI->SourceFileSetProperty(source_file, "SOME_PROPERTY2", "HERE");
cstr = info->CAPI->SourceFileGetProperty(source_file, "ABSTRACT"); cstr = info->CAPI->SourceFileGetProperty(source_file, "ABSTRACT");
sprintf(buffer, "Should be 0 (source file abstract property): [%p]", cstr); snprintf(buffer, sizeof(buffer),
"Should be 0 (source file abstract property): [%p]", cstr);
info->CAPI->DisplaySatus(mf, buffer); info->CAPI->DisplaySatus(mf, buffer);
info->CAPI->DestroySourceFile(source_file); info->CAPI->DestroySourceFile(source_file);