From 951682a15c0fb813def00165f806bbe76963cd7e Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 11 Nov 2015 14:32:41 +0000 Subject: [PATCH] Add more types --- clang-plugin/printf_check.cc | 82 ++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/clang-plugin/printf_check.cc b/clang-plugin/printf_check.cc index 37e8dc399..7f20bdaac 100644 --- a/clang-plugin/printf_check.cc +++ b/clang-plugin/printf_check.cc @@ -22,6 +22,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include "printf_check.h" #include "clang/AST/AST.h" #include "clang/AST/Expr.h" @@ -31,6 +32,9 @@ #include #include #include +#include +#include +#include using namespace clang; @@ -51,6 +55,12 @@ namespace rspamd { struct PrintfArgChecker *ctx); static bool long_double_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx); + static bool pointer_arg_handler (const Expr *arg, + struct PrintfArgChecker *ctx); + static bool pid_arg_handler (const Expr *arg, + struct PrintfArgChecker *ctx); + static bool int64_arg_handler (const Expr *arg, + struct PrintfArgChecker *ctx); using arg_parser_t = bool (*) (const Expr *, struct PrintfArgChecker *); @@ -118,6 +128,15 @@ namespace rspamd { case 'c': return llvm::make_unique (char_arg_handler, this->pcontext); + case 'p': + return llvm::make_unique (pointer_arg_handler, + this->pcontext); + case 'P': + return llvm::make_unique (pid_arg_handler, + this->pcontext); + case 'L': + return llvm::make_unique (int64_arg_handler, + this->pcontext); default: llvm::errs () << "unknown parser flag: " << type << "\n"; break; @@ -455,6 +474,9 @@ namespace rspamd { BuiltinType::Kind::Int}, "%z"); } + else { + assert (0); + } return true; } @@ -476,4 +498,64 @@ namespace rspamd { {BuiltinType::Kind::LongDouble}, "%F or %G"); } + static bool + pid_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx) + { + if (sizeof (pid_t) == sizeof (long)) { + return check_builtin_type (arg, + ctx, + {BuiltinType::Kind::ULong, + BuiltinType::Kind::Long}, + "%P"); + } + else if (sizeof (pid_t) == sizeof (int)) { + return check_builtin_type (arg, + ctx, + {BuiltinType::Kind::UInt, + BuiltinType::Kind::Int}, + "%P"); + } + else { + assert (0); + } + } + + static bool + pointer_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx) + { + auto type = arg->getType ().split ().Ty; + + if (!type->isPointerType ()) { + print_error ( + std::string ("bad pointer argument for %p: ") + + arg->getType ().getAsString (), arg, ctx->past); + return false; + } + + return true; + } + + static bool + int64_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx) + { + if (sizeof (int64_t) == sizeof (long long)) { + return check_builtin_type (arg, + ctx, + {BuiltinType::Kind::ULongLong, + BuiltinType::Kind::LongLong}, + "%L"); + } + else if (sizeof (int64_t) == sizeof (long)) { + return check_builtin_type (arg, + ctx, + {BuiltinType::Kind::ULong, + BuiltinType::Kind::Long}, + "%z"); + } + else { + assert (0); + } + + return true; + } }; -- 2.39.5