You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

printf_check.cc 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * Copyright (c) 2015, Vsevolod Stakhov
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include <sys/types.h>
  25. #include "printf_check.h"
  26. #include "clang/AST/AST.h"
  27. #include "clang/AST/Expr.h"
  28. #include "clang/AST/ASTConsumer.h"
  29. #include "clang/AST/RecursiveASTVisitor.h"
  30. #include <unordered_map>
  31. #include <unordered_set>
  32. #include <vector>
  33. #include <sstream>
  34. #include <ctype.h>
  35. #include <signal.h>
  36. #include <assert.h>
  37. #include <cstdint>
  38. using namespace clang;
  39. namespace rspamd {
  40. struct PrintfArgChecker;
  41. static bool cstring_arg_handler (const Expr *arg,
  42. struct PrintfArgChecker *ctx);
  43. static bool int_arg_handler (const Expr *arg,
  44. struct PrintfArgChecker *ctx);
  45. static bool long_arg_handler (const Expr *arg,
  46. struct PrintfArgChecker *ctx);
  47. static bool size_arg_handler (const Expr *arg,
  48. struct PrintfArgChecker *ctx);
  49. static bool char_arg_handler (const Expr *arg,
  50. struct PrintfArgChecker *ctx);
  51. static bool double_arg_handler (const Expr *arg,
  52. struct PrintfArgChecker *ctx);
  53. static bool long_double_arg_handler (const Expr *arg,
  54. struct PrintfArgChecker *ctx);
  55. static bool pointer_arg_handler (const Expr *arg,
  56. struct PrintfArgChecker *ctx);
  57. static bool pid_arg_handler (const Expr *arg,
  58. struct PrintfArgChecker *ctx);
  59. static bool int64_arg_handler (const Expr *arg,
  60. struct PrintfArgChecker *ctx);
  61. static bool int32_arg_handler (const Expr *arg,
  62. struct PrintfArgChecker *ctx);
  63. static bool gboolean_arg_handler (const Expr *arg,
  64. struct PrintfArgChecker *ctx);
  65. static bool tok_arg_handler (const Expr *arg,
  66. struct PrintfArgChecker *ctx);
  67. static bool fstring_arg_handler (const Expr *arg,
  68. struct PrintfArgChecker *ctx);
  69. static bool gstring_arg_handler (const Expr *arg,
  70. struct PrintfArgChecker *ctx);
  71. static bool gerr_arg_handler (const Expr *arg,
  72. struct PrintfArgChecker *ctx);
  73. using arg_parser_t = bool (*) (const Expr *, struct PrintfArgChecker *);
  74. static void
  75. print_error (const std::string &err, const Expr *e, const ASTContext *ast,
  76. CompilerInstance *ci)
  77. {
  78. auto loc = e->getExprLoc ();
  79. auto &diag = ci->getDiagnostics ();
  80. auto id = diag.getCustomDiagID (DiagnosticsEngine::Error,
  81. "format query error: %0");
  82. diag.Report (loc, id) << err;
  83. }
  84. static void
  85. print_warning (const std::string &err, const Expr *e, const ASTContext *ast,
  86. CompilerInstance *ci)
  87. {
  88. auto loc = e->getExprLoc ();
  89. auto &diag = ci->getDiagnostics ();
  90. auto id = diag.getCustomDiagID (DiagnosticsEngine::Warning,
  91. "format query warning: %0");
  92. diag.Report (loc, id) << err;
  93. }
  94. struct PrintfArgChecker {
  95. private:
  96. arg_parser_t parser;
  97. public:
  98. int width;
  99. int precision;
  100. bool is_unsigned;
  101. ASTContext *past;
  102. CompilerInstance *pci;
  103. PrintfArgChecker (arg_parser_t _p, ASTContext *_ast, CompilerInstance *_ci) :
  104. parser (_p), past (_ast), pci(_ci)
  105. {
  106. width = 0;
  107. precision = 0;
  108. is_unsigned = false;
  109. }
  110. virtual ~PrintfArgChecker ()
  111. {
  112. }
  113. bool operator() (const Expr *e)
  114. {
  115. return parser (e, this);
  116. }
  117. };
  118. class PrintfCheckVisitor::impl {
  119. std::unordered_map<std::string, unsigned int> printf_functions;
  120. std::unordered_set<char> format_specs;
  121. ASTContext *pcontext;
  122. CompilerInstance *ci;
  123. std::unique_ptr <PrintfArgChecker> parseFlags (const std::string &flags,
  124. const Expr *e)
  125. {
  126. auto type = flags.back ();
  127. switch (type) {
  128. case 's':
  129. return llvm::make_unique<PrintfArgChecker> (cstring_arg_handler,
  130. this->pcontext, this->ci);
  131. case 'd':
  132. return llvm::make_unique<PrintfArgChecker> (int_arg_handler,
  133. this->pcontext, this->ci);
  134. case 'z':
  135. return llvm::make_unique<PrintfArgChecker> (size_arg_handler,
  136. this->pcontext, this->ci);
  137. case 'l':
  138. return llvm::make_unique<PrintfArgChecker> (long_arg_handler,
  139. this->pcontext, this->ci);
  140. case 'f':
  141. case 'g':
  142. return llvm::make_unique<PrintfArgChecker> (double_arg_handler,
  143. this->pcontext, this->ci);
  144. case 'F':
  145. case 'G':
  146. return llvm::make_unique<PrintfArgChecker> (
  147. long_double_arg_handler,
  148. this->pcontext, this->ci);
  149. case 'c':
  150. return llvm::make_unique<PrintfArgChecker> (char_arg_handler,
  151. this->pcontext, this->ci);
  152. case 'p':
  153. return llvm::make_unique<PrintfArgChecker> (pointer_arg_handler,
  154. this->pcontext, this->ci);
  155. case 'P':
  156. return llvm::make_unique<PrintfArgChecker> (pid_arg_handler,
  157. this->pcontext, this->ci);
  158. case 'L':
  159. return llvm::make_unique<PrintfArgChecker> (int64_arg_handler,
  160. this->pcontext, this->ci);
  161. case 'D':
  162. return llvm::make_unique<PrintfArgChecker> (int32_arg_handler,
  163. this->pcontext, this->ci);
  164. case 'b':
  165. return llvm::make_unique<PrintfArgChecker> (gboolean_arg_handler,
  166. this->pcontext, this->ci);
  167. case 'T':
  168. return llvm::make_unique<PrintfArgChecker> (tok_arg_handler,
  169. this->pcontext, this->ci);
  170. case 'V':
  171. return llvm::make_unique<PrintfArgChecker> (fstring_arg_handler,
  172. this->pcontext, this->ci);
  173. case 'v':
  174. return llvm::make_unique<PrintfArgChecker> (gstring_arg_handler,
  175. this->pcontext, this->ci);
  176. case 'e':
  177. return llvm::make_unique<PrintfArgChecker> (gerr_arg_handler,
  178. this->pcontext, this->ci);
  179. default:
  180. print_warning (std::string("unknown parser flag: ") + type,
  181. e, this->pcontext, this->ci);
  182. break;
  183. }
  184. return nullptr;
  185. }
  186. std::shared_ptr <std::vector<PrintfArgChecker>>
  187. genParsers (const StringRef query, const Expr *e)
  188. {
  189. enum {
  190. ignore_chars = 0,
  191. read_percent,
  192. read_width,
  193. read_precision,
  194. read_arg
  195. } state = ignore_chars;
  196. int width, precision;
  197. std::string flags;
  198. auto res = std::make_shared<std::vector<PrintfArgChecker> > ();
  199. for (auto citer = query.begin(); citer != query.end(); ++citer) {
  200. auto c = *citer;
  201. switch (state) {
  202. case ignore_chars:
  203. if (c == '%') {
  204. state = read_percent;
  205. flags.clear ();
  206. width = precision = 0;
  207. }
  208. break;
  209. case read_percent:
  210. if (isdigit (c)) {
  211. state = read_width;
  212. width = c - '0';
  213. }
  214. else if (c == '.') {
  215. state = read_precision;
  216. precision = c - '0';
  217. }
  218. else if (c == '*') {
  219. /* %*s - need integer argument */
  220. res->emplace_back (int_arg_handler, this->pcontext,
  221. this->ci);
  222. if (*std::next (citer) == '.') {
  223. ++citer;
  224. state = read_precision;
  225. }
  226. else {
  227. state = read_arg;
  228. }
  229. }
  230. else if (c == '%') {
  231. /* Percent character, ignore */
  232. state = ignore_chars;
  233. }
  234. else {
  235. // Rewind iter
  236. --citer;
  237. state = read_arg;
  238. }
  239. break;
  240. case read_width:
  241. if (isdigit (c)) {
  242. width *= 10;
  243. width += c - '0';
  244. }
  245. else if (c == '.') {
  246. state = read_precision;
  247. precision = c - '0';
  248. }
  249. else {
  250. // Rewind iter
  251. --citer;
  252. state = read_arg;
  253. }
  254. break;
  255. case read_precision:
  256. if (isdigit (c)) {
  257. precision *= 10;
  258. precision += c - '0';
  259. }
  260. else if (c == '*') {
  261. res->emplace_back (int_arg_handler, this->pcontext,
  262. this->ci);
  263. state = read_arg;
  264. }
  265. else {
  266. // Rewind iter
  267. --citer;
  268. state = read_arg;
  269. }
  270. break;
  271. case read_arg:
  272. auto found = format_specs.find (c);
  273. if (found != format_specs.end () || !isalpha (c)) {
  274. if (isalpha (c)) {
  275. flags.push_back (c);
  276. }
  277. auto handler = parseFlags (flags, e);
  278. if (handler) {
  279. auto handler_copy = *handler;
  280. handler_copy.precision = precision;
  281. handler_copy.width = width;
  282. res->emplace_back (std::move (handler_copy));
  283. }
  284. else {
  285. return nullptr;
  286. }
  287. if (c == '%') {
  288. state = read_percent;
  289. }
  290. else {
  291. state = ignore_chars;
  292. }
  293. flags.clear ();
  294. width = precision = 0;
  295. }
  296. else {
  297. flags.push_back (c);
  298. }
  299. break;
  300. }
  301. }
  302. if (state == read_arg) {
  303. auto handler = parseFlags (flags, e);
  304. if (handler) {
  305. auto handler_copy = *handler;
  306. handler_copy.precision = precision;
  307. handler_copy.width = width;
  308. res->emplace_back (std::move (handler_copy));
  309. }
  310. else {
  311. return nullptr;
  312. }
  313. }
  314. return res;
  315. }
  316. public:
  317. impl (ASTContext *_ctx, clang::CompilerInstance &_ci)
  318. : pcontext (_ctx), ci(&_ci)
  319. {
  320. /* name -> format string position */
  321. printf_functions = {
  322. {"rspamd_printf", 0},
  323. {"rspamd_default_log_function", 4},
  324. {"rspamd_snprintf", 2},
  325. {"rspamd_fprintf", 1}
  326. };
  327. format_specs = {
  328. 's', 'd', 'l', 'L', 'v', 'V', 'f', 'F', 'g', 'G',
  329. 'T', 'z', 'D', 'c', 'p', 'P', 'e', 'b'
  330. };
  331. };
  332. bool VisitCallExpr (CallExpr *E)
  333. {
  334. auto callee = dyn_cast<NamedDecl> (E->getCalleeDecl ());
  335. if (callee == NULL) {
  336. llvm::errs () << "Bad callee\n";
  337. return false;
  338. }
  339. auto fname = callee->getNameAsString ();
  340. auto pos_it = printf_functions.find (fname);
  341. if (pos_it != printf_functions.end ()) {
  342. const auto args = E->getArgs ();
  343. auto pos = pos_it->second;
  344. auto query = args[pos];
  345. if (!query->isEvaluatable (*pcontext)) {
  346. print_warning (std::string ("cannot evaluate query"),
  347. E, this->pcontext, this->ci);
  348. return false;
  349. }
  350. clang::Expr::EvalResult r;
  351. if (!query->EvaluateAsRValue (r, *pcontext)) {
  352. print_warning (std::string ("cannot evaluate rvalue of query"),
  353. E, this->pcontext, this->ci);
  354. return false;
  355. }
  356. auto qval = dyn_cast<StringLiteral> (
  357. r.Val.getLValueBase ().get<const Expr *> ());
  358. if (!qval) {
  359. print_warning (std::string ("bad or absent query string"),
  360. E, this->pcontext, this->ci);
  361. return false;
  362. }
  363. auto parsers = genParsers (qval->getString (), E);
  364. if (parsers) {
  365. if (parsers->size () != E->getNumArgs () - (pos + 1)) {
  366. std::ostringstream err_buf;
  367. err_buf << "number of arguments for " << fname
  368. << " missmatches query string '" <<
  369. qval->getString ().str ()
  370. << "', expected " << parsers->size () <<
  371. " args"
  372. << ", got " <<
  373. (E->getNumArgs () - (pos + 1))
  374. << " args";
  375. print_error (err_buf.str (), E, this->pcontext, this->ci);
  376. return false;
  377. }
  378. else {
  379. for (auto i = pos + 1; i < E->getNumArgs (); i++) {
  380. auto arg = args[i];
  381. if (arg) {
  382. if (!parsers->at (i - (pos + 1)) (arg)) {
  383. return false;
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. return true;
  391. }
  392. };
  393. PrintfCheckVisitor::PrintfCheckVisitor (ASTContext *ctx,
  394. clang::CompilerInstance &ci) :
  395. pimpl{new impl (ctx, ci)}
  396. {
  397. }
  398. PrintfCheckVisitor::~PrintfCheckVisitor ()
  399. {
  400. }
  401. bool PrintfCheckVisitor::VisitCallExpr (clang::CallExpr *E)
  402. {
  403. return pimpl->VisitCallExpr (E);
  404. }
  405. /* Type handlers */
  406. static bool
  407. cstring_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  408. {
  409. auto type = arg->getType ().split ().Ty;
  410. if (!type->isPointerType ()) {
  411. print_error (
  412. std::string ("bad string argument for %s: ") +
  413. arg->getType ().getAsString (),
  414. arg, ctx->past, ctx->pci);
  415. return false;
  416. }
  417. auto ptr_type = type->getPointeeType ().split ().Ty;
  418. if (!ptr_type->isCharType ()) {
  419. /* We might have gchar * here */
  420. auto desugared_type = ptr_type->getUnqualifiedDesugaredType ();
  421. auto desugared_ptr_type = type->getUnqualifiedDesugaredType ();
  422. if (!desugared_type || (!desugared_type->isCharType () &&
  423. !desugared_ptr_type->isVoidPointerType ())) {
  424. if (desugared_type) {
  425. desugared_type->dump ();
  426. }
  427. print_error (
  428. std::string ("bad string argument for %s: ") +
  429. arg->getType ().getAsString (),
  430. arg, ctx->past, ctx->pci);
  431. return false;
  432. }
  433. }
  434. return true;
  435. }
  436. static bool
  437. check_builtin_type (const Expr *arg, struct PrintfArgChecker *ctx,
  438. const std::vector <BuiltinType::Kind> &k, const std::string &fmt)
  439. {
  440. auto type = arg->getType ().split ().Ty;
  441. auto desugared_type = type->getUnqualifiedDesugaredType ();
  442. if (!desugared_type->isBuiltinType ()) {
  443. print_error (
  444. std::string ("not a builtin type for ") + fmt + " arg: " +
  445. arg->getType ().getAsString (),
  446. arg, ctx->past, ctx->pci);
  447. return false;
  448. }
  449. auto builtin_type = dyn_cast<BuiltinType> (desugared_type);
  450. auto kind = builtin_type->getKind ();
  451. auto found = false;
  452. for (auto kk : k) {
  453. if (kind == kk) {
  454. found = true;
  455. break;
  456. }
  457. }
  458. if (!found) {
  459. print_error (
  460. std::string ("bad argument for ") + fmt + " arg: " +
  461. arg->getType ().getAsString () + ", resolved as: " +
  462. builtin_type->getNameAsCString (ctx->past->getPrintingPolicy ()),
  463. arg, ctx->past, ctx->pci);
  464. return false;
  465. }
  466. return true;
  467. }
  468. static bool
  469. int_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  470. {
  471. return check_builtin_type (arg,
  472. ctx,
  473. {BuiltinType::Kind::UInt,
  474. BuiltinType::Kind::Int},
  475. "%d or *");
  476. }
  477. static bool
  478. long_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  479. {
  480. return check_builtin_type (arg,
  481. ctx,
  482. {BuiltinType::Kind::ULong,
  483. BuiltinType::Kind::Long},
  484. "%l");
  485. }
  486. static bool
  487. char_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  488. {
  489. return check_builtin_type (arg,
  490. ctx,
  491. {BuiltinType::Kind::UChar,
  492. BuiltinType::Kind::SChar,
  493. BuiltinType::Kind::Int}, // Because of char -> int propagation
  494. "%c");
  495. }
  496. static bool
  497. size_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  498. {
  499. if (sizeof (size_t) == sizeof (long)) {
  500. return check_builtin_type (arg,
  501. ctx,
  502. {BuiltinType::Kind::ULong,
  503. BuiltinType::Kind::Long},
  504. "%z");
  505. }
  506. else if (sizeof (size_t) == sizeof (int)) {
  507. return check_builtin_type (arg,
  508. ctx,
  509. {BuiltinType::Kind::UInt,
  510. BuiltinType::Kind::Int},
  511. "%z");
  512. }
  513. else {
  514. assert (0);
  515. }
  516. return true;
  517. }
  518. static bool
  519. double_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  520. {
  521. return check_builtin_type (arg,
  522. ctx,
  523. {BuiltinType::Kind::Double},
  524. "%f or %g");
  525. }
  526. static bool
  527. long_double_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  528. {
  529. return check_builtin_type (arg,
  530. ctx,
  531. {BuiltinType::Kind::LongDouble},
  532. "%F or %G");
  533. }
  534. static bool
  535. pid_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  536. {
  537. if (sizeof (pid_t) == sizeof (long)) {
  538. return check_builtin_type (arg,
  539. ctx,
  540. {BuiltinType::Kind::ULong,
  541. BuiltinType::Kind::Long},
  542. "%P");
  543. }
  544. else if (sizeof (pid_t) == sizeof (int)) {
  545. return check_builtin_type (arg,
  546. ctx,
  547. {BuiltinType::Kind::UInt,
  548. BuiltinType::Kind::Int},
  549. "%P");
  550. }
  551. else {
  552. assert (0);
  553. }
  554. }
  555. static bool
  556. pointer_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  557. {
  558. auto type = arg->getType ().split ().Ty;
  559. if (!type->isPointerType ()) {
  560. print_error (
  561. std::string ("bad pointer argument for %p: ") +
  562. arg->getType ().getAsString (),
  563. arg, ctx->past, ctx->pci);
  564. return false;
  565. }
  566. return true;
  567. }
  568. static bool
  569. int64_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  570. {
  571. std::vector <BuiltinType::Kind> check;
  572. if (sizeof (int64_t) == sizeof (long long)) {
  573. check.push_back (BuiltinType::Kind::ULongLong);
  574. check.push_back (BuiltinType::Kind::LongLong);
  575. }
  576. if (sizeof (int64_t) == sizeof (long)) {
  577. check.push_back (BuiltinType::Kind::ULong);
  578. check.push_back (BuiltinType::Kind::Long);
  579. }
  580. return check_builtin_type (arg,
  581. ctx,
  582. check,
  583. "%L");
  584. return true;
  585. }
  586. static bool
  587. int32_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  588. {
  589. std::vector < BuiltinType::Kind> check;
  590. if (sizeof (int32_t) == sizeof (long)) {
  591. check.push_back (BuiltinType::Kind::ULong);
  592. check.push_back (BuiltinType::Kind::Long);
  593. }
  594. if (sizeof (int32_t) == sizeof (int)) {
  595. check.push_back (BuiltinType::Kind::UInt);
  596. check.push_back (BuiltinType::Kind::Int);
  597. }
  598. return check_builtin_type (arg,
  599. ctx,
  600. check,
  601. "%D");
  602. return true;
  603. }
  604. static bool
  605. gboolean_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  606. {
  607. return check_builtin_type (arg,
  608. ctx,
  609. {BuiltinType::Kind::Int}, // gboolean is int in fact
  610. "%b");
  611. }
  612. static bool
  613. check_struct_type (const Expr *arg, struct PrintfArgChecker *ctx,
  614. const std::string &sname, const std::string &fmt)
  615. {
  616. auto type = arg->getType ().split ().Ty;
  617. if (!type->isPointerType ()) {
  618. print_error (
  619. std::string ("bad string argument for %s: ") +
  620. arg->getType ().getAsString (),
  621. arg, ctx->past, ctx->pci);
  622. return false;
  623. }
  624. auto ptr_type = type->getPointeeType ().split ().Ty;
  625. auto desugared_type = ptr_type->getUnqualifiedDesugaredType ();
  626. if (!desugared_type->isRecordType ()) {
  627. print_error (
  628. std::string ("not a record type for ") + fmt + " arg: " +
  629. arg->getType ().getAsString (),
  630. arg, ctx->past, ctx->pci);
  631. return false;
  632. }
  633. auto struct_type = desugared_type->getAsStructureType ();
  634. auto struct_decl = struct_type->getDecl ();
  635. auto struct_def = struct_decl->getNameAsString ();
  636. if (struct_def != sname) {
  637. print_error (std::string ("bad argument '") + struct_def + "' for "
  638. + fmt + " arg: " +
  639. arg->getType ().getAsString (),
  640. arg, ctx->past, ctx->pci);
  641. return false;
  642. }
  643. return true;
  644. }
  645. static bool
  646. tok_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  647. {
  648. return check_struct_type (arg,
  649. ctx,
  650. "f_str_tok",
  651. "%T");
  652. }
  653. static bool
  654. fstring_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  655. {
  656. return check_struct_type (arg,
  657. ctx,
  658. "f_str_s",
  659. "%V");
  660. }
  661. static bool
  662. gstring_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  663. {
  664. return check_struct_type (arg,
  665. ctx,
  666. "_GString",
  667. "%v");
  668. }
  669. static bool
  670. gerr_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  671. {
  672. return check_struct_type (arg,
  673. ctx,
  674. "_GError",
  675. "%e");
  676. }
  677. };