Skip to content

Use sizeof for buffer lengths in snprintf #3037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Basic/Demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ static void unreachable(const char *Message) {

DemanglerPrinter &DemanglerPrinter::operator<<(unsigned long long n) & {
char buffer[32];
snprintf(buffer, 32, "%llu", n);
snprintf(buffer, sizeof(buffer), "%llu", n);
Stream.append(buffer);
return *this;
}
DemanglerPrinter &DemanglerPrinter::operator<<(long long n) & {
char buffer[32];
snprintf(buffer, 32, "%lld",n);
snprintf(buffer, sizeof(buffer), "%lld",n);
Stream.append(buffer);
return *this;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/PlaygroundTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ class Instrumenter {
std::pair<PatternBindingDecl*, VarDecl*>
buildPatternAndVariable(Expr *InitExpr) {
char NameBuf[11] = { 0 };
snprintf(NameBuf, 11, "tmp%u", TmpNameIndex);
snprintf(NameBuf, sizeof(NameBuf), "tmp%u", TmpNameIndex);
TmpNameIndex++;

Expr *MaybeLoadInitExpr = nullptr;
Expand Down
3 changes: 1 addition & 2 deletions stdlib/public/runtime/Reflection.mm
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,7 @@ void swift_TupleMirror_subscript(String *outString,

// The name is the stringized element number '.0'.
char buf[32];
snprintf(buf, 31, ".%zd", i);
buf[31] = 0;
snprintf(buf, sizeof(buf), ".%zd", i);
new (outString) String(buf, strlen(buf));

// Get a Mirror for the nth element.
Expand Down