diff options
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Sema/Sema.h | 1 | ||||
-rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 19 | ||||
-rw-r--r-- | clang/lib/Sema/SemaDeclAttr.cpp | 3 |
3 files changed, 20 insertions, 3 deletions
diff --git a/clang/lib/Sema/Sema.h b/clang/lib/Sema/Sema.h index 0a4086fe3e4..63ecdcb108d 100644 --- a/clang/lib/Sema/Sema.h +++ b/clang/lib/Sema/Sema.h @@ -3308,6 +3308,7 @@ private: Action::OwningExprResult CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall); SourceLocation getLocationOfStringLiteralByte(const StringLiteral *SL, unsigned ByteNo) const; + bool CheckablePrintfAttr(const FormatAttr *Format, CallExpr *TheCall); bool CheckObjCString(Expr *Arg); bool SemaBuiltinVAStart(CallExpr *TheCall); bool SemaBuiltinUnorderedCompare(CallExpr *TheCall); diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index d6095fc2ae5..949c33dfff8 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -84,6 +84,21 @@ SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, } } +/// CheckablePrintfAttr - does a function call have a "printf" attribute +/// and arguments that merit checking? +bool Sema::CheckablePrintfAttr(const FormatAttr *Format, CallExpr *TheCall) { + if (Format->getType() == "printf") return true; + if (Format->getType() == "printf0") { + // printf0 allows null "format" string; if so don't check format/args + unsigned format_idx = Format->getFormatIdx() - 1; + if (format_idx < TheCall->getNumArgs()) { + Expr *Format = TheCall->getArg(format_idx)->IgnoreParenCasts(); + if (!Format->isNullPointerConstant(Context)) + return true; + } + } + return false; +} /// CheckFunctionCall - Check a direct function call for various correctness /// and safety properties not strictly enforced by the C type system. @@ -167,7 +182,7 @@ Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) { // Printf checking. if (const FormatAttr *Format = FDecl->getAttr<FormatAttr>()) { - if (Format->getType() == "printf") { + if (CheckablePrintfAttr(Format, TheCall)) { bool HasVAListArg = Format->getFirstArg() == 0; if (!HasVAListArg) { if (const FunctionProtoType *Proto @@ -201,7 +216,7 @@ Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) { QualType Ty = V->getType(); if (!Ty->isBlockPointerType()) return move(TheCallResult); - if (Format->getType() == "printf") { + if (CheckablePrintfAttr(Format, TheCall)) { bool HasVAListArg = Format->getFirstArg() == 0; if (!HasVAListArg) { const FunctionType *FT = diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 239f9951c51..35230005904 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -1212,7 +1212,8 @@ static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { default: break; case 5: Supported = !memcmp(Format, "scanf", 5); break; case 6: Supported = !memcmp(Format, "printf", 6); break; - case 7: Supported = !memcmp(Format, "strfmon", 7); break; + case 7: Supported = !memcmp(Format, "printf0", 7) || + !memcmp(Format, "strfmon", 7); break; case 8: Supported = (is_strftime = !memcmp(Format, "strftime", 8)) || (is_NSString = !memcmp(Format, "NSString", 8)) || |