[cfe-dev] sprintf -> snprintf conversion
Vladimir Kirillov
proger at uaoug.org.ua
Mon Oct 25 01:00:09 PDT 2010
Hello, cfe-dev!
I'm using LLVM/Clang on OpenBSD. This project proactively advocates
usage of 'secure' C apis, especially related to memory bounds
checking. Thus using functions like sprintf/strcpy/etc usually spits
out a linker warning in base toolchain:
/home/proger/dev/llvm/Debug+Asserts/lib/libclangFrontend.a(DocumentXML.o)(.text+0xc65): In function `clang::DocumentXML::escapeString(char const*, unsigned long)':
/home/proger/dev/llvm/tools/clang/lib/Frontend/DocumentXML.cpp:107: warning: sprintf() is often misused, please use snprintf()
I've done some conversions from sprintf to snprintf, please commit those.
Thanks!
-------------- next part --------------
Index: include/llvm/ADT/StringExtras.h
===================================================================
--- include/llvm/ADT/StringExtras.h (revision 117247)
+++ include/llvm/ADT/StringExtras.h (working copy)
@@ -102,7 +102,7 @@
static inline std::string ftostr(double V) {
char Buffer[200];
- sprintf(Buffer, "%20.6e", V);
+ snprintf(Buffer, sizeof(Buffer), "%20.6e", V);
char *B = Buffer;
while (*B == ' ') ++B;
return B;
Index: lib/System/Errno.cpp
===================================================================
--- lib/System/Errno.cpp (revision 117247)
+++ lib/System/Errno.cpp (working copy)
@@ -63,7 +63,7 @@
#else
// Strange that this system doesn't even have strerror
// but, oh well, just use a generic message
- sprintf(buffer, "Error #%d", errnum);
+ snprintf(buffer, sizeof(buffer), "Error #%d", errnum);
#endif
return str;
}
Index: tools/clang/lib/Lex/PPMacroExpansion.cpp
===================================================================
--- tools/clang/lib/Lex/PPMacroExpansion.cpp (revision 117247)
+++ tools/clang/lib/Lex/PPMacroExpansion.cpp (working copy)
@@ -482,8 +482,8 @@
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
};
- char TmpBuffer[100];
- sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
+ char TmpBuffer[32];
+ snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
TM->tm_year+1900);
Token TmpTok;
@@ -491,7 +491,7 @@
PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
DATELoc = TmpTok.getLocation();
- sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
+ snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
TIMELoc = TmpTok.getLocation();
}
Index: lib/Archive/ArchiveWriter.cpp
===================================================================
--- lib/Archive/ArchiveWriter.cpp (revision 117247)
+++ lib/Archive/ArchiveWriter.cpp (working copy)
@@ -82,16 +82,16 @@
// Set the permissions mode, uid and gid
hdr.init();
char buffer[32];
- sprintf(buffer, "%-8o", mbr.getMode());
+ snprintf(buffer, sizeof(buffer), "%-8o", mbr.getMode());
memcpy(hdr.mode,buffer,8);
- sprintf(buffer, "%-6u", mbr.getUser());
+ snprintf(buffer, sizeof(buffer), "%-6u", mbr.getUser());
memcpy(hdr.uid,buffer,6);
- sprintf(buffer, "%-6u", mbr.getGroup());
+ snprintf(buffer, sizeof(buffer), "%-6u", mbr.getGroup());
memcpy(hdr.gid,buffer,6);
// Set the last modification date
uint64_t secondsSinceEpoch = mbr.getModTime().toEpochTime();
- sprintf(buffer,"%-12u", unsigned(secondsSinceEpoch));
+ snprintf(buffer, sizeof(buffer), "%-12u", unsigned(secondsSinceEpoch));
memcpy(hdr.date,buffer,12);
// Get rid of trailing blanks in the name
@@ -141,9 +141,9 @@
// Set the size field
if (sz < 0) {
buffer[0] = '-';
- sprintf(&buffer[1],"%-9u",(unsigned)-sz);
+ snprintf(&buffer[1], sizeof(buffer) - 1, "%-9u", (unsigned)-sz);
} else {
- sprintf(buffer, "%-10u", (unsigned)sz);
+ snprintf(buffer, sizeof(buffer), "%-10u", (unsigned)sz);
}
memcpy(hdr.size,buffer,10);
@@ -290,16 +290,16 @@
memcpy(Hdr.name,ARFILE_LLVM_SYMTAB_NAME,16);
uint64_t secondsSinceEpoch = sys::TimeValue::now().toEpochTime();
char buffer[32];
- sprintf(buffer, "%-8o", 0644);
- memcpy(Hdr.mode,buffer,8);
- sprintf(buffer, "%-6u", sys::Process::GetCurrentUserId());
- memcpy(Hdr.uid,buffer,6);
- sprintf(buffer, "%-6u", sys::Process::GetCurrentGroupId());
- memcpy(Hdr.gid,buffer,6);
- sprintf(buffer,"%-12u", unsigned(secondsSinceEpoch));
- memcpy(Hdr.date,buffer,12);
- sprintf(buffer,"%-10u",symTabSize);
- memcpy(Hdr.size,buffer,10);
+ snprintf(buffer, sizeof(buffer), "%-8o", 0644);
+ memcpy(Hdr.mode,buffer, 8);
+ snprintf(buffer, sizeof(buffer), "%-6u", sys::Process::GetCurrentUserId());
+ memcpy(Hdr.uid,buffer, 6);
+ snprintf(buffer, sizeof(buffer), "%-6u", sys::Process::GetCurrentGroupId());
+ memcpy(Hdr.gid,buffer, 6);
+ snprintf(buffer, sizeof(buffer), "%-12u", unsigned(secondsSinceEpoch));
+ memcpy(Hdr.date,buffer, 12);
+ snprintf(buffer, sizeof(buffer), "%-10u",symTabSize);
+ memcpy(Hdr.size,buffer, 10);
// Write the header
ARFile.write((char*)&Hdr, sizeof(Hdr));
Index: lib/Target/CBackend/CBackend.cpp
===================================================================
--- lib/Target/CBackend/CBackend.cpp (revision 117247)
+++ lib/Target/CBackend/CBackend.cpp (working copy)
@@ -746,7 +746,7 @@
APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &ignored);
#if HAVE_PRINTF_A && ENABLE_CBE_PRINTF_A
char Buffer[100];
- sprintf(Buffer, "%a", APF.convertToDouble());
+ snprintf(Buffer, sizeof(Buffer), "%a", APF.convertToDouble());
if (!strncmp(Buffer, "0x", 2) ||
!strncmp(Buffer, "-0x", 3) ||
!strncmp(Buffer, "+0x", 3))
@@ -1072,7 +1072,7 @@
char Buffer[100];
uint64_t ll = DoubleToBits(V);
- sprintf(Buffer, "0x%llx", static_cast<long long>(ll));
+ snprintf(Buffer, sizeof(Buffer), "0x%llx", static_cast<long long>(ll));
std::string Num(&Buffer[0], &Buffer[6]);
unsigned long Val = strtoul(Num.c_str(), 0, 16);
@@ -1094,7 +1094,7 @@
#if HAVE_PRINTF_A && ENABLE_CBE_PRINTF_A
// Print out the constant as a floating point number.
char Buffer[100];
- sprintf(Buffer, "%a", V);
+ snprintf(Buffer, sizeof(Buffer), "%a", V);
Num = Buffer;
#else
Num = ftostr(FPC->getValueAPF());
@@ -1343,7 +1343,7 @@
if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') || ch == '_')) {
char buffer[5];
- sprintf(buffer, "_%x_", ch);
+ snprintf(buffer, sizeof(buffer), "_%x_", ch);
VarName += buffer;
} else
VarName += ch;
Index: lib/Target/CppBackend/CPPBackend.cpp
===================================================================
--- lib/Target/CppBackend/CPPBackend.cpp (revision 117247)
+++ lib/Target/CppBackend/CPPBackend.cpp (working copy)
@@ -224,7 +224,7 @@
Out << "APFloat(";
#if HAVE_PRINTF_A
char Buffer[100];
- sprintf(Buffer, "%A", APF.convertToDouble());
+ snprintf(Buffer, sizeof(Buffer), "%A", APF.convertToDouble());
if ((!strncmp(Buffer, "0x", 2) ||
!strncmp(Buffer, "-0x", 3) ||
!strncmp(Buffer, "+0x", 3)) &&
More information about the cfe-dev
mailing list