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

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