2016-02-04 10:37:21 +01:00
|
|
|
/*-
|
|
|
|
* Copyright 2016 Vsevolod Stakhov
|
2015-11-11 00:55:24 +01:00
|
|
|
*
|
2016-02-04 10:37:21 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-11-11 00:55:24 +01:00
|
|
|
*
|
2016-02-04 10:37:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-11-11 00:55:24 +01:00
|
|
|
*/
|
2015-11-11 15:32:41 +01:00
|
|
|
#include <sys/types.h>
|
2015-11-11 00:55:24 +01:00
|
|
|
#include "printf_check.h"
|
|
|
|
#include "clang/AST/AST.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
|
|
|
#include <unordered_map>
|
2015-11-11 18:10:16 +01:00
|
|
|
#include <unordered_set>
|
2015-11-11 14:07:32 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <sstream>
|
|
|
|
#include <ctype.h>
|
2015-11-11 15:32:41 +01:00
|
|
|
#include <signal.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <cstdint>
|
2015-11-11 00:55:24 +01:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace rspamd {
|
2015-11-11 14:07:32 +01:00
|
|
|
struct PrintfArgChecker;
|
|
|
|
|
2015-11-11 14:53:36 +01:00
|
|
|
static bool cstring_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
2015-11-11 16:25:24 +01:00
|
|
|
|
2015-11-11 14:53:36 +01:00
|
|
|
static bool int_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
2015-11-11 16:25:24 +01:00
|
|
|
|
2015-11-11 15:05:48 +01:00
|
|
|
static bool long_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
2015-11-11 16:25:24 +01:00
|
|
|
|
2015-11-11 15:05:48 +01:00
|
|
|
static bool size_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
2015-11-11 16:25:24 +01:00
|
|
|
|
2015-11-11 15:05:48 +01:00
|
|
|
static bool char_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
2015-11-11 16:25:24 +01:00
|
|
|
|
2015-11-11 15:05:48 +01:00
|
|
|
static bool double_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
2015-11-11 16:25:24 +01:00
|
|
|
|
2015-11-11 15:05:48 +01:00
|
|
|
static bool long_double_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
2015-11-11 16:25:24 +01:00
|
|
|
|
2015-11-11 15:32:41 +01:00
|
|
|
static bool pointer_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
2015-11-11 16:25:24 +01:00
|
|
|
|
2015-11-11 15:32:41 +01:00
|
|
|
static bool pid_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
2015-11-11 16:25:24 +01:00
|
|
|
|
2017-04-08 10:56:09 +02:00
|
|
|
static bool time_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
|
|
|
|
2015-11-11 15:32:41 +01:00
|
|
|
static bool int64_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
2015-11-11 14:53:36 +01:00
|
|
|
|
2015-11-11 18:22:47 +01:00
|
|
|
static bool int32_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
|
|
|
|
2015-11-11 18:26:06 +01:00
|
|
|
static bool gboolean_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
|
|
|
|
2015-11-11 16:25:24 +01:00
|
|
|
static bool tok_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
|
|
|
|
|
|
|
static bool fstring_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
|
|
|
|
|
|
|
static bool gstring_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
|
|
|
|
|
|
|
static bool gerr_arg_handler (const Expr *arg,
|
|
|
|
struct PrintfArgChecker *ctx);
|
|
|
|
|
2015-11-11 14:07:32 +01:00
|
|
|
using arg_parser_t = bool (*) (const Expr *, struct PrintfArgChecker *);
|
|
|
|
|
|
|
|
static void
|
2015-11-11 17:18:22 +01:00
|
|
|
print_error (const std::string &err, const Expr *e, const ASTContext *ast,
|
|
|
|
CompilerInstance *ci)
|
2015-11-11 14:07:32 +01:00
|
|
|
{
|
|
|
|
auto loc = e->getExprLoc ();
|
2015-11-11 17:18:22 +01:00
|
|
|
auto &diag = ci->getDiagnostics ();
|
|
|
|
auto id = diag.getCustomDiagID (DiagnosticsEngine::Error,
|
|
|
|
"format query error: %0");
|
|
|
|
diag.Report (loc, id) << err;
|
2015-11-11 14:07:32 +01:00
|
|
|
}
|
|
|
|
|
2015-11-11 17:49:43 +01:00
|
|
|
static void
|
|
|
|
print_warning (const std::string &err, const Expr *e, const ASTContext *ast,
|
|
|
|
CompilerInstance *ci)
|
|
|
|
{
|
|
|
|
auto loc = e->getExprLoc ();
|
|
|
|
auto &diag = ci->getDiagnostics ();
|
|
|
|
auto id = diag.getCustomDiagID (DiagnosticsEngine::Warning,
|
|
|
|
"format query warning: %0");
|
|
|
|
diag.Report (loc, id) << err;
|
|
|
|
}
|
|
|
|
|
2015-11-11 14:07:32 +01:00
|
|
|
struct PrintfArgChecker {
|
|
|
|
private:
|
|
|
|
arg_parser_t parser;
|
|
|
|
public:
|
|
|
|
int width;
|
|
|
|
int precision;
|
2015-11-11 15:05:48 +01:00
|
|
|
bool is_unsigned;
|
2015-11-11 14:53:36 +01:00
|
|
|
ASTContext *past;
|
2015-11-11 17:18:22 +01:00
|
|
|
CompilerInstance *pci;
|
2015-11-11 14:07:32 +01:00
|
|
|
|
2015-11-11 17:18:22 +01:00
|
|
|
PrintfArgChecker (arg_parser_t _p, ASTContext *_ast, CompilerInstance *_ci) :
|
|
|
|
parser (_p), past (_ast), pci(_ci)
|
2015-11-11 15:05:48 +01:00
|
|
|
{
|
|
|
|
width = 0;
|
|
|
|
precision = 0;
|
|
|
|
is_unsigned = false;
|
|
|
|
}
|
2015-11-11 14:53:36 +01:00
|
|
|
|
2015-11-11 16:25:24 +01:00
|
|
|
virtual ~PrintfArgChecker ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator() (const Expr *e)
|
2015-11-11 14:07:32 +01:00
|
|
|
{
|
|
|
|
return parser (e, this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-11 00:55:24 +01:00
|
|
|
class PrintfCheckVisitor::impl {
|
2015-11-11 15:32:25 +01:00
|
|
|
std::unordered_map<std::string, unsigned int> printf_functions;
|
2015-11-11 18:10:16 +01:00
|
|
|
std::unordered_set<char> format_specs;
|
2015-11-11 00:55:24 +01:00
|
|
|
ASTContext *pcontext;
|
2015-11-11 17:18:22 +01:00
|
|
|
CompilerInstance *ci;
|
2015-11-11 00:55:24 +01:00
|
|
|
|
2015-11-11 17:49:43 +01:00
|
|
|
std::unique_ptr <PrintfArgChecker> parseFlags (const std::string &flags,
|
|
|
|
const Expr *e)
|
2015-11-11 14:07:32 +01:00
|
|
|
{
|
2015-11-11 16:25:24 +01:00
|
|
|
auto type = flags.back ();
|
2015-11-11 14:07:32 +01:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 's':
|
2015-11-11 16:25:24 +01:00
|
|
|
return llvm::make_unique<PrintfArgChecker> (cstring_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2015-11-11 14:07:32 +01:00
|
|
|
case 'd':
|
2015-11-11 16:25:24 +01:00
|
|
|
return llvm::make_unique<PrintfArgChecker> (int_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2015-11-11 15:05:48 +01:00
|
|
|
case 'z':
|
|
|
|
return llvm::make_unique<PrintfArgChecker> (size_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2015-11-11 15:05:48 +01:00
|
|
|
case 'l':
|
|
|
|
return llvm::make_unique<PrintfArgChecker> (long_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2015-11-11 15:05:48 +01:00
|
|
|
case 'f':
|
|
|
|
case 'g':
|
|
|
|
return llvm::make_unique<PrintfArgChecker> (double_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2015-11-11 15:05:48 +01:00
|
|
|
case 'F':
|
|
|
|
case 'G':
|
2015-11-11 16:25:24 +01:00
|
|
|
return llvm::make_unique<PrintfArgChecker> (
|
|
|
|
long_double_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2015-11-11 15:05:48 +01:00
|
|
|
case 'c':
|
|
|
|
return llvm::make_unique<PrintfArgChecker> (char_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2015-11-11 15:32:41 +01:00
|
|
|
case 'p':
|
|
|
|
return llvm::make_unique<PrintfArgChecker> (pointer_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2015-11-11 15:32:41 +01:00
|
|
|
case 'P':
|
|
|
|
return llvm::make_unique<PrintfArgChecker> (pid_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2017-04-08 10:56:09 +02:00
|
|
|
case 't':
|
|
|
|
return llvm::make_unique<PrintfArgChecker> (time_arg_handler,
|
|
|
|
this->pcontext, this->ci);
|
2015-11-11 15:32:41 +01:00
|
|
|
case 'L':
|
|
|
|
return llvm::make_unique<PrintfArgChecker> (int64_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2015-11-11 18:22:47 +01:00
|
|
|
case 'D':
|
|
|
|
return llvm::make_unique<PrintfArgChecker> (int32_arg_handler,
|
|
|
|
this->pcontext, this->ci);
|
2015-12-22 17:26:29 +01:00
|
|
|
case 'B':
|
2015-11-11 18:26:06 +01:00
|
|
|
return llvm::make_unique<PrintfArgChecker> (gboolean_arg_handler,
|
|
|
|
this->pcontext, this->ci);
|
2015-11-11 16:25:24 +01:00
|
|
|
case 'T':
|
|
|
|
return llvm::make_unique<PrintfArgChecker> (tok_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2015-11-11 16:25:24 +01:00
|
|
|
case 'V':
|
|
|
|
return llvm::make_unique<PrintfArgChecker> (fstring_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2015-11-11 16:25:24 +01:00
|
|
|
case 'v':
|
|
|
|
return llvm::make_unique<PrintfArgChecker> (gstring_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2015-11-11 16:25:24 +01:00
|
|
|
case 'e':
|
|
|
|
return llvm::make_unique<PrintfArgChecker> (gerr_arg_handler,
|
2015-11-11 17:18:22 +01:00
|
|
|
this->pcontext, this->ci);
|
2015-11-11 14:07:32 +01:00
|
|
|
default:
|
2015-11-11 17:49:43 +01:00
|
|
|
print_warning (std::string("unknown parser flag: ") + type,
|
|
|
|
e, this->pcontext, this->ci);
|
2015-11-11 14:07:32 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-11-11 16:25:24 +01:00
|
|
|
std::shared_ptr <std::vector<PrintfArgChecker>>
|
2015-11-11 17:49:43 +01:00
|
|
|
genParsers (const StringRef query, const Expr *e)
|
2015-11-11 14:07:32 +01:00
|
|
|
{
|
|
|
|
enum {
|
|
|
|
ignore_chars = 0,
|
|
|
|
read_percent,
|
|
|
|
read_width,
|
|
|
|
read_precision,
|
|
|
|
read_arg
|
|
|
|
} state = ignore_chars;
|
|
|
|
int width, precision;
|
|
|
|
std::string flags;
|
|
|
|
|
2015-11-11 17:18:22 +01:00
|
|
|
auto res = std::make_shared<std::vector<PrintfArgChecker> > ();
|
2015-11-11 14:07:32 +01:00
|
|
|
|
2015-11-11 18:58:58 +01:00
|
|
|
for (auto citer = query.begin(); citer != query.end(); ++citer) {
|
|
|
|
auto c = *citer;
|
|
|
|
|
2015-11-11 14:07:32 +01:00
|
|
|
switch (state) {
|
|
|
|
case ignore_chars:
|
|
|
|
if (c == '%') {
|
|
|
|
state = read_percent;
|
|
|
|
flags.clear ();
|
|
|
|
width = precision = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case read_percent:
|
|
|
|
if (isdigit (c)) {
|
|
|
|
state = read_width;
|
|
|
|
width = c - '0';
|
|
|
|
}
|
|
|
|
else if (c == '.') {
|
|
|
|
state = read_precision;
|
|
|
|
precision = c - '0';
|
|
|
|
}
|
|
|
|
else if (c == '*') {
|
|
|
|
/* %*s - need integer argument */
|
2015-11-11 17:18:22 +01:00
|
|
|
res->emplace_back (int_arg_handler, this->pcontext,
|
|
|
|
this->ci);
|
2015-11-11 18:58:58 +01:00
|
|
|
|
|
|
|
if (*std::next (citer) == '.') {
|
|
|
|
++citer;
|
|
|
|
state = read_precision;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state = read_arg;
|
|
|
|
}
|
2015-11-11 14:07:32 +01:00
|
|
|
}
|
|
|
|
else if (c == '%') {
|
|
|
|
/* Percent character, ignore */
|
|
|
|
state = ignore_chars;
|
|
|
|
}
|
|
|
|
else {
|
2015-11-11 18:58:58 +01:00
|
|
|
// Rewind iter
|
|
|
|
--citer;
|
2015-11-11 14:07:32 +01:00
|
|
|
state = read_arg;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case read_width:
|
|
|
|
if (isdigit (c)) {
|
|
|
|
width *= 10;
|
|
|
|
width += c - '0';
|
|
|
|
}
|
|
|
|
else if (c == '.') {
|
|
|
|
state = read_precision;
|
|
|
|
precision = c - '0';
|
|
|
|
}
|
|
|
|
else {
|
2015-11-11 18:58:58 +01:00
|
|
|
// Rewind iter
|
|
|
|
--citer;
|
2015-11-11 14:07:32 +01:00
|
|
|
state = read_arg;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case read_precision:
|
|
|
|
if (isdigit (c)) {
|
|
|
|
precision *= 10;
|
|
|
|
precision += c - '0';
|
|
|
|
}
|
|
|
|
else if (c == '*') {
|
2015-11-11 17:18:22 +01:00
|
|
|
res->emplace_back (int_arg_handler, this->pcontext,
|
|
|
|
this->ci);
|
2015-11-11 14:07:32 +01:00
|
|
|
state = read_arg;
|
|
|
|
}
|
|
|
|
else {
|
2015-11-11 18:58:58 +01:00
|
|
|
// Rewind iter
|
|
|
|
--citer;
|
2015-11-11 14:07:32 +01:00
|
|
|
state = read_arg;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case read_arg:
|
2015-11-11 18:10:16 +01:00
|
|
|
auto found = format_specs.find (c);
|
|
|
|
if (found != format_specs.end () || !isalpha (c)) {
|
|
|
|
|
|
|
|
if (isalpha (c)) {
|
|
|
|
flags.push_back (c);
|
|
|
|
}
|
2015-11-11 18:22:47 +01:00
|
|
|
|
2015-11-11 17:49:43 +01:00
|
|
|
auto handler = parseFlags (flags, e);
|
2015-11-11 14:07:32 +01:00
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
auto handler_copy = *handler;
|
2015-11-11 14:53:36 +01:00
|
|
|
handler_copy.precision = precision;
|
|
|
|
handler_copy.width = width;
|
2015-11-11 14:07:32 +01:00
|
|
|
res->emplace_back (std::move (handler_copy));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-11-11 15:32:25 +01:00
|
|
|
|
|
|
|
if (c == '%') {
|
|
|
|
state = read_percent;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state = ignore_chars;
|
|
|
|
}
|
2015-11-11 18:58:58 +01:00
|
|
|
flags.clear ();
|
|
|
|
width = precision = 0;
|
2015-11-11 14:07:32 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
flags.push_back (c);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == read_arg) {
|
2015-11-11 17:49:43 +01:00
|
|
|
auto handler = parseFlags (flags, e);
|
2015-11-11 14:07:32 +01:00
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
auto handler_copy = *handler;
|
2015-11-11 14:53:36 +01:00
|
|
|
handler_copy.precision = precision;
|
|
|
|
handler_copy.width = width;
|
2015-11-11 14:07:32 +01:00
|
|
|
res->emplace_back (std::move (handler_copy));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2015-11-11 16:25:24 +01:00
|
|
|
|
2015-11-11 00:55:24 +01:00
|
|
|
public:
|
2015-11-11 17:18:22 +01:00
|
|
|
impl (ASTContext *_ctx, clang::CompilerInstance &_ci)
|
|
|
|
: pcontext (_ctx), ci(&_ci)
|
2015-11-11 00:55:24 +01:00
|
|
|
{
|
|
|
|
/* name -> format string position */
|
|
|
|
printf_functions = {
|
|
|
|
{"rspamd_printf", 0},
|
|
|
|
{"rspamd_default_log_function", 4},
|
|
|
|
{"rspamd_snprintf", 2},
|
2015-11-16 10:06:22 +01:00
|
|
|
{"rspamd_fprintf", 1},
|
|
|
|
{"rspamd_printf_gstring", 1},
|
|
|
|
{"rspamd_printf_fstring", 1}
|
2015-11-11 00:55:24 +01:00
|
|
|
};
|
2015-11-11 18:10:16 +01:00
|
|
|
|
|
|
|
format_specs = {
|
|
|
|
's', 'd', 'l', 'L', 'v', 'V', 'f', 'F', 'g', 'G',
|
2015-12-22 17:26:29 +01:00
|
|
|
'T', 'z', 'D', 'c', 'p', 'P', 'e', 'B'
|
2015-11-11 18:10:16 +01:00
|
|
|
};
|
2015-11-11 00:55:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
bool VisitCallExpr (CallExpr *E)
|
|
|
|
{
|
2018-01-09 12:38:07 +01:00
|
|
|
if (E->getCalleeDecl () == nullptr) {
|
|
|
|
llvm::errs () << "Bad callee\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-11 00:55:24 +01:00
|
|
|
auto callee = dyn_cast<NamedDecl> (E->getCalleeDecl ());
|
|
|
|
if (callee == NULL) {
|
|
|
|
llvm::errs () << "Bad callee\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto fname = callee->getNameAsString ();
|
|
|
|
|
|
|
|
auto pos_it = printf_functions.find (fname);
|
|
|
|
|
|
|
|
if (pos_it != printf_functions.end ()) {
|
|
|
|
const auto args = E->getArgs ();
|
|
|
|
auto pos = pos_it->second;
|
|
|
|
auto query = args[pos];
|
|
|
|
|
|
|
|
if (!query->isEvaluatable (*pcontext)) {
|
2015-11-11 17:49:43 +01:00
|
|
|
print_warning (std::string ("cannot evaluate query"),
|
|
|
|
E, this->pcontext, this->ci);
|
2015-11-11 00:55:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
clang::Expr::EvalResult r;
|
|
|
|
|
|
|
|
if (!query->EvaluateAsRValue (r, *pcontext)) {
|
2015-11-11 17:49:43 +01:00
|
|
|
print_warning (std::string ("cannot evaluate rvalue of query"),
|
|
|
|
E, this->pcontext, this->ci);
|
2015-11-11 00:55:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto qval = dyn_cast<StringLiteral> (
|
|
|
|
r.Val.getLValueBase ().get<const Expr *> ());
|
2015-11-11 14:53:36 +01:00
|
|
|
if (!qval) {
|
2015-11-11 17:49:43 +01:00
|
|
|
print_warning (std::string ("bad or absent query string"),
|
|
|
|
E, this->pcontext, this->ci);
|
2015-11-11 14:07:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-11-11 00:55:24 +01:00
|
|
|
|
2015-11-11 17:49:43 +01:00
|
|
|
auto parsers = genParsers (qval->getString (), E);
|
2015-11-11 14:07:32 +01:00
|
|
|
|
|
|
|
if (parsers) {
|
|
|
|
if (parsers->size () != E->getNumArgs () - (pos + 1)) {
|
|
|
|
std::ostringstream err_buf;
|
|
|
|
err_buf << "number of arguments for " << fname
|
2017-05-13 17:20:00 +02:00
|
|
|
<< " mismatches query string '" <<
|
2015-11-11 16:25:24 +01:00
|
|
|
qval->getString ().str ()
|
|
|
|
<< "', expected " << parsers->size () <<
|
|
|
|
" args"
|
|
|
|
<< ", got " <<
|
|
|
|
(E->getNumArgs () - (pos + 1))
|
|
|
|
<< " args";
|
2015-11-11 17:18:22 +01:00
|
|
|
print_error (err_buf.str (), E, this->pcontext, this->ci);
|
2015-11-11 14:07:32 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (auto i = pos + 1; i < E->getNumArgs (); i++) {
|
|
|
|
auto arg = args[i];
|
2015-11-11 00:55:24 +01:00
|
|
|
|
2015-11-11 14:07:32 +01:00
|
|
|
if (arg) {
|
2015-11-11 16:25:24 +01:00
|
|
|
if (!parsers->at (i - (pos + 1)) (arg)) {
|
2015-11-11 14:07:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-11 00:55:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-11 17:18:22 +01:00
|
|
|
PrintfCheckVisitor::PrintfCheckVisitor (ASTContext *ctx,
|
|
|
|
clang::CompilerInstance &ci) :
|
|
|
|
pimpl{new impl (ctx, ci)}
|
2015-11-11 00:55:24 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintfCheckVisitor::~PrintfCheckVisitor ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PrintfCheckVisitor::VisitCallExpr (clang::CallExpr *E)
|
|
|
|
{
|
|
|
|
return pimpl->VisitCallExpr (E);
|
|
|
|
}
|
2015-11-11 14:53:36 +01:00
|
|
|
|
|
|
|
/* Type handlers */
|
|
|
|
static bool
|
|
|
|
cstring_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
|
|
|
auto type = arg->getType ().split ().Ty;
|
|
|
|
|
|
|
|
if (!type->isPointerType ()) {
|
|
|
|
print_error (
|
|
|
|
std::string ("bad string argument for %s: ") +
|
2015-11-11 17:18:22 +01:00
|
|
|
arg->getType ().getAsString (),
|
|
|
|
arg, ctx->past, ctx->pci);
|
2015-11-11 14:53:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-11 16:25:24 +01:00
|
|
|
auto ptr_type = type->getPointeeType ().split ().Ty;
|
2015-11-11 14:53:36 +01:00
|
|
|
|
|
|
|
if (!ptr_type->isCharType ()) {
|
|
|
|
/* We might have gchar * here */
|
|
|
|
auto desugared_type = ptr_type->getUnqualifiedDesugaredType ();
|
2015-11-11 16:49:28 +01:00
|
|
|
auto desugared_ptr_type = type->getUnqualifiedDesugaredType ();
|
2015-11-11 14:53:36 +01:00
|
|
|
|
2015-11-11 16:49:28 +01:00
|
|
|
if (!desugared_type || (!desugared_type->isCharType () &&
|
|
|
|
!desugared_ptr_type->isVoidPointerType ())) {
|
2015-11-11 14:53:36 +01:00
|
|
|
if (desugared_type) {
|
|
|
|
desugared_type->dump ();
|
|
|
|
}
|
|
|
|
print_error (
|
|
|
|
std::string ("bad string argument for %s: ") +
|
2015-11-11 17:18:22 +01:00
|
|
|
arg->getType ().getAsString (),
|
|
|
|
arg, ctx->past, ctx->pci);
|
2015-11-11 14:53:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2015-11-11 15:21:27 +01:00
|
|
|
check_builtin_type (const Expr *arg, struct PrintfArgChecker *ctx,
|
2015-11-11 16:25:24 +01:00
|
|
|
const std::vector <BuiltinType::Kind> &k, const std::string &fmt)
|
2015-11-11 15:05:48 +01:00
|
|
|
{
|
|
|
|
auto type = arg->getType ().split ().Ty;
|
|
|
|
|
|
|
|
auto desugared_type = type->getUnqualifiedDesugaredType ();
|
|
|
|
|
2015-11-11 15:21:27 +01:00
|
|
|
if (!desugared_type->isBuiltinType ()) {
|
2015-11-11 15:05:48 +01:00
|
|
|
print_error (
|
2015-11-11 15:21:27 +01:00
|
|
|
std::string ("not a builtin type for ") + fmt + " arg: " +
|
2015-11-11 17:18:22 +01:00
|
|
|
arg->getType ().getAsString (),
|
|
|
|
arg, ctx->past, ctx->pci);
|
2015-11-11 15:05:48 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto builtin_type = dyn_cast<BuiltinType> (desugared_type);
|
|
|
|
auto kind = builtin_type->getKind ();
|
2015-11-11 15:21:27 +01:00
|
|
|
auto found = false;
|
|
|
|
|
|
|
|
for (auto kk : k) {
|
|
|
|
if (kind == kk) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-11-11 15:05:48 +01:00
|
|
|
|
2015-11-11 15:21:27 +01:00
|
|
|
if (!found) {
|
2015-11-11 15:05:48 +01:00
|
|
|
print_error (
|
2015-11-11 15:21:27 +01:00
|
|
|
std::string ("bad argument for ") + fmt + " arg: " +
|
2015-11-11 18:22:34 +01:00
|
|
|
arg->getType ().getAsString () + ", resolved as: " +
|
|
|
|
builtin_type->getNameAsCString (ctx->past->getPrintingPolicy ()),
|
2015-11-11 17:18:22 +01:00
|
|
|
arg, ctx->past, ctx->pci);
|
2015-11-11 15:05:48 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2015-11-11 15:21:27 +01:00
|
|
|
int_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
2015-11-11 15:05:48 +01:00
|
|
|
{
|
2015-11-11 16:25:24 +01:00
|
|
|
return check_builtin_type (arg,
|
|
|
|
ctx,
|
|
|
|
{BuiltinType::Kind::UInt,
|
|
|
|
BuiltinType::Kind::Int},
|
|
|
|
"%d or *");
|
2015-11-11 15:21:27 +01:00
|
|
|
}
|
2015-11-11 15:05:48 +01:00
|
|
|
|
2015-11-11 15:21:27 +01:00
|
|
|
static bool
|
|
|
|
long_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
|
|
|
return check_builtin_type (arg,
|
|
|
|
ctx,
|
|
|
|
{BuiltinType::Kind::ULong,
|
|
|
|
BuiltinType::Kind::Long},
|
|
|
|
"%l");
|
|
|
|
}
|
2015-11-11 15:05:48 +01:00
|
|
|
|
2015-11-11 15:21:27 +01:00
|
|
|
static bool
|
|
|
|
char_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
|
|
|
return check_builtin_type (arg,
|
|
|
|
ctx,
|
|
|
|
{BuiltinType::Kind::UChar,
|
2015-11-11 17:43:40 +01:00
|
|
|
BuiltinType::Kind::SChar,
|
|
|
|
BuiltinType::Kind::Int}, // Because of char -> int propagation
|
2015-11-11 15:21:27 +01:00
|
|
|
"%c");
|
2015-11-11 15:05:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
size_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
|
|
|
if (sizeof (size_t) == sizeof (long)) {
|
2015-11-11 15:21:27 +01:00
|
|
|
return check_builtin_type (arg,
|
|
|
|
ctx,
|
|
|
|
{BuiltinType::Kind::ULong,
|
|
|
|
BuiltinType::Kind::Long},
|
|
|
|
"%z");
|
2015-11-11 15:05:48 +01:00
|
|
|
}
|
|
|
|
else if (sizeof (size_t) == sizeof (int)) {
|
2015-11-11 15:21:27 +01:00
|
|
|
return check_builtin_type (arg,
|
|
|
|
ctx,
|
|
|
|
{BuiltinType::Kind::UInt,
|
|
|
|
BuiltinType::Kind::Int},
|
|
|
|
"%z");
|
2015-11-11 15:05:48 +01:00
|
|
|
}
|
2015-11-11 15:32:41 +01:00
|
|
|
else {
|
|
|
|
assert (0);
|
|
|
|
}
|
2015-11-11 15:05:48 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
double_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
2015-11-11 15:21:27 +01:00
|
|
|
return check_builtin_type (arg,
|
|
|
|
ctx,
|
|
|
|
{BuiltinType::Kind::Double},
|
|
|
|
"%f or %g");
|
2015-11-11 15:05:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
long_double_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
2015-11-11 15:21:27 +01:00
|
|
|
return check_builtin_type (arg,
|
|
|
|
ctx,
|
|
|
|
{BuiltinType::Kind::LongDouble},
|
|
|
|
"%F or %G");
|
2015-11-11 15:05:48 +01:00
|
|
|
}
|
2015-11-11 16:25:24 +01:00
|
|
|
|
2015-11-11 15:32:41 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-08 10:56:09 +02:00
|
|
|
static bool
|
|
|
|
time_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
|
|
|
if (sizeof (time_t) == sizeof (long)) {
|
|
|
|
return check_builtin_type (arg,
|
|
|
|
ctx,
|
|
|
|
{BuiltinType::Kind::ULong,
|
|
|
|
BuiltinType::Kind::Long},
|
|
|
|
"%t");
|
|
|
|
}
|
|
|
|
else if (sizeof (time_t) == sizeof (int)) {
|
|
|
|
return check_builtin_type (arg,
|
|
|
|
ctx,
|
|
|
|
{BuiltinType::Kind::UInt,
|
|
|
|
BuiltinType::Kind::Int},
|
|
|
|
"%t");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-11 15:32:41 +01:00
|
|
|
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: ") +
|
2015-11-11 17:18:22 +01:00
|
|
|
arg->getType ().getAsString (),
|
|
|
|
arg, ctx->past, ctx->pci);
|
2015-11-11 15:32:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
int64_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
2015-11-11 18:22:47 +01:00
|
|
|
std::vector <BuiltinType::Kind> check;
|
|
|
|
|
2015-11-11 15:32:41 +01:00
|
|
|
if (sizeof (int64_t) == sizeof (long long)) {
|
2015-11-11 18:22:47 +01:00
|
|
|
check.push_back (BuiltinType::Kind::ULongLong);
|
|
|
|
check.push_back (BuiltinType::Kind::LongLong);
|
2015-11-11 15:32:41 +01:00
|
|
|
}
|
2015-11-11 18:22:47 +01:00
|
|
|
if (sizeof (int64_t) == sizeof (long)) {
|
|
|
|
check.push_back (BuiltinType::Kind::ULong);
|
|
|
|
check.push_back (BuiltinType::Kind::Long);
|
2015-11-11 15:32:41 +01:00
|
|
|
}
|
2015-11-11 18:22:47 +01:00
|
|
|
|
|
|
|
return check_builtin_type (arg,
|
|
|
|
ctx,
|
|
|
|
check,
|
|
|
|
"%L");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
int32_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
|
|
|
std::vector < BuiltinType::Kind> check;
|
|
|
|
|
|
|
|
if (sizeof (int32_t) == sizeof (long)) {
|
|
|
|
check.push_back (BuiltinType::Kind::ULong);
|
|
|
|
check.push_back (BuiltinType::Kind::Long);
|
|
|
|
}
|
|
|
|
if (sizeof (int32_t) == sizeof (int)) {
|
|
|
|
check.push_back (BuiltinType::Kind::UInt);
|
|
|
|
check.push_back (BuiltinType::Kind::Int);
|
2015-11-11 15:32:41 +01:00
|
|
|
}
|
|
|
|
|
2015-11-11 18:22:47 +01:00
|
|
|
return check_builtin_type (arg,
|
|
|
|
ctx,
|
|
|
|
check,
|
|
|
|
"%D");
|
|
|
|
|
2015-11-11 15:32:41 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-11-11 16:25:24 +01:00
|
|
|
|
2015-11-11 18:26:06 +01:00
|
|
|
static bool
|
|
|
|
gboolean_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
|
|
|
return check_builtin_type (arg,
|
|
|
|
ctx,
|
|
|
|
{BuiltinType::Kind::Int}, // gboolean is int in fact
|
|
|
|
"%b");
|
|
|
|
}
|
|
|
|
|
2015-11-11 16:25:24 +01:00
|
|
|
static bool
|
|
|
|
check_struct_type (const Expr *arg, struct PrintfArgChecker *ctx,
|
|
|
|
const std::string &sname, const std::string &fmt)
|
|
|
|
{
|
|
|
|
auto type = arg->getType ().split ().Ty;
|
|
|
|
|
|
|
|
if (!type->isPointerType ()) {
|
|
|
|
print_error (
|
|
|
|
std::string ("bad string argument for %s: ") +
|
2015-11-11 17:18:22 +01:00
|
|
|
arg->getType ().getAsString (),
|
|
|
|
arg, ctx->past, ctx->pci);
|
2015-11-11 16:25:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr_type = type->getPointeeType ().split ().Ty;
|
|
|
|
auto desugared_type = ptr_type->getUnqualifiedDesugaredType ();
|
|
|
|
|
|
|
|
if (!desugared_type->isRecordType ()) {
|
|
|
|
print_error (
|
|
|
|
std::string ("not a record type for ") + fmt + " arg: " +
|
2015-11-11 17:18:22 +01:00
|
|
|
arg->getType ().getAsString (),
|
|
|
|
arg, ctx->past, ctx->pci);
|
2015-11-11 16:25:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto struct_type = desugared_type->getAsStructureType ();
|
|
|
|
auto struct_decl = struct_type->getDecl ();
|
|
|
|
auto struct_def = struct_decl->getNameAsString ();
|
|
|
|
|
|
|
|
if (struct_def != sname) {
|
|
|
|
print_error (std::string ("bad argument '") + struct_def + "' for "
|
|
|
|
+ fmt + " arg: " +
|
2015-11-11 17:18:22 +01:00
|
|
|
arg->getType ().getAsString (),
|
|
|
|
arg, ctx->past, ctx->pci);
|
2015-11-11 16:25:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
tok_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
|
|
|
return check_struct_type (arg,
|
|
|
|
ctx,
|
|
|
|
"f_str_tok",
|
|
|
|
"%T");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
fstring_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
|
|
|
return check_struct_type (arg,
|
|
|
|
ctx,
|
|
|
|
"f_str_s",
|
|
|
|
"%V");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
gstring_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
|
|
|
return check_struct_type (arg,
|
|
|
|
ctx,
|
|
|
|
"_GString",
|
|
|
|
"%v");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
gerr_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
|
|
|
|
{
|
|
|
|
return check_struct_type (arg,
|
|
|
|
ctx,
|
|
|
|
"_GError",
|
|
|
|
"%e");
|
|
|
|
}
|
2018-01-09 12:38:07 +01:00
|
|
|
}
|