2010-07-16 10:11:31 +08:00
|
|
|
//= ScanfFormatString.cpp - Analysis of printf format strings --*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Handling of format string in scanf and friends. The structure of format
|
|
|
|
// strings for fscanf() are described in C99 7.19.6.2.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/Analyses/FormatString.h"
|
|
|
|
#include "FormatStringParsing.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2010-07-16 10:11:31 +08:00
|
|
|
|
2012-08-07 16:11:26 +08:00
|
|
|
using clang::analyze_format_string::ArgType;
|
2010-07-16 10:11:31 +08:00
|
|
|
using clang::analyze_format_string::FormatStringHandler;
|
|
|
|
using clang::analyze_format_string::LengthModifier;
|
|
|
|
using clang::analyze_format_string::OptionalAmount;
|
2010-07-21 04:04:47 +08:00
|
|
|
using clang::analyze_format_string::ConversionSpecifier;
|
2010-07-21 04:04:27 +08:00
|
|
|
using clang::analyze_scanf::ScanfConversionSpecifier;
|
2010-07-16 10:11:31 +08:00
|
|
|
using clang::analyze_scanf::ScanfSpecifier;
|
2010-07-27 05:25:24 +08:00
|
|
|
using clang::UpdateOnReturn;
|
2011-12-10 21:20:11 +08:00
|
|
|
using namespace clang;
|
2010-07-16 10:11:31 +08:00
|
|
|
|
|
|
|
typedef clang::analyze_format_string::SpecifierResult<ScanfSpecifier>
|
|
|
|
ScanfSpecifierResult;
|
|
|
|
|
|
|
|
static bool ParseScanList(FormatStringHandler &H,
|
2010-07-21 04:04:27 +08:00
|
|
|
ScanfConversionSpecifier &CS,
|
2010-07-16 10:11:31 +08:00
|
|
|
const char *&Beg, const char *E) {
|
|
|
|
const char *I = Beg;
|
|
|
|
const char *start = I - 1;
|
|
|
|
UpdateOnReturn <const char*> UpdateBeg(Beg, I);
|
|
|
|
|
|
|
|
// No more characters?
|
|
|
|
if (I == E) {
|
|
|
|
H.HandleIncompleteScanList(start, I);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case: ']' is the first character.
|
|
|
|
if (*I == ']') {
|
|
|
|
if (++I == E) {
|
2010-07-17 02:28:03 +08:00
|
|
|
H.HandleIncompleteScanList(start, I - 1);
|
2010-07-16 10:11:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-03 02:12:30 +08:00
|
|
|
// Special case: "^]" are the first characters.
|
2014-04-30 03:42:27 +08:00
|
|
|
if (I + 1 != E && I[0] == '^' && I[1] == ']') {
|
|
|
|
I += 2;
|
|
|
|
if (I == E) {
|
|
|
|
H.HandleIncompleteScanList(start, I - 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-16 10:11:31 +08:00
|
|
|
// Look for a ']' character which denotes the end of the scan list.
|
|
|
|
while (*I != ']') {
|
|
|
|
if (++I == E) {
|
2010-07-17 02:28:03 +08:00
|
|
|
H.HandleIncompleteScanList(start, I - 1);
|
2010-07-16 10:11:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CS.setEndScanList(I);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Much of this is copy-paste from ParsePrintfSpecifier.
|
|
|
|
// We can possibly refactor.
|
|
|
|
static ScanfSpecifierResult ParseScanfSpecifier(FormatStringHandler &H,
|
|
|
|
const char *&Beg,
|
|
|
|
const char *E,
|
2011-12-15 18:25:47 +08:00
|
|
|
unsigned &argIndex,
|
2012-09-13 10:11:03 +08:00
|
|
|
const LangOptions &LO,
|
|
|
|
const TargetInfo &Target) {
|
2016-03-30 01:35:02 +08:00
|
|
|
using namespace clang::analyze_format_string;
|
2010-07-16 10:11:31 +08:00
|
|
|
using namespace clang::analyze_scanf;
|
|
|
|
const char *I = Beg;
|
2014-05-20 12:30:07 +08:00
|
|
|
const char *Start = nullptr;
|
2010-07-16 10:11:31 +08:00
|
|
|
UpdateOnReturn <const char*> UpdateBeg(Beg, I);
|
|
|
|
|
|
|
|
// Look for a '%' character that indicates the start of a format specifier.
|
|
|
|
for ( ; I != E ; ++I) {
|
|
|
|
char c = *I;
|
|
|
|
if (c == '\0') {
|
|
|
|
// Detect spurious null characters, which are likely errors.
|
|
|
|
H.HandleNullChar(I);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (c == '%') {
|
|
|
|
Start = I++; // Record the start of the format specifier.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No format specifier found?
|
|
|
|
if (!Start)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (I == E) {
|
|
|
|
// No more characters left?
|
|
|
|
H.HandleIncompleteSpecifier(Start, E - Start);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScanfSpecifier FS;
|
|
|
|
if (ParseArgPosition(H, FS, Start, I, E))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (I == E) {
|
|
|
|
// No more characters left?
|
|
|
|
H.HandleIncompleteSpecifier(Start, E - Start);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for '*' flag if it is present.
|
|
|
|
if (*I == '*') {
|
|
|
|
FS.setSuppressAssignment(I);
|
|
|
|
if (++I == E) {
|
|
|
|
H.HandleIncompleteSpecifier(Start, E - Start);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the field width (if any). Unlike printf, this is either
|
|
|
|
// a fixed integer or isn't present.
|
|
|
|
const OptionalAmount &Amt = clang::analyze_format_string::ParseAmount(I, E);
|
|
|
|
if (Amt.getHowSpecified() != OptionalAmount::NotSpecified) {
|
|
|
|
assert(Amt.getHowSpecified() == OptionalAmount::Constant);
|
|
|
|
FS.setFieldWidth(Amt);
|
|
|
|
|
|
|
|
if (I == E) {
|
|
|
|
// No more characters left?
|
|
|
|
H.HandleIncompleteSpecifier(Start, E - Start);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the length modifier.
|
2011-12-15 18:25:47 +08:00
|
|
|
if (ParseLengthModifier(FS, I, E, LO, /*scanf=*/true) && I == E) {
|
2010-07-16 10:11:31 +08:00
|
|
|
// No more characters left?
|
|
|
|
H.HandleIncompleteSpecifier(Start, E - Start);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect spurious null characters, which are likely errors.
|
|
|
|
if (*I == '\0') {
|
|
|
|
H.HandleNullChar(I);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, look for the conversion specifier.
|
|
|
|
const char *conversionPosition = I++;
|
2010-07-21 04:04:27 +08:00
|
|
|
ScanfConversionSpecifier::Kind k = ScanfConversionSpecifier::InvalidSpecifier;
|
2010-07-16 10:11:31 +08:00
|
|
|
switch (*conversionPosition) {
|
|
|
|
default:
|
|
|
|
break;
|
2010-07-21 04:04:47 +08:00
|
|
|
case '%': k = ConversionSpecifier::PercentArg; break;
|
|
|
|
case 'A': k = ConversionSpecifier::AArg; break;
|
|
|
|
case 'E': k = ConversionSpecifier::EArg; break;
|
|
|
|
case 'F': k = ConversionSpecifier::FArg; break;
|
|
|
|
case 'G': k = ConversionSpecifier::GArg; break;
|
|
|
|
case 'X': k = ConversionSpecifier::XArg; break;
|
|
|
|
case 'a': k = ConversionSpecifier::aArg; break;
|
|
|
|
case 'd': k = ConversionSpecifier::dArg; break;
|
|
|
|
case 'e': k = ConversionSpecifier::eArg; break;
|
|
|
|
case 'f': k = ConversionSpecifier::fArg; break;
|
|
|
|
case 'g': k = ConversionSpecifier::gArg; break;
|
|
|
|
case 'i': k = ConversionSpecifier::iArg; break;
|
|
|
|
case 'n': k = ConversionSpecifier::nArg; break;
|
|
|
|
case 'c': k = ConversionSpecifier::cArg; break;
|
|
|
|
case 'C': k = ConversionSpecifier::CArg; break;
|
|
|
|
case 'S': k = ConversionSpecifier::SArg; break;
|
|
|
|
case '[': k = ConversionSpecifier::ScanListArg; break;
|
|
|
|
case 'u': k = ConversionSpecifier::uArg; break;
|
|
|
|
case 'x': k = ConversionSpecifier::xArg; break;
|
|
|
|
case 'o': k = ConversionSpecifier::oArg; break;
|
|
|
|
case 's': k = ConversionSpecifier::sArg; break;
|
|
|
|
case 'p': k = ConversionSpecifier::pArg; break;
|
2012-09-13 10:11:03 +08:00
|
|
|
// Apple extensions
|
|
|
|
// Apple-specific
|
|
|
|
case 'D':
|
|
|
|
if (Target.getTriple().isOSDarwin())
|
|
|
|
k = ConversionSpecifier::DArg;
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
if (Target.getTriple().isOSDarwin())
|
|
|
|
k = ConversionSpecifier::OArg;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
if (Target.getTriple().isOSDarwin())
|
|
|
|
k = ConversionSpecifier::UArg;
|
|
|
|
break;
|
2010-07-16 10:11:31 +08:00
|
|
|
}
|
2010-07-21 04:04:27 +08:00
|
|
|
ScanfConversionSpecifier CS(conversionPosition, k);
|
|
|
|
if (k == ScanfConversionSpecifier::ScanListArg) {
|
2012-01-12 22:44:54 +08:00
|
|
|
if (ParseScanList(H, CS, I, E))
|
2010-07-16 10:11:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
FS.setConversionSpecifier(CS);
|
|
|
|
if (CS.consumesDataArgument() && !FS.getSuppressAssignment()
|
|
|
|
&& !FS.usesPositionalArg())
|
|
|
|
FS.setArgIndex(argIndex++);
|
|
|
|
|
|
|
|
// FIXME: '%' and '*' doesn't make sense. Issue a warning.
|
|
|
|
// FIXME: 'ConsumedSoFar' and '*' doesn't make sense.
|
2016-03-30 01:35:02 +08:00
|
|
|
|
2010-07-21 04:04:27 +08:00
|
|
|
if (k == ScanfConversionSpecifier::InvalidSpecifier) {
|
2016-03-30 01:35:02 +08:00
|
|
|
unsigned Len = I - Beg;
|
|
|
|
if (ParseUTF8InvalidSpecifier(Beg, E, Len)) {
|
|
|
|
CS.setEndScanList(Beg + Len);
|
|
|
|
FS.setConversionSpecifier(CS);
|
|
|
|
}
|
2010-07-16 10:11:31 +08:00
|
|
|
// Assume the conversion takes one argument.
|
2016-03-30 01:35:02 +08:00
|
|
|
return !H.HandleInvalidScanfConversionSpecifier(FS, Beg, Len);
|
2010-07-16 10:11:31 +08:00
|
|
|
}
|
|
|
|
return ScanfSpecifierResult(Start, FS);
|
|
|
|
}
|
2011-12-10 21:20:11 +08:00
|
|
|
|
2012-08-07 16:59:46 +08:00
|
|
|
ArgType ScanfSpecifier::getArgType(ASTContext &Ctx) const {
|
2011-12-10 21:20:11 +08:00
|
|
|
const ScanfConversionSpecifier &CS = getConversionSpecifier();
|
|
|
|
|
|
|
|
if (!CS.consumesDataArgument())
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::Invalid();
|
2011-12-10 21:20:11 +08:00
|
|
|
|
|
|
|
switch(CS.getKind()) {
|
|
|
|
// Signed int.
|
|
|
|
case ConversionSpecifier::dArg:
|
2012-09-13 10:11:03 +08:00
|
|
|
case ConversionSpecifier::DArg:
|
2011-12-10 21:20:11 +08:00
|
|
|
case ConversionSpecifier::iArg:
|
|
|
|
switch (LM.getKind()) {
|
2012-08-07 16:59:46 +08:00
|
|
|
case LengthModifier::None:
|
|
|
|
return ArgType::PtrTo(Ctx.IntTy);
|
2011-12-10 21:20:11 +08:00
|
|
|
case LengthModifier::AsChar:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::PtrTo(ArgType::AnyCharTy);
|
|
|
|
case LengthModifier::AsShort:
|
|
|
|
return ArgType::PtrTo(Ctx.ShortTy);
|
|
|
|
case LengthModifier::AsLong:
|
|
|
|
return ArgType::PtrTo(Ctx.LongTy);
|
2012-02-17 00:34:54 +08:00
|
|
|
case LengthModifier::AsLongLong:
|
|
|
|
case LengthModifier::AsQuad:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::PtrTo(Ctx.LongLongTy);
|
2013-08-22 05:54:46 +08:00
|
|
|
case LengthModifier::AsInt64:
|
|
|
|
return ArgType::PtrTo(ArgType(Ctx.LongLongTy, "__int64"));
|
2011-12-10 21:20:11 +08:00
|
|
|
case LengthModifier::AsIntMax:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::PtrTo(ArgType(Ctx.getIntMaxType(), "intmax_t"));
|
2011-12-10 21:20:11 +08:00
|
|
|
case LengthModifier::AsSizeT:
|
2017-07-21 04:11:47 +08:00
|
|
|
return ArgType::PtrTo(ArgType(Ctx.getSignedSizeType(), "ssize_t"));
|
2011-12-10 21:20:11 +08:00
|
|
|
case LengthModifier::AsPtrDiff:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::PtrTo(ArgType(Ctx.getPointerDiffType(), "ptrdiff_t"));
|
2012-01-25 05:29:54 +08:00
|
|
|
case LengthModifier::AsLongDouble:
|
|
|
|
// GNU extension.
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::PtrTo(Ctx.LongLongTy);
|
|
|
|
case LengthModifier::AsAllocate:
|
|
|
|
case LengthModifier::AsMAllocate:
|
2013-08-22 05:54:46 +08:00
|
|
|
case LengthModifier::AsInt32:
|
|
|
|
case LengthModifier::AsInt3264:
|
2014-09-05 05:39:46 +08:00
|
|
|
case LengthModifier::AsWide:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::Invalid();
|
2011-12-10 21:20:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unsigned int.
|
|
|
|
case ConversionSpecifier::oArg:
|
2012-09-13 10:11:03 +08:00
|
|
|
case ConversionSpecifier::OArg:
|
2011-12-10 21:20:11 +08:00
|
|
|
case ConversionSpecifier::uArg:
|
2012-09-13 10:11:03 +08:00
|
|
|
case ConversionSpecifier::UArg:
|
2011-12-10 21:20:11 +08:00
|
|
|
case ConversionSpecifier::xArg:
|
|
|
|
case ConversionSpecifier::XArg:
|
|
|
|
switch (LM.getKind()) {
|
2012-08-07 16:59:46 +08:00
|
|
|
case LengthModifier::None:
|
|
|
|
return ArgType::PtrTo(Ctx.UnsignedIntTy);
|
|
|
|
case LengthModifier::AsChar:
|
|
|
|
return ArgType::PtrTo(Ctx.UnsignedCharTy);
|
|
|
|
case LengthModifier::AsShort:
|
|
|
|
return ArgType::PtrTo(Ctx.UnsignedShortTy);
|
|
|
|
case LengthModifier::AsLong:
|
|
|
|
return ArgType::PtrTo(Ctx.UnsignedLongTy);
|
2011-12-10 21:20:11 +08:00
|
|
|
case LengthModifier::AsLongLong:
|
2012-02-17 00:34:54 +08:00
|
|
|
case LengthModifier::AsQuad:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::PtrTo(Ctx.UnsignedLongLongTy);
|
2013-08-22 05:54:46 +08:00
|
|
|
case LengthModifier::AsInt64:
|
|
|
|
return ArgType::PtrTo(ArgType(Ctx.UnsignedLongLongTy, "unsigned __int64"));
|
2011-12-10 21:20:11 +08:00
|
|
|
case LengthModifier::AsIntMax:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::PtrTo(ArgType(Ctx.getUIntMaxType(), "uintmax_t"));
|
2011-12-10 21:20:11 +08:00
|
|
|
case LengthModifier::AsSizeT:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::PtrTo(ArgType(Ctx.getSizeType(), "size_t"));
|
2011-12-10 21:20:11 +08:00
|
|
|
case LengthModifier::AsPtrDiff:
|
2017-09-29 07:11:31 +08:00
|
|
|
return ArgType::PtrTo(
|
|
|
|
ArgType(Ctx.getUnsignedPointerDiffType(), "unsigned ptrdiff_t"));
|
2012-01-25 05:29:54 +08:00
|
|
|
case LengthModifier::AsLongDouble:
|
|
|
|
// GNU extension.
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::PtrTo(Ctx.UnsignedLongLongTy);
|
|
|
|
case LengthModifier::AsAllocate:
|
|
|
|
case LengthModifier::AsMAllocate:
|
2013-08-22 05:54:46 +08:00
|
|
|
case LengthModifier::AsInt32:
|
|
|
|
case LengthModifier::AsInt3264:
|
2014-09-05 05:39:46 +08:00
|
|
|
case LengthModifier::AsWide:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::Invalid();
|
2011-12-10 21:20:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Float.
|
|
|
|
case ConversionSpecifier::aArg:
|
|
|
|
case ConversionSpecifier::AArg:
|
|
|
|
case ConversionSpecifier::eArg:
|
|
|
|
case ConversionSpecifier::EArg:
|
|
|
|
case ConversionSpecifier::fArg:
|
|
|
|
case ConversionSpecifier::FArg:
|
|
|
|
case ConversionSpecifier::gArg:
|
|
|
|
case ConversionSpecifier::GArg:
|
|
|
|
switch (LM.getKind()) {
|
2012-08-07 16:59:46 +08:00
|
|
|
case LengthModifier::None:
|
|
|
|
return ArgType::PtrTo(Ctx.FloatTy);
|
|
|
|
case LengthModifier::AsLong:
|
|
|
|
return ArgType::PtrTo(Ctx.DoubleTy);
|
2011-12-10 21:20:11 +08:00
|
|
|
case LengthModifier::AsLongDouble:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::PtrTo(Ctx.LongDoubleTy);
|
2011-12-10 21:20:11 +08:00
|
|
|
default:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::Invalid();
|
2011-12-10 21:20:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Char, string and scanlist.
|
|
|
|
case ConversionSpecifier::cArg:
|
|
|
|
case ConversionSpecifier::sArg:
|
|
|
|
case ConversionSpecifier::ScanListArg:
|
|
|
|
switch (LM.getKind()) {
|
2012-08-07 16:59:46 +08:00
|
|
|
case LengthModifier::None:
|
|
|
|
return ArgType::PtrTo(ArgType::AnyCharTy);
|
2011-12-10 21:20:11 +08:00
|
|
|
case LengthModifier::AsLong:
|
2014-09-05 05:39:46 +08:00
|
|
|
case LengthModifier::AsWide:
|
2013-05-10 18:08:40 +08:00
|
|
|
return ArgType::PtrTo(ArgType(Ctx.getWideCharType(), "wchar_t"));
|
2012-01-13 01:11:12 +08:00
|
|
|
case LengthModifier::AsAllocate:
|
|
|
|
case LengthModifier::AsMAllocate:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::PtrTo(ArgType::CStrTy);
|
2014-09-05 05:39:52 +08:00
|
|
|
case LengthModifier::AsShort:
|
|
|
|
if (Ctx.getTargetInfo().getTriple().isOSMSVCRT())
|
|
|
|
return ArgType::PtrTo(ArgType::AnyCharTy);
|
2017-06-03 14:23:51 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2011-12-10 21:20:11 +08:00
|
|
|
default:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::Invalid();
|
2011-12-10 21:20:11 +08:00
|
|
|
}
|
|
|
|
case ConversionSpecifier::CArg:
|
|
|
|
case ConversionSpecifier::SArg:
|
|
|
|
// FIXME: Mac OS X specific?
|
2012-01-13 01:11:12 +08:00
|
|
|
switch (LM.getKind()) {
|
|
|
|
case LengthModifier::None:
|
2014-09-05 05:39:46 +08:00
|
|
|
case LengthModifier::AsWide:
|
2013-05-10 18:08:40 +08:00
|
|
|
return ArgType::PtrTo(ArgType(Ctx.getWideCharType(), "wchar_t"));
|
2012-01-13 01:11:12 +08:00
|
|
|
case LengthModifier::AsAllocate:
|
|
|
|
case LengthModifier::AsMAllocate:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::PtrTo(ArgType(ArgType::WCStrTy, "wchar_t *"));
|
2014-09-05 05:39:52 +08:00
|
|
|
case LengthModifier::AsShort:
|
|
|
|
if (Ctx.getTargetInfo().getTriple().isOSMSVCRT())
|
|
|
|
return ArgType::PtrTo(ArgType::AnyCharTy);
|
2017-06-03 14:23:51 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2012-01-13 01:11:12 +08:00
|
|
|
default:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::Invalid();
|
2012-01-13 01:11:12 +08:00
|
|
|
}
|
2011-12-10 21:20:11 +08:00
|
|
|
|
|
|
|
// Pointer.
|
|
|
|
case ConversionSpecifier::pArg:
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType::PtrTo(ArgType::CPointerTy);
|
2011-12-10 21:20:11 +08:00
|
|
|
|
2012-08-07 16:59:46 +08:00
|
|
|
// Write-back.
|
2012-07-31 01:11:32 +08:00
|
|
|
case ConversionSpecifier::nArg:
|
2012-08-07 17:13:19 +08:00
|
|
|
switch (LM.getKind()) {
|
|
|
|
case LengthModifier::None:
|
|
|
|
return ArgType::PtrTo(Ctx.IntTy);
|
|
|
|
case LengthModifier::AsChar:
|
|
|
|
return ArgType::PtrTo(Ctx.SignedCharTy);
|
|
|
|
case LengthModifier::AsShort:
|
|
|
|
return ArgType::PtrTo(Ctx.ShortTy);
|
|
|
|
case LengthModifier::AsLong:
|
|
|
|
return ArgType::PtrTo(Ctx.LongTy);
|
|
|
|
case LengthModifier::AsLongLong:
|
|
|
|
case LengthModifier::AsQuad:
|
|
|
|
return ArgType::PtrTo(Ctx.LongLongTy);
|
2013-08-22 05:54:46 +08:00
|
|
|
case LengthModifier::AsInt64:
|
|
|
|
return ArgType::PtrTo(ArgType(Ctx.LongLongTy, "__int64"));
|
2012-08-07 17:13:19 +08:00
|
|
|
case LengthModifier::AsIntMax:
|
|
|
|
return ArgType::PtrTo(ArgType(Ctx.getIntMaxType(), "intmax_t"));
|
|
|
|
case LengthModifier::AsSizeT:
|
2017-07-21 04:11:47 +08:00
|
|
|
return ArgType::PtrTo(ArgType(Ctx.getSignedSizeType(), "ssize_t"));
|
2012-08-07 17:13:19 +08:00
|
|
|
case LengthModifier::AsPtrDiff:
|
|
|
|
return ArgType::PtrTo(ArgType(Ctx.getPointerDiffType(), "ptrdiff_t"));
|
|
|
|
case LengthModifier::AsLongDouble:
|
|
|
|
return ArgType(); // FIXME: Is this a known extension?
|
|
|
|
case LengthModifier::AsAllocate:
|
|
|
|
case LengthModifier::AsMAllocate:
|
2013-08-22 05:54:46 +08:00
|
|
|
case LengthModifier::AsInt32:
|
|
|
|
case LengthModifier::AsInt3264:
|
2014-09-05 05:39:46 +08:00
|
|
|
case LengthModifier::AsWide:
|
2012-08-07 17:13:19 +08:00
|
|
|
return ArgType::Invalid();
|
|
|
|
}
|
2012-07-31 01:11:32 +08:00
|
|
|
|
2011-12-10 21:20:11 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-08-07 16:59:46 +08:00
|
|
|
return ArgType();
|
2011-12-10 21:20:11 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 11:32:39 +08:00
|
|
|
bool ScanfSpecifier::fixType(QualType QT, QualType RawQT,
|
|
|
|
const LangOptions &LangOpt,
|
2012-02-15 17:59:46 +08:00
|
|
|
ASTContext &Ctx) {
|
2011-12-10 21:20:11 +08:00
|
|
|
|
2012-07-31 01:11:32 +08:00
|
|
|
// %n is different from other conversion specifiers; don't try to fix it.
|
|
|
|
if (CS.getKind() == ConversionSpecifier::nArg)
|
|
|
|
return false;
|
|
|
|
|
2014-03-20 11:32:39 +08:00
|
|
|
if (!QT->isPointerType())
|
|
|
|
return false;
|
|
|
|
|
2011-12-10 21:20:11 +08:00
|
|
|
QualType PT = QT->getPointeeType();
|
2012-06-05 06:49:02 +08:00
|
|
|
|
|
|
|
// If it's an enum, get its underlying type.
|
2016-08-21 00:51:33 +08:00
|
|
|
if (const EnumType *ETy = PT->getAs<EnumType>()) {
|
|
|
|
// Don't try to fix incomplete enums.
|
|
|
|
if (!ETy->getDecl()->isComplete())
|
|
|
|
return false;
|
2014-03-20 11:32:39 +08:00
|
|
|
PT = ETy->getDecl()->getIntegerType();
|
2016-08-21 00:51:33 +08:00
|
|
|
}
|
2014-03-20 11:32:39 +08:00
|
|
|
|
2011-12-10 21:20:11 +08:00
|
|
|
const BuiltinType *BT = PT->getAs<BuiltinType>();
|
|
|
|
if (!BT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Pointer to a character.
|
|
|
|
if (PT->isAnyCharacterType()) {
|
|
|
|
CS.setKind(ConversionSpecifier::sArg);
|
|
|
|
if (PT->isWideCharType())
|
|
|
|
LM.setKind(LengthModifier::AsWideChar);
|
|
|
|
else
|
|
|
|
LM.setKind(LengthModifier::None);
|
2014-03-20 11:32:39 +08:00
|
|
|
|
|
|
|
// If we know the target array length, we can use it as a field width.
|
|
|
|
if (const ConstantArrayType *CAT = Ctx.getAsConstantArrayType(RawQT)) {
|
|
|
|
if (CAT->getSizeModifier() == ArrayType::Normal)
|
|
|
|
FieldWidth = OptionalAmount(OptionalAmount::Constant,
|
|
|
|
CAT->getSize().getZExtValue() - 1,
|
|
|
|
"", 0, false);
|
|
|
|
|
|
|
|
}
|
2011-12-10 21:20:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out the length modifier.
|
|
|
|
switch (BT->getKind()) {
|
|
|
|
// no modifier
|
|
|
|
case BuiltinType::UInt:
|
|
|
|
case BuiltinType::Int:
|
|
|
|
case BuiltinType::Float:
|
|
|
|
LM.setKind(LengthModifier::None);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// hh
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
LM.setKind(LengthModifier::AsChar);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// h
|
|
|
|
case BuiltinType::Short:
|
|
|
|
case BuiltinType::UShort:
|
|
|
|
LM.setKind(LengthModifier::AsShort);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// l
|
|
|
|
case BuiltinType::Long:
|
|
|
|
case BuiltinType::ULong:
|
|
|
|
case BuiltinType::Double:
|
|
|
|
LM.setKind(LengthModifier::AsLong);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// ll
|
|
|
|
case BuiltinType::LongLong:
|
|
|
|
case BuiltinType::ULongLong:
|
|
|
|
LM.setKind(LengthModifier::AsLongLong);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// L
|
|
|
|
case BuiltinType::LongDouble:
|
|
|
|
LM.setKind(LengthModifier::AsLongDouble);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Don't know.
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle size_t, ptrdiff_t, etc. that have dedicated length modifiers in C99.
|
2013-01-02 19:42:31 +08:00
|
|
|
if (isa<TypedefType>(PT) && (LangOpt.C99 || LangOpt.CPlusPlus11))
|
2012-07-28 03:17:46 +08:00
|
|
|
namedTypeToLengthModifier(PT, LM);
|
2011-12-10 21:20:11 +08:00
|
|
|
|
2012-02-15 17:59:46 +08:00
|
|
|
// If fixing the length modifier was enough, we are done.
|
2012-09-08 12:00:03 +08:00
|
|
|
if (hasValidLengthModifier(Ctx.getTargetInfo())) {
|
|
|
|
const analyze_scanf::ArgType &AT = getArgType(Ctx);
|
|
|
|
if (AT.isValid() && AT.matchesType(Ctx, QT))
|
|
|
|
return true;
|
|
|
|
}
|
2012-02-15 17:59:46 +08:00
|
|
|
|
2011-12-10 21:20:11 +08:00
|
|
|
// Figure out the conversion specifier.
|
|
|
|
if (PT->isRealFloatingType())
|
|
|
|
CS.setKind(ConversionSpecifier::fArg);
|
|
|
|
else if (PT->isSignedIntegerType())
|
|
|
|
CS.setKind(ConversionSpecifier::dArg);
|
2012-02-15 17:59:46 +08:00
|
|
|
else if (PT->isUnsignedIntegerType())
|
|
|
|
CS.setKind(ConversionSpecifier::uArg);
|
|
|
|
else
|
2011-12-10 21:20:11 +08:00
|
|
|
llvm_unreachable("Unexpected type");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanfSpecifier::toString(raw_ostream &os) const {
|
|
|
|
os << "%";
|
|
|
|
|
|
|
|
if (usesPositionalArg())
|
|
|
|
os << getPositionalArgIndex() << "$";
|
|
|
|
if (SuppressAssignment)
|
|
|
|
os << "*";
|
|
|
|
|
|
|
|
FieldWidth.toString(os);
|
|
|
|
os << LM.toString();
|
|
|
|
os << CS.toString();
|
|
|
|
}
|
|
|
|
|
2010-07-16 10:11:31 +08:00
|
|
|
bool clang::analyze_format_string::ParseScanfString(FormatStringHandler &H,
|
|
|
|
const char *I,
|
2011-12-15 18:25:47 +08:00
|
|
|
const char *E,
|
2012-09-13 10:11:03 +08:00
|
|
|
const LangOptions &LO,
|
|
|
|
const TargetInfo &Target) {
|
2010-07-16 10:11:31 +08:00
|
|
|
|
|
|
|
unsigned argIndex = 0;
|
|
|
|
|
|
|
|
// Keep looking for a format specifier until we have exhausted the string.
|
|
|
|
while (I != E) {
|
2011-12-15 18:25:47 +08:00
|
|
|
const ScanfSpecifierResult &FSR = ParseScanfSpecifier(H, I, E, argIndex,
|
2012-09-13 10:11:03 +08:00
|
|
|
LO, Target);
|
2010-07-16 10:11:31 +08:00
|
|
|
// Did a fail-stop error of any kind occur when parsing the specifier?
|
|
|
|
// If so, don't do any more processing.
|
|
|
|
if (FSR.shouldStop())
|
2012-09-11 05:20:09 +08:00
|
|
|
return true;
|
2010-07-16 10:11:31 +08:00
|
|
|
// Did we exhaust the string or encounter an error that
|
|
|
|
// we can recover from?
|
|
|
|
if (!FSR.hasValue())
|
|
|
|
continue;
|
|
|
|
// We have a format specifier. Pass it to the callback.
|
|
|
|
if (!H.HandleScanfSpecifier(FSR.getValue(), FSR.getStart(),
|
|
|
|
I - FSR.getStart())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(I == E && "Format string not exhausted");
|
|
|
|
return false;
|
|
|
|
}
|