ScanfFormatString.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 'A': k = ConversionSpecifier::AArg; break;
  144. case 'E': k = ConversionSpecifier::EArg; break;
  145. case 'F': k = ConversionSpecifier::FArg; break;
  146. case 'G': k = ConversionSpecifier::GArg; break;
  147. case 'X': k = ConversionSpecifier::XArg; break;
  148. case 'a': k = ConversionSpecifier::aArg; break;
  149. case 'd': k = ConversionSpecifier::dArg; break;
  150. case 'e': k = ConversionSpecifier::eArg; break;
  151. case 'f': k = ConversionSpecifier::fArg; break;
  152. case 'g': k = ConversionSpecifier::gArg; break;
  153. case 'i': k = ConversionSpecifier::iArg; break;
  154. case 'n': k = ConversionSpecifier::nArg; break;
  155. case 'c': k = ConversionSpecifier::cArg; break;
  156. case 'C': k = ConversionSpecifier::CArg; break;
  157. case 'S': k = ConversionSpecifier::SArg; break;
  158. case '[': k = ConversionSpecifier::ScanListArg; break;
  159. case 'u': k = ConversionSpecifier::uArg; break;
  160. case 'x': k = ConversionSpecifier::xArg; break;
  161. case 'o': k = ConversionSpecifier::oArg; break;
  162. case 's': k = ConversionSpecifier::sArg; break;
  163. case 'p': k = ConversionSpecifier::pArg; break;
  164. // Apple extensions
  165. // Apple-specific
  166. case 'D':
  167. if (Target.getTriple().isOSDarwin())
  168. k = ConversionSpecifier::DArg;
  169. break;
  170. case 'O':
  171. if (Target.getTriple().isOSDarwin())
  172. k = ConversionSpecifier::OArg;
  173. break;
  174. case 'U':
  175. if (Target.getTriple().isOSDarwin())
  176. k = ConversionSpecifier::UArg;
  177. break;
  178. }
  179. ScanfConversionSpecifier CS(conversionPosition, k);
  180. if (k == ScanfConversionSpecifier::ScanListArg) {
  181. if (ParseScanList(H, CS, I, E))
  182. return true;
  183. }
  184. FS.setConversionSpecifier(CS);
  185. if (CS.consumesDataArgument() && !FS.getSuppressAssignment()
  186. && !FS.usesPositionalArg())
  187. FS.setArgIndex(argIndex++);
  188. // FIXME: '%' and '*' doesn't make sense. Issue a warning.
  189. // FIXME: 'ConsumedSoFar' and '*' doesn't make sense.
  190. if (k == ScanfConversionSpecifier::InvalidSpecifier) {
  191. unsigned Len = I - Beg;
  192. if (ParseUTF8InvalidSpecifier(Beg, E, Len)) {
  193. CS.setEndScanList(Beg + Len);
  194. FS.setConversionSpecifier(CS);
  195. }
  196. // Assume the conversion takes one argument.
  197. return !H.HandleInvalidScanfConversionSpecifier(FS, Beg, Len);
  198. }
  199. return ScanfSpecifierResult(Start, FS);
  200. }
  201. ArgType ScanfSpecifier::getArgType(ASTContext &Ctx) const {
  202. const ScanfConversionSpecifier &CS = getConversionSpecifier();
  203. if (!CS.consumesDataArgument())
  204. return ArgType::Invalid();
  205. switch(CS.getKind()) {
  206. // Signed int.
  207. case ConversionSpecifier::dArg:
  208. case ConversionSpecifier::DArg:
  209. case ConversionSpecifier::iArg:
  210. switch (LM.getKind()) {
  211. case LengthModifier::None:
  212. return ArgType::PtrTo(Ctx.IntTy);
  213. case LengthModifier::AsChar:
  214. return ArgType::PtrTo(ArgType::AnyCharTy);
  215. case LengthModifier::AsShort:
  216. return ArgType::PtrTo(Ctx.ShortTy);
  217. case LengthModifier::AsLong:
  218. return ArgType::PtrTo(Ctx.LongTy);
  219. case LengthModifier::AsLongLong:
  220. case LengthModifier::AsQuad:
  221. return ArgType::PtrTo(Ctx.LongLongTy);
  222. case LengthModifier::AsInt64:
  223. return ArgType::PtrTo(ArgType(Ctx.LongLongTy, "__int64"));
  224. case LengthModifier::AsIntMax:
  225. return ArgType::PtrTo(ArgType(Ctx.getIntMaxType(), "intmax_t"));
  226. case LengthModifier::AsSizeT:
  227. return ArgType::PtrTo(ArgType(Ctx.getSignedSizeType(), "ssize_t"));
  228. case LengthModifier::AsPtrDiff:
  229. return ArgType::PtrTo(ArgType(Ctx.getPointerDiffType(), "ptrdiff_t"));
  230. case LengthModifier::AsLongDouble:
  231. // GNU extension.
  232. return ArgType::PtrTo(Ctx.LongLongTy);
  233. case LengthModifier::AsAllocate:
  234. case LengthModifier::AsMAllocate:
  235. case LengthModifier::AsInt32:
  236. case LengthModifier::AsInt3264:
  237. case LengthModifier::AsWide:
  238. case LengthModifier::AsShortLong:
  239. return ArgType::Invalid();
  240. }
  241. llvm_unreachable("Unsupported LengthModifier Type");
  242. // Unsigned int.
  243. case ConversionSpecifier::oArg:
  244. case ConversionSpecifier::OArg:
  245. case ConversionSpecifier::uArg:
  246. case ConversionSpecifier::UArg:
  247. case ConversionSpecifier::xArg:
  248. case ConversionSpecifier::XArg:
  249. switch (LM.getKind()) {
  250. case LengthModifier::None:
  251. return ArgType::PtrTo(Ctx.UnsignedIntTy);
  252. case LengthModifier::AsChar:
  253. return ArgType::PtrTo(Ctx.UnsignedCharTy);
  254. case LengthModifier::AsShort:
  255. return ArgType::PtrTo(Ctx.UnsignedShortTy);
  256. case LengthModifier::AsLong:
  257. return ArgType::PtrTo(Ctx.UnsignedLongTy);
  258. case LengthModifier::AsLongLong:
  259. case LengthModifier::AsQuad:
  260. return ArgType::PtrTo(Ctx.UnsignedLongLongTy);
  261. case LengthModifier::AsInt64:
  262. return ArgType::PtrTo(ArgType(Ctx.UnsignedLongLongTy, "unsigned __int64"));
  263. case LengthModifier::AsIntMax:
  264. return ArgType::PtrTo(ArgType(Ctx.getUIntMaxType(), "uintmax_t"));
  265. case LengthModifier::AsSizeT:
  266. return ArgType::PtrTo(ArgType(Ctx.getSizeType(), "size_t"));
  267. case LengthModifier::AsPtrDiff:
  268. return ArgType::PtrTo(
  269. ArgType(Ctx.getUnsignedPointerDiffType(), "unsigned ptrdiff_t"));
  270. case LengthModifier::AsLongDouble:
  271. // GNU extension.
  272. return ArgType::PtrTo(Ctx.UnsignedLongLongTy);
  273. case LengthModifier::AsAllocate:
  274. case LengthModifier::AsMAllocate:
  275. case LengthModifier::AsInt32:
  276. case LengthModifier::AsInt3264:
  277. case LengthModifier::AsWide:
  278. case LengthModifier::AsShortLong:
  279. return ArgType::Invalid();
  280. }
  281. llvm_unreachable("Unsupported LengthModifier Type");
  282. // Float.
  283. case ConversionSpecifier::aArg:
  284. case ConversionSpecifier::AArg:
  285. case ConversionSpecifier::eArg:
  286. case ConversionSpecifier::EArg:
  287. case ConversionSpecifier::fArg:
  288. case ConversionSpecifier::FArg:
  289. case ConversionSpecifier::gArg:
  290. case ConversionSpecifier::GArg:
  291. switch (LM.getKind()) {
  292. case LengthModifier::None:
  293. return ArgType::PtrTo(Ctx.FloatTy);
  294. case LengthModifier::AsLong:
  295. return ArgType::PtrTo(Ctx.DoubleTy);
  296. case LengthModifier::AsLongDouble:
  297. return ArgType::PtrTo(Ctx.LongDoubleTy);
  298. default:
  299. return ArgType::Invalid();
  300. }
  301. // Char, string and scanlist.
  302. case ConversionSpecifier::cArg:
  303. case ConversionSpecifier::sArg:
  304. case ConversionSpecifier::ScanListArg:
  305. switch (LM.getKind()) {
  306. case LengthModifier::None:
  307. return ArgType::PtrTo(ArgType::AnyCharTy);
  308. case LengthModifier::AsLong:
  309. case LengthModifier::AsWide:
  310. return ArgType::PtrTo(ArgType(Ctx.getWideCharType(), "wchar_t"));
  311. case LengthModifier::AsAllocate:
  312. case LengthModifier::AsMAllocate:
  313. return ArgType::PtrTo(ArgType::CStrTy);
  314. case LengthModifier::AsShort:
  315. if (Ctx.getTargetInfo().getTriple().isOSMSVCRT())
  316. return ArgType::PtrTo(ArgType::AnyCharTy);
  317. LLVM_FALLTHROUGH;
  318. default:
  319. return ArgType::Invalid();
  320. }
  321. case ConversionSpecifier::CArg:
  322. case ConversionSpecifier::SArg:
  323. // FIXME: Mac OS X specific?
  324. switch (LM.getKind()) {
  325. case LengthModifier::None:
  326. case LengthModifier::AsWide:
  327. return ArgType::PtrTo(ArgType(Ctx.getWideCharType(), "wchar_t"));
  328. case LengthModifier::AsAllocate:
  329. case LengthModifier::AsMAllocate:
  330. return ArgType::PtrTo(ArgType(ArgType::WCStrTy, "wchar_t *"));
  331. case LengthModifier::AsShort:
  332. if (Ctx.getTargetInfo().getTriple().isOSMSVCRT())
  333. return ArgType::PtrTo(ArgType::AnyCharTy);
  334. LLVM_FALLTHROUGH;
  335. default:
  336. return ArgType::Invalid();
  337. }
  338. // Pointer.
  339. case ConversionSpecifier::pArg:
  340. return ArgType::PtrTo(ArgType::CPointerTy);
  341. // Write-back.
  342. case ConversionSpecifier::nArg:
  343. switch (LM.getKind()) {
  344. case LengthModifier::None:
  345. return ArgType::PtrTo(Ctx.IntTy);
  346. case LengthModifier::AsChar:
  347. return ArgType::PtrTo(Ctx.SignedCharTy);
  348. case LengthModifier::AsShort:
  349. return ArgType::PtrTo(Ctx.ShortTy);
  350. case LengthModifier::AsLong:
  351. return ArgType::PtrTo(Ctx.LongTy);
  352. case LengthModifier::AsLongLong:
  353. case LengthModifier::AsQuad:
  354. return ArgType::PtrTo(Ctx.LongLongTy);
  355. case LengthModifier::AsInt64:
  356. return ArgType::PtrTo(ArgType(Ctx.LongLongTy, "__int64"));
  357. case LengthModifier::AsIntMax:
  358. return ArgType::PtrTo(ArgType(Ctx.getIntMaxType(), "intmax_t"));
  359. case LengthModifier::AsSizeT:
  360. return ArgType::PtrTo(ArgType(Ctx.getSignedSizeType(), "ssize_t"));
  361. case LengthModifier::AsPtrDiff:
  362. return ArgType::PtrTo(ArgType(Ctx.getPointerDiffType(), "ptrdiff_t"));
  363. case LengthModifier::AsLongDouble:
  364. return ArgType(); // FIXME: Is this a known extension?
  365. case LengthModifier::AsAllocate:
  366. case LengthModifier::AsMAllocate:
  367. case LengthModifier::AsInt32:
  368. case LengthModifier::AsInt3264:
  369. case LengthModifier::AsWide:
  370. case LengthModifier::AsShortLong:
  371. return ArgType::Invalid();
  372. }
  373. default:
  374. break;
  375. }
  376. return ArgType();
  377. }
  378. bool ScanfSpecifier::fixType(QualType QT, QualType RawQT,
  379. const LangOptions &LangOpt,
  380. ASTContext &Ctx) {
  381. // %n is different from other conversion specifiers; don't try to fix it.
  382. if (CS.getKind() == ConversionSpecifier::nArg)
  383. return false;
  384. if (!QT->isPointerType())
  385. return false;
  386. QualType PT = QT->getPointeeType();
  387. // If it's an enum, get its underlying type.
  388. if (const EnumType *ETy = PT->getAs<EnumType>()) {
  389. // Don't try to fix incomplete enums.
  390. if (!ETy->getDecl()->isComplete())
  391. return false;
  392. PT = ETy->getDecl()->getIntegerType();
  393. }
  394. const BuiltinType *BT = PT->getAs<BuiltinType>();
  395. if (!BT)
  396. return false;
  397. // Pointer to a character.
  398. if (PT->isAnyCharacterType()) {
  399. CS.setKind(ConversionSpecifier::sArg);
  400. if (PT->isWideCharType())
  401. LM.setKind(LengthModifier::AsWideChar);
  402. else
  403. LM.setKind(LengthModifier::None);
  404. // If we know the target array length, we can use it as a field width.
  405. if (const ConstantArrayType *CAT = Ctx.getAsConstantArrayType(RawQT)) {
  406. if (CAT->getSizeModifier() == ArrayType::Normal)
  407. FieldWidth = OptionalAmount(OptionalAmount::Constant,
  408. CAT->getSize().getZExtValue() - 1,
  409. "", 0, false);
  410. }
  411. return true;
  412. }
  413. // Figure out the length modifier.
  414. switch (BT->getKind()) {
  415. // no modifier
  416. case BuiltinType::UInt:
  417. case BuiltinType::Int:
  418. case BuiltinType::Float:
  419. LM.setKind(LengthModifier::None);
  420. break;
  421. // hh
  422. case BuiltinType::Char_U:
  423. case BuiltinType::UChar:
  424. case BuiltinType::Char_S:
  425. case BuiltinType::SChar:
  426. LM.setKind(LengthModifier::AsChar);
  427. break;
  428. // h
  429. case BuiltinType::Short:
  430. case BuiltinType::UShort:
  431. LM.setKind(LengthModifier::AsShort);
  432. break;
  433. // l
  434. case BuiltinType::Long:
  435. case BuiltinType::ULong:
  436. case BuiltinType::Double:
  437. LM.setKind(LengthModifier::AsLong);
  438. break;
  439. // ll
  440. case BuiltinType::LongLong:
  441. case BuiltinType::ULongLong:
  442. LM.setKind(LengthModifier::AsLongLong);
  443. break;
  444. // L
  445. case BuiltinType::LongDouble:
  446. LM.setKind(LengthModifier::AsLongDouble);
  447. break;
  448. // Don't know.
  449. default:
  450. return false;
  451. }
  452. // Handle size_t, ptrdiff_t, etc. that have dedicated length modifiers in C99.
  453. if (isa<TypedefType>(PT) && (LangOpt.C99 || LangOpt.CPlusPlus11))
  454. namedTypeToLengthModifier(PT, LM);
  455. // If fixing the length modifier was enough, we are done.
  456. if (hasValidLengthModifier(Ctx.getTargetInfo(), LangOpt)) {
  457. const analyze_scanf::ArgType &AT = getArgType(Ctx);
  458. if (AT.isValid() && AT.matchesType(Ctx, QT))
  459. return true;
  460. }
  461. // Figure out the conversion specifier.
  462. if (PT->isRealFloatingType())
  463. CS.setKind(ConversionSpecifier::fArg);
  464. else if (PT->isSignedIntegerType())
  465. CS.setKind(ConversionSpecifier::dArg);
  466. else if (PT->isUnsignedIntegerType())
  467. CS.setKind(ConversionSpecifier::uArg);
  468. else
  469. llvm_unreachable("Unexpected type");
  470. return true;
  471. }
  472. void ScanfSpecifier::toString(raw_ostream &os) const {
  473. os << "%";
  474. if (usesPositionalArg())
  475. os << getPositionalArgIndex() << "$";
  476. if (SuppressAssignment)
  477. os << "*";
  478. FieldWidth.toString(os);
  479. os << LM.toString();
  480. os << CS.toString();
  481. }
  482. bool clang::analyze_format_string::ParseScanfString(FormatStringHandler &H,
  483. const char *I,
  484. const char *E,
  485. const LangOptions &LO,
  486. const TargetInfo &Target) {
  487. unsigned argIndex = 0;
  488. // Keep looking for a format specifier until we have exhausted the string.
  489. while (I != E) {
  490. const ScanfSpecifierResult &FSR = ParseScanfSpecifier(H, I, E, argIndex,
  491. LO, Target);
  492. // Did a fail-stop error of any kind occur when parsing the specifier?
  493. // If so, don't do any more processing.
  494. if (FSR.shouldStop())
  495. return true;
  496. // Did we exhaust the string or encounter an error that
  497. // we can recover from?
  498. if (!FSR.hasValue())
  499. continue;
  500. // We have a format specifier. Pass it to the callback.
  501. if (!H.HandleScanfSpecifier(FSR.getValue(), FSR.getStart(),
  502. I - FSR.getStart())) {
  503. return true;
  504. }
  505. }
  506. assert(I == E && "Format string not exhausted");
  507. return false;
  508. }