ScanfFormatString.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. //= ScanfFormatString.cpp - Analysis of printf format strings --*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Handling of format string in scanf and friends. The structure of format
  10. // strings for fscanf() are described in C99 7.19.6.2.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/FormatString.h"
  14. #include "FormatStringParsing.h"
  15. #include "clang/Basic/TargetInfo.h"
  16. using clang::analyze_format_string::ArgType;
  17. using clang::analyze_format_string::FormatStringHandler;
  18. using clang::analyze_format_string::LengthModifier;
  19. using clang::analyze_format_string::OptionalAmount;
  20. using clang::analyze_format_string::ConversionSpecifier;
  21. using clang::analyze_scanf::ScanfConversionSpecifier;
  22. using clang::analyze_scanf::ScanfSpecifier;
  23. using clang::UpdateOnReturn;
  24. using namespace clang;
  25. typedef clang::analyze_format_string::SpecifierResult<ScanfSpecifier>
  26. ScanfSpecifierResult;
  27. static bool ParseScanList(FormatStringHandler &H,
  28. ScanfConversionSpecifier &CS,
  29. const char *&Beg, const char *E) {
  30. const char *I = Beg;
  31. const char *start = I - 1;
  32. UpdateOnReturn <const char*> UpdateBeg(Beg, I);
  33. // No more characters?
  34. if (I == E) {
  35. H.HandleIncompleteScanList(start, I);
  36. return true;
  37. }
  38. // Special case: ']' is the first character.
  39. if (*I == ']') {
  40. if (++I == E) {
  41. H.HandleIncompleteScanList(start, I - 1);
  42. return true;
  43. }
  44. }
  45. // Special case: "^]" are the first characters.
  46. if (I + 1 != E && I[0] == '^' && I[1] == ']') {
  47. I += 2;
  48. if (I == E) {
  49. H.HandleIncompleteScanList(start, I - 1);
  50. return true;
  51. }
  52. }
  53. // Look for a ']' character which denotes the end of the scan list.
  54. while (*I != ']') {
  55. if (++I == E) {
  56. H.HandleIncompleteScanList(start, I - 1);
  57. return true;
  58. }
  59. }
  60. CS.setEndScanList(I);
  61. return false;
  62. }
  63. // FIXME: Much of this is copy-paste from ParsePrintfSpecifier.
  64. // We can possibly refactor.
  65. static ScanfSpecifierResult ParseScanfSpecifier(FormatStringHandler &H,
  66. const char *&Beg,
  67. const char *E,
  68. unsigned &argIndex,
  69. const LangOptions &LO,
  70. const TargetInfo &Target) {
  71. using namespace clang::analyze_format_string;
  72. using namespace clang::analyze_scanf;
  73. const char *I = Beg;
  74. const char *Start = nullptr;
  75. UpdateOnReturn <const char*> UpdateBeg(Beg, I);
  76. // Look for a '%' character that indicates the start of a format specifier.
  77. for ( ; I != E ; ++I) {
  78. char c = *I;
  79. if (c == '\0') {
  80. // Detect spurious null characters, which are likely errors.
  81. H.HandleNullChar(I);
  82. return true;
  83. }
  84. if (c == '%') {
  85. Start = I++; // Record the start of the format specifier.
  86. break;
  87. }
  88. }
  89. // No format specifier found?
  90. if (!Start)
  91. return false;
  92. if (I == E) {
  93. // No more characters left?
  94. H.HandleIncompleteSpecifier(Start, E - Start);
  95. return true;
  96. }
  97. ScanfSpecifier FS;
  98. if (ParseArgPosition(H, FS, Start, I, E))
  99. return true;
  100. if (I == E) {
  101. // No more characters left?
  102. H.HandleIncompleteSpecifier(Start, E - Start);
  103. return true;
  104. }
  105. // Look for '*' flag if it is present.
  106. if (*I == '*') {
  107. FS.setSuppressAssignment(I);
  108. if (++I == E) {
  109. H.HandleIncompleteSpecifier(Start, E - Start);
  110. return true;
  111. }
  112. }
  113. // Look for the field width (if any). Unlike printf, this is either
  114. // a fixed integer or isn't present.
  115. const OptionalAmount &Amt = clang::analyze_format_string::ParseAmount(I, E);
  116. if (Amt.getHowSpecified() != OptionalAmount::NotSpecified) {
  117. assert(Amt.getHowSpecified() == OptionalAmount::Constant);
  118. FS.setFieldWidth(Amt);
  119. if (I == E) {
  120. // No more characters left?
  121. H.HandleIncompleteSpecifier(Start, E - Start);
  122. return true;
  123. }
  124. }
  125. // Look for the length modifier.
  126. if (ParseLengthModifier(FS, I, E, LO, /*IsScanf=*/true) && I == E) {
  127. // No more characters left?
  128. H.HandleIncompleteSpecifier(Start, E - Start);
  129. return true;
  130. }
  131. // Detect spurious null characters, which are likely errors.
  132. if (*I == '\0') {
  133. H.HandleNullChar(I);
  134. return true;
  135. }
  136. // Finally, look for the conversion specifier.
  137. const char *conversionPosition = I++;
  138. ScanfConversionSpecifier::Kind k = ScanfConversionSpecifier::InvalidSpecifier;
  139. switch (*conversionPosition) {
  140. default:
  141. break;
  142. case '%': k = ConversionSpecifier::PercentArg; break;
  143. case 'b': k = ConversionSpecifier::bArg; break;
  144. case 'A': k = ConversionSpecifier::AArg; break;
  145. case 'E': k = ConversionSpecifier::EArg; break;
  146. case 'F': k = ConversionSpecifier::FArg; break;
  147. case 'G': k = ConversionSpecifier::GArg; break;
  148. case 'X': k = ConversionSpecifier::XArg; break;
  149. case 'a': k = ConversionSpecifier::aArg; break;
  150. case 'd': k = ConversionSpecifier::dArg; break;
  151. case 'e': k = ConversionSpecifier::eArg; break;
  152. case 'f': k = ConversionSpecifier::fArg; break;
  153. case 'g': k = ConversionSpecifier::gArg; break;
  154. case 'i': k = ConversionSpecifier::iArg; break;
  155. case 'n': k = ConversionSpecifier::nArg; break;
  156. case 'c': k = ConversionSpecifier::cArg; break;
  157. case 'C': k = ConversionSpecifier::CArg; break;
  158. case 'S': k = ConversionSpecifier::SArg; break;
  159. case '[': k = ConversionSpecifier::ScanListArg; break;
  160. case 'u': k = ConversionSpecifier::uArg; break;
  161. case 'x': k = ConversionSpecifier::xArg; break;
  162. case 'o': k = ConversionSpecifier::oArg; break;
  163. case 's': k = ConversionSpecifier::sArg; break;
  164. case 'p': k = ConversionSpecifier::pArg; break;
  165. // Apple extensions
  166. // Apple-specific
  167. case 'D':
  168. if (Target.getTriple().isOSDarwin())
  169. k = ConversionSpecifier::DArg;
  170. break;
  171. case 'O':
  172. if (Target.getTriple().isOSDarwin())
  173. k = ConversionSpecifier::OArg;
  174. break;
  175. case 'U':
  176. if (Target.getTriple().isOSDarwin())
  177. k = ConversionSpecifier::UArg;
  178. break;
  179. }
  180. ScanfConversionSpecifier CS(conversionPosition, k);
  181. if (k == ScanfConversionSpecifier::ScanListArg) {
  182. if (ParseScanList(H, CS, I, E))
  183. return true;
  184. }
  185. FS.setConversionSpecifier(CS);
  186. if (CS.consumesDataArgument() && !FS.getSuppressAssignment()
  187. && !FS.usesPositionalArg())
  188. FS.setArgIndex(argIndex++);
  189. // FIXME: '%' and '*' doesn't make sense. Issue a warning.
  190. // FIXME: 'ConsumedSoFar' and '*' doesn't make sense.
  191. if (k == ScanfConversionSpecifier::InvalidSpecifier) {
  192. unsigned Len = I - Beg;
  193. if (ParseUTF8InvalidSpecifier(Beg, E, Len)) {
  194. CS.setEndScanList(Beg + Len);
  195. FS.setConversionSpecifier(CS);
  196. }
  197. // Assume the conversion takes one argument.
  198. return !H.HandleInvalidScanfConversionSpecifier(FS, Beg, Len);
  199. }
  200. return ScanfSpecifierResult(Start, FS);
  201. }
  202. ArgType ScanfSpecifier::getArgType(ASTContext &Ctx) const {
  203. const ScanfConversionSpecifier &CS = getConversionSpecifier();
  204. if (!CS.consumesDataArgument())
  205. return ArgType::Invalid();
  206. switch(CS.getKind()) {
  207. // Signed int.
  208. case ConversionSpecifier::dArg:
  209. case ConversionSpecifier::DArg:
  210. case ConversionSpecifier::iArg:
  211. switch (LM.getKind()) {
  212. case LengthModifier::None:
  213. return ArgType::PtrTo(Ctx.IntTy);
  214. case LengthModifier::AsChar:
  215. return ArgType::PtrTo(ArgType::AnyCharTy);
  216. case LengthModifier::AsShort:
  217. return ArgType::PtrTo(Ctx.ShortTy);
  218. case LengthModifier::AsLong:
  219. return ArgType::PtrTo(Ctx.LongTy);
  220. case LengthModifier::AsLongLong:
  221. case LengthModifier::AsQuad:
  222. return ArgType::PtrTo(Ctx.LongLongTy);
  223. case LengthModifier::AsInt64:
  224. return ArgType::PtrTo(ArgType(Ctx.LongLongTy, "__int64"));
  225. case LengthModifier::AsIntMax:
  226. return ArgType::PtrTo(ArgType(Ctx.getIntMaxType(), "intmax_t"));
  227. case LengthModifier::AsSizeT:
  228. return ArgType::PtrTo(ArgType(Ctx.getSignedSizeType(), "ssize_t"));
  229. case LengthModifier::AsPtrDiff:
  230. return ArgType::PtrTo(ArgType(Ctx.getPointerDiffType(), "ptrdiff_t"));
  231. case LengthModifier::AsLongDouble:
  232. // GNU extension.
  233. return ArgType::PtrTo(Ctx.LongLongTy);
  234. case LengthModifier::AsAllocate:
  235. case LengthModifier::AsMAllocate:
  236. case LengthModifier::AsInt32:
  237. case LengthModifier::AsInt3264:
  238. case LengthModifier::AsWide:
  239. case LengthModifier::AsShortLong:
  240. return ArgType::Invalid();
  241. }
  242. llvm_unreachable("Unsupported LengthModifier Type");
  243. // Unsigned int.
  244. case ConversionSpecifier::bArg:
  245. case ConversionSpecifier::oArg:
  246. case ConversionSpecifier::OArg:
  247. case ConversionSpecifier::uArg:
  248. case ConversionSpecifier::UArg:
  249. case ConversionSpecifier::xArg:
  250. case ConversionSpecifier::XArg:
  251. switch (LM.getKind()) {
  252. case LengthModifier::None:
  253. return ArgType::PtrTo(Ctx.UnsignedIntTy);
  254. case LengthModifier::AsChar:
  255. return ArgType::PtrTo(Ctx.UnsignedCharTy);
  256. case LengthModifier::AsShort:
  257. return ArgType::PtrTo(Ctx.UnsignedShortTy);
  258. case LengthModifier::AsLong:
  259. return ArgType::PtrTo(Ctx.UnsignedLongTy);
  260. case LengthModifier::AsLongLong:
  261. case LengthModifier::AsQuad:
  262. return ArgType::PtrTo(Ctx.UnsignedLongLongTy);
  263. case LengthModifier::AsInt64:
  264. return ArgType::PtrTo(ArgType(Ctx.UnsignedLongLongTy, "unsigned __int64"));
  265. case LengthModifier::AsIntMax:
  266. return ArgType::PtrTo(ArgType(Ctx.getUIntMaxType(), "uintmax_t"));
  267. case LengthModifier::AsSizeT:
  268. return ArgType::PtrTo(ArgType(Ctx.getSizeType(), "size_t"));
  269. case LengthModifier::AsPtrDiff:
  270. return ArgType::PtrTo(
  271. ArgType(Ctx.getUnsignedPointerDiffType(), "unsigned ptrdiff_t"));
  272. case LengthModifier::AsLongDouble:
  273. // GNU extension.
  274. return ArgType::PtrTo(Ctx.UnsignedLongLongTy);
  275. case LengthModifier::AsAllocate:
  276. case LengthModifier::AsMAllocate:
  277. case LengthModifier::AsInt32:
  278. case LengthModifier::AsInt3264:
  279. case LengthModifier::AsWide:
  280. case LengthModifier::AsShortLong:
  281. return ArgType::Invalid();
  282. }
  283. llvm_unreachable("Unsupported LengthModifier Type");
  284. // Float.
  285. case ConversionSpecifier::aArg:
  286. case ConversionSpecifier::AArg:
  287. case ConversionSpecifier::eArg:
  288. case ConversionSpecifier::EArg:
  289. case ConversionSpecifier::fArg:
  290. case ConversionSpecifier::FArg:
  291. case ConversionSpecifier::gArg:
  292. case ConversionSpecifier::GArg:
  293. switch (LM.getKind()) {
  294. case LengthModifier::None:
  295. return ArgType::PtrTo(Ctx.FloatTy);
  296. case LengthModifier::AsLong:
  297. return ArgType::PtrTo(Ctx.DoubleTy);
  298. case LengthModifier::AsLongDouble:
  299. return ArgType::PtrTo(Ctx.LongDoubleTy);
  300. default:
  301. return ArgType::Invalid();
  302. }
  303. // Char, string and scanlist.
  304. case ConversionSpecifier::cArg:
  305. case ConversionSpecifier::sArg:
  306. case ConversionSpecifier::ScanListArg:
  307. switch (LM.getKind()) {
  308. case LengthModifier::None:
  309. return ArgType::PtrTo(ArgType::AnyCharTy);
  310. case LengthModifier::AsLong:
  311. case LengthModifier::AsWide:
  312. return ArgType::PtrTo(ArgType(Ctx.getWideCharType(), "wchar_t"));
  313. case LengthModifier::AsAllocate:
  314. case LengthModifier::AsMAllocate:
  315. return ArgType::PtrTo(ArgType::CStrTy);
  316. case LengthModifier::AsShort:
  317. if (Ctx.getTargetInfo().getTriple().isOSMSVCRT())
  318. return ArgType::PtrTo(ArgType::AnyCharTy);
  319. [[fallthrough]];
  320. default:
  321. return ArgType::Invalid();
  322. }
  323. case ConversionSpecifier::CArg:
  324. case ConversionSpecifier::SArg:
  325. // FIXME: Mac OS X specific?
  326. switch (LM.getKind()) {
  327. case LengthModifier::None:
  328. case LengthModifier::AsWide:
  329. return ArgType::PtrTo(ArgType(Ctx.getWideCharType(), "wchar_t"));
  330. case LengthModifier::AsAllocate:
  331. case LengthModifier::AsMAllocate:
  332. return ArgType::PtrTo(ArgType(ArgType::WCStrTy, "wchar_t *"));
  333. case LengthModifier::AsShort:
  334. if (Ctx.getTargetInfo().getTriple().isOSMSVCRT())
  335. return ArgType::PtrTo(ArgType::AnyCharTy);
  336. [[fallthrough]];
  337. default:
  338. return ArgType::Invalid();
  339. }
  340. // Pointer.
  341. case ConversionSpecifier::pArg:
  342. return ArgType::PtrTo(ArgType::CPointerTy);
  343. // Write-back.
  344. case ConversionSpecifier::nArg:
  345. switch (LM.getKind()) {
  346. case LengthModifier::None:
  347. return ArgType::PtrTo(Ctx.IntTy);
  348. case LengthModifier::AsChar:
  349. return ArgType::PtrTo(Ctx.SignedCharTy);
  350. case LengthModifier::AsShort:
  351. return ArgType::PtrTo(Ctx.ShortTy);
  352. case LengthModifier::AsLong:
  353. return ArgType::PtrTo(Ctx.LongTy);
  354. case LengthModifier::AsLongLong:
  355. case LengthModifier::AsQuad:
  356. return ArgType::PtrTo(Ctx.LongLongTy);
  357. case LengthModifier::AsInt64:
  358. return ArgType::PtrTo(ArgType(Ctx.LongLongTy, "__int64"));
  359. case LengthModifier::AsIntMax:
  360. return ArgType::PtrTo(ArgType(Ctx.getIntMaxType(), "intmax_t"));
  361. case LengthModifier::AsSizeT:
  362. return ArgType::PtrTo(ArgType(Ctx.getSignedSizeType(), "ssize_t"));
  363. case LengthModifier::AsPtrDiff:
  364. return ArgType::PtrTo(ArgType(Ctx.getPointerDiffType(), "ptrdiff_t"));
  365. case LengthModifier::AsLongDouble:
  366. return ArgType(); // FIXME: Is this a known extension?
  367. case LengthModifier::AsAllocate:
  368. case LengthModifier::AsMAllocate:
  369. case LengthModifier::AsInt32:
  370. case LengthModifier::AsInt3264:
  371. case LengthModifier::AsWide:
  372. case LengthModifier::AsShortLong:
  373. return ArgType::Invalid();
  374. }
  375. default:
  376. break;
  377. }
  378. return ArgType();
  379. }
  380. bool ScanfSpecifier::fixType(QualType QT, QualType RawQT,
  381. const LangOptions &LangOpt,
  382. ASTContext &Ctx) {
  383. // %n is different from other conversion specifiers; don't try to fix it.
  384. if (CS.getKind() == ConversionSpecifier::nArg)
  385. return false;
  386. if (!QT->isPointerType())
  387. return false;
  388. QualType PT = QT->getPointeeType();
  389. // If it's an enum, get its underlying type.
  390. if (const EnumType *ETy = PT->getAs<EnumType>()) {
  391. // Don't try to fix incomplete enums.
  392. if (!ETy->getDecl()->isComplete())
  393. return false;
  394. PT = ETy->getDecl()->getIntegerType();
  395. }
  396. const BuiltinType *BT = PT->getAs<BuiltinType>();
  397. if (!BT)
  398. return false;
  399. // Pointer to a character.
  400. if (PT->isAnyCharacterType()) {
  401. CS.setKind(ConversionSpecifier::sArg);
  402. if (PT->isWideCharType())
  403. LM.setKind(LengthModifier::AsWideChar);
  404. else
  405. LM.setKind(LengthModifier::None);
  406. // If we know the target array length, we can use it as a field width.
  407. if (const ConstantArrayType *CAT = Ctx.getAsConstantArrayType(RawQT)) {
  408. if (CAT->getSizeModifier() == ArrayType::Normal)
  409. FieldWidth = OptionalAmount(OptionalAmount::Constant,
  410. CAT->getSize().getZExtValue() - 1,
  411. "", 0, false);
  412. }
  413. return true;
  414. }
  415. // Figure out the length modifier.
  416. switch (BT->getKind()) {
  417. // no modifier
  418. case BuiltinType::UInt:
  419. case BuiltinType::Int:
  420. case BuiltinType::Float:
  421. LM.setKind(LengthModifier::None);
  422. break;
  423. // hh
  424. case BuiltinType::Char_U:
  425. case BuiltinType::UChar:
  426. case BuiltinType::Char_S:
  427. case BuiltinType::SChar:
  428. LM.setKind(LengthModifier::AsChar);
  429. break;
  430. // h
  431. case BuiltinType::Short:
  432. case BuiltinType::UShort:
  433. LM.setKind(LengthModifier::AsShort);
  434. break;
  435. // l
  436. case BuiltinType::Long:
  437. case BuiltinType::ULong:
  438. case BuiltinType::Double:
  439. LM.setKind(LengthModifier::AsLong);
  440. break;
  441. // ll
  442. case BuiltinType::LongLong:
  443. case BuiltinType::ULongLong:
  444. LM.setKind(LengthModifier::AsLongLong);
  445. break;
  446. // L
  447. case BuiltinType::LongDouble:
  448. LM.setKind(LengthModifier::AsLongDouble);
  449. break;
  450. // Don't know.
  451. default:
  452. return false;
  453. }
  454. // Handle size_t, ptrdiff_t, etc. that have dedicated length modifiers in C99.
  455. if (LangOpt.C99 || LangOpt.CPlusPlus11)
  456. namedTypeToLengthModifier(PT, LM);
  457. // If fixing the length modifier was enough, we are done.
  458. if (hasValidLengthModifier(Ctx.getTargetInfo(), LangOpt)) {
  459. const analyze_scanf::ArgType &AT = getArgType(Ctx);
  460. if (AT.isValid() && AT.matchesType(Ctx, QT))
  461. return true;
  462. }
  463. // Figure out the conversion specifier.
  464. if (PT->isRealFloatingType())
  465. CS.setKind(ConversionSpecifier::fArg);
  466. else if (PT->isSignedIntegerType())
  467. CS.setKind(ConversionSpecifier::dArg);
  468. else if (PT->isUnsignedIntegerType())
  469. CS.setKind(ConversionSpecifier::uArg);
  470. else
  471. llvm_unreachable("Unexpected type");
  472. return true;
  473. }
  474. void ScanfSpecifier::toString(raw_ostream &os) const {
  475. os << "%";
  476. if (usesPositionalArg())
  477. os << getPositionalArgIndex() << "$";
  478. if (SuppressAssignment)
  479. os << "*";
  480. FieldWidth.toString(os);
  481. os << LM.toString();
  482. os << CS.toString();
  483. }
  484. bool clang::analyze_format_string::ParseScanfString(FormatStringHandler &H,
  485. const char *I,
  486. const char *E,
  487. const LangOptions &LO,
  488. const TargetInfo &Target) {
  489. unsigned argIndex = 0;
  490. // Keep looking for a format specifier until we have exhausted the string.
  491. while (I != E) {
  492. const ScanfSpecifierResult &FSR = ParseScanfSpecifier(H, I, E, argIndex,
  493. LO, Target);
  494. // Did a fail-stop error of any kind occur when parsing the specifier?
  495. // If so, don't do any more processing.
  496. if (FSR.shouldStop())
  497. return true;
  498. // Did we exhaust the string or encounter an error that
  499. // we can recover from?
  500. if (!FSR.hasValue())
  501. continue;
  502. // We have a format specifier. Pass it to the callback.
  503. if (!H.HandleScanfSpecifier(FSR.getValue(), FSR.getStart(),
  504. I - FSR.getStart())) {
  505. return true;
  506. }
  507. }
  508. assert(I == E && "Format string not exhausted");
  509. return false;
  510. }