2010-01-28 07:43:25 +08:00
|
|
|
//= PrintfFormatStrings.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 printf and friends. The structure of format
|
|
|
|
// strings for fprintf() are described in C99 7.19.6.1.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/Analyses/PrintfFormatString.h"
|
2010-01-30 06:59:32 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2010-01-28 07:43:25 +08:00
|
|
|
|
2010-01-29 10:13:53 +08:00
|
|
|
using clang::analyze_printf::FormatSpecifier;
|
|
|
|
using clang::analyze_printf::OptionalAmount;
|
2010-01-30 06:59:32 +08:00
|
|
|
using clang::analyze_printf::ArgTypeResult;
|
2010-01-29 11:16:21 +08:00
|
|
|
using namespace clang;
|
2010-01-28 07:43:25 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class FormatSpecifierResult {
|
|
|
|
FormatSpecifier FS;
|
|
|
|
const char *Start;
|
2010-01-29 10:40:24 +08:00
|
|
|
bool Stop;
|
2010-01-28 07:43:25 +08:00
|
|
|
public:
|
2010-01-29 10:40:24 +08:00
|
|
|
FormatSpecifierResult(bool stop = false)
|
|
|
|
: Start(0), Stop(stop) {}
|
2010-01-28 07:43:25 +08:00
|
|
|
FormatSpecifierResult(const char *start,
|
2010-01-28 10:02:59 +08:00
|
|
|
const FormatSpecifier &fs)
|
2010-01-29 10:40:24 +08:00
|
|
|
: FS(fs), Start(start), Stop(false) {}
|
2010-01-28 07:43:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
const char *getStart() const { return Start; }
|
2010-01-29 10:40:24 +08:00
|
|
|
bool shouldStop() const { return Stop; }
|
2010-01-28 07:43:25 +08:00
|
|
|
bool hasValue() const { return Start != 0; }
|
|
|
|
const FormatSpecifier &getValue() const {
|
|
|
|
assert(hasValue());
|
|
|
|
return FS;
|
|
|
|
}
|
2010-01-28 10:02:59 +08:00
|
|
|
const FormatSpecifier &getValue() { return FS; }
|
2010-01-28 07:43:25 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class UpdateOnReturn {
|
|
|
|
T &ValueToUpdate;
|
|
|
|
const T &ValueToCopy;
|
|
|
|
public:
|
|
|
|
UpdateOnReturn(T &valueToUpdate, const T &valueToCopy)
|
|
|
|
: ValueToUpdate(valueToUpdate), ValueToCopy(valueToCopy) {}
|
|
|
|
|
|
|
|
~UpdateOnReturn() {
|
|
|
|
ValueToUpdate = ValueToCopy;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static OptionalAmount ParseAmount(const char *&Beg, const char *E) {
|
|
|
|
const char *I = Beg;
|
|
|
|
UpdateOnReturn <const char*> UpdateBeg(Beg, I);
|
|
|
|
|
|
|
|
bool foundDigits = false;
|
|
|
|
unsigned accumulator = 0;
|
|
|
|
|
|
|
|
for ( ; I != E; ++I) {
|
|
|
|
char c = *I;
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
foundDigits = true;
|
|
|
|
accumulator += (accumulator * 10) + (c - '0');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundDigits)
|
2010-01-29 09:06:55 +08:00
|
|
|
return OptionalAmount(accumulator, Beg);
|
2010-01-28 07:43:25 +08:00
|
|
|
|
2010-01-29 09:06:55 +08:00
|
|
|
if (c == '*') {
|
|
|
|
++I;
|
|
|
|
return OptionalAmount(OptionalAmount::Arg, Beg);
|
|
|
|
}
|
2010-01-28 07:43:25 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OptionalAmount();
|
|
|
|
}
|
|
|
|
|
2010-01-29 11:16:21 +08:00
|
|
|
static FormatSpecifierResult ParseFormatSpecifier(FormatStringHandler &H,
|
|
|
|
const char *&Beg, const char *E) {
|
2010-01-29 10:13:53 +08:00
|
|
|
|
|
|
|
using namespace clang::analyze_printf;
|
2010-01-28 07:43:25 +08:00
|
|
|
|
|
|
|
const char *I = Beg;
|
2010-01-28 08:02:05 +08:00
|
|
|
const char *Start = 0;
|
2010-01-28 07:43:25 +08:00
|
|
|
UpdateOnReturn <const char*> UpdateBeg(Beg, I);
|
|
|
|
|
|
|
|
// Look for a '%' character that indicates the start of a format specifier.
|
2010-01-29 07:56:52 +08:00
|
|
|
for ( ; I != E ; ++I) {
|
2010-01-28 07:43:25 +08:00
|
|
|
char c = *I;
|
|
|
|
if (c == '\0') {
|
|
|
|
// Detect spurious null characters, which are likely errors.
|
|
|
|
H.HandleNullChar(I);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (c == '%') {
|
2010-01-29 07:56:52 +08:00
|
|
|
Start = I++; // Record the start of the format specifier.
|
2010-01-28 07:43:25 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No format specifier found?
|
|
|
|
if (!Start)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (I == E) {
|
|
|
|
// No more characters left?
|
2010-01-29 11:16:21 +08:00
|
|
|
H.HandleIncompleteFormatSpecifier(Start, E - Start);
|
2010-01-28 07:43:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FormatSpecifier FS;
|
|
|
|
|
|
|
|
// Look for flags (if any).
|
|
|
|
bool hasMore = true;
|
|
|
|
for ( ; I != E; ++I) {
|
|
|
|
switch (*I) {
|
|
|
|
default: hasMore = false; break;
|
|
|
|
case '-': FS.setIsLeftJustified(); break;
|
|
|
|
case '+': FS.setHasPlusPrefix(); break;
|
|
|
|
case ' ': FS.setHasSpacePrefix(); break;
|
|
|
|
case '#': FS.setHasAlternativeForm(); break;
|
|
|
|
case '0': FS.setHasLeadingZeros(); break;
|
|
|
|
}
|
|
|
|
if (!hasMore)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I == E) {
|
|
|
|
// No more characters left?
|
2010-01-29 11:16:21 +08:00
|
|
|
H.HandleIncompleteFormatSpecifier(Start, E - Start);
|
2010-01-28 07:43:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the field width (if any).
|
|
|
|
FS.setFieldWidth(ParseAmount(I, E));
|
|
|
|
|
|
|
|
if (I == E) {
|
|
|
|
// No more characters left?
|
2010-01-29 11:16:21 +08:00
|
|
|
H.HandleIncompleteFormatSpecifier(Start, E - Start);
|
2010-01-28 07:43:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the precision (if any).
|
|
|
|
if (*I == '.') {
|
2010-01-29 11:16:21 +08:00
|
|
|
++I;
|
2010-01-28 07:43:25 +08:00
|
|
|
if (I == E) {
|
2010-01-29 11:16:21 +08:00
|
|
|
H.HandleIncompleteFormatSpecifier(Start, E - Start);
|
2010-01-28 07:43:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FS.setPrecision(ParseAmount(I, E));
|
|
|
|
|
|
|
|
if (I == E) {
|
|
|
|
// No more characters left?
|
2010-01-29 11:16:21 +08:00
|
|
|
H.HandleIncompleteFormatSpecifier(Start, E - Start);
|
2010-01-28 07:43:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the length modifier.
|
|
|
|
LengthModifier lm = None;
|
|
|
|
switch (*I) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
++I;
|
|
|
|
lm = (I != E && *I == 'h') ? ++I, AsChar : AsShort;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
++I;
|
|
|
|
lm = (I != E && *I == 'l') ? ++I, AsLongLong : AsLong;
|
|
|
|
break;
|
|
|
|
case 'j': lm = AsIntMax; ++I; break;
|
|
|
|
case 'z': lm = AsSizeT; ++I; break;
|
|
|
|
case 't': lm = AsPtrDiff; ++I; break;
|
|
|
|
case 'L': lm = AsLongDouble; ++I; break;
|
2010-01-30 23:49:20 +08:00
|
|
|
case 'q': lm = AsLongLong; ++I; break;
|
2010-01-28 07:43:25 +08:00
|
|
|
}
|
|
|
|
FS.setLengthModifier(lm);
|
|
|
|
|
|
|
|
if (I == E) {
|
|
|
|
// No more characters left?
|
2010-01-29 11:16:21 +08:00
|
|
|
H.HandleIncompleteFormatSpecifier(Start, E - Start);
|
2010-01-28 07:43:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
2010-01-30 04:29:53 +08:00
|
|
|
|
|
|
|
if (*I == '\0') {
|
|
|
|
// Detect spurious null characters, which are likely errors.
|
|
|
|
H.HandleNullChar(I);
|
|
|
|
return true;
|
|
|
|
}
|
2010-01-28 07:43:25 +08:00
|
|
|
|
|
|
|
// Finally, look for the conversion specifier.
|
2010-01-28 10:46:17 +08:00
|
|
|
const char *conversionPosition = I++;
|
2010-01-29 10:40:24 +08:00
|
|
|
ConversionSpecifier::Kind k = ConversionSpecifier::InvalidSpecifier;
|
2010-01-28 10:46:17 +08:00
|
|
|
switch (*conversionPosition) {
|
2010-01-28 07:43:25 +08:00
|
|
|
default:
|
2010-01-29 10:40:24 +08:00
|
|
|
break;
|
2010-01-28 08:55:28 +08:00
|
|
|
// C99: 7.19.6.1 (section 8).
|
2010-01-28 10:46:17 +08:00
|
|
|
case 'd': k = ConversionSpecifier::dArg; break;
|
|
|
|
case 'i': k = ConversionSpecifier::iArg; break;
|
|
|
|
case 'o': k = ConversionSpecifier::oArg; break;
|
|
|
|
case 'u': k = ConversionSpecifier::uArg; break;
|
|
|
|
case 'x': k = ConversionSpecifier::xArg; break;
|
|
|
|
case 'X': k = ConversionSpecifier::XArg; break;
|
|
|
|
case 'f': k = ConversionSpecifier::fArg; break;
|
|
|
|
case 'F': k = ConversionSpecifier::FArg; break;
|
|
|
|
case 'e': k = ConversionSpecifier::eArg; break;
|
|
|
|
case 'E': k = ConversionSpecifier::EArg; break;
|
|
|
|
case 'g': k = ConversionSpecifier::gArg; break;
|
|
|
|
case 'G': k = ConversionSpecifier::GArg; break;
|
|
|
|
case 'a': k = ConversionSpecifier::aArg; break;
|
|
|
|
case 'A': k = ConversionSpecifier::AArg; break;
|
|
|
|
case 'c': k = ConversionSpecifier::IntAsCharArg; break;
|
|
|
|
case 's': k = ConversionSpecifier::CStrArg; break;
|
|
|
|
case 'p': k = ConversionSpecifier::VoidPtrArg; break;
|
|
|
|
case 'n': k = ConversionSpecifier::OutIntPtrArg; break;
|
|
|
|
case '%': k = ConversionSpecifier::PercentArg; break;
|
2010-01-28 08:55:28 +08:00
|
|
|
// Objective-C.
|
2010-01-30 04:29:53 +08:00
|
|
|
case '@': k = ConversionSpecifier::ObjCObjArg; break;
|
|
|
|
// Glibc specific.
|
|
|
|
case 'm': k = ConversionSpecifier::PrintErrno; break;
|
2010-01-28 07:43:25 +08:00
|
|
|
}
|
2010-01-28 10:46:17 +08:00
|
|
|
FS.setConversionSpecifier(ConversionSpecifier(conversionPosition, k));
|
2010-01-29 10:40:24 +08:00
|
|
|
|
|
|
|
if (k == ConversionSpecifier::InvalidSpecifier) {
|
|
|
|
H.HandleInvalidConversionSpecifier(FS, Beg, I - Beg);
|
|
|
|
return false; // Keep processing format specifiers.
|
|
|
|
}
|
2010-01-28 07:43:25 +08:00
|
|
|
return FormatSpecifierResult(Start, FS);
|
|
|
|
}
|
|
|
|
|
2010-01-29 11:16:21 +08:00
|
|
|
bool clang::ParseFormatString(FormatStringHandler &H,
|
2010-01-28 10:46:17 +08:00
|
|
|
const char *I, const char *E) {
|
2010-01-28 07:43:25 +08:00
|
|
|
// Keep looking for a format specifier until we have exhausted the string.
|
|
|
|
while (I != E) {
|
|
|
|
const FormatSpecifierResult &FSR = ParseFormatSpecifier(H, I, E);
|
2010-01-29 10:40:24 +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())
|
2010-01-28 07:43:25 +08:00
|
|
|
return true;;
|
2010-01-29 10:40:24 +08:00
|
|
|
// Did we exhaust the string or encounter an error that
|
|
|
|
// we can recover from?
|
2010-01-28 07:43:25 +08:00
|
|
|
if (!FSR.hasValue())
|
2010-01-29 10:40:24 +08:00
|
|
|
continue;
|
2010-01-28 07:43:25 +08:00
|
|
|
// We have a format specifier. Pass it to the callback.
|
2010-01-28 10:46:17 +08:00
|
|
|
if (!H.HandleFormatSpecifier(FSR.getValue(), FSR.getStart(),
|
|
|
|
I - FSR.getStart()))
|
2010-01-30 04:29:53 +08:00
|
|
|
return true;
|
2010-01-28 07:43:25 +08:00
|
|
|
}
|
|
|
|
assert(I == E && "Format string not exhausted");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FormatStringHandler::~FormatStringHandler() {}
|
2010-01-30 06:59:32 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods on FormatSpecifier.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
ArgTypeResult FormatSpecifier::getArgType(ASTContext &Ctx) const {
|
|
|
|
if (!CS.consumesDataArgument())
|
|
|
|
return ArgTypeResult::Invalid();
|
|
|
|
|
|
|
|
if (CS.isIntArg())
|
|
|
|
switch (LM) {
|
|
|
|
case AsLongDouble:
|
|
|
|
return ArgTypeResult::Invalid();
|
|
|
|
case None: return Ctx.IntTy;
|
|
|
|
case AsChar: return Ctx.SignedCharTy;
|
|
|
|
case AsShort: return Ctx.ShortTy;
|
|
|
|
case AsLong: return Ctx.LongTy;
|
|
|
|
case AsLongLong: return Ctx.LongLongTy;
|
|
|
|
case AsIntMax:
|
|
|
|
// FIXME: Return unknown for now.
|
|
|
|
return ArgTypeResult();
|
|
|
|
case AsSizeT: return Ctx.getSizeType();
|
|
|
|
case AsPtrDiff: return Ctx.getPointerDiffType();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CS.isUIntArg())
|
|
|
|
switch (LM) {
|
|
|
|
case AsLongDouble:
|
|
|
|
return ArgTypeResult::Invalid();
|
|
|
|
case None: return Ctx.UnsignedIntTy;
|
|
|
|
case AsChar: return Ctx.UnsignedCharTy;
|
|
|
|
case AsShort: return Ctx.UnsignedShortTy;
|
|
|
|
case AsLong: return Ctx.UnsignedLongTy;
|
|
|
|
case AsLongLong: return Ctx.UnsignedLongLongTy;
|
|
|
|
case AsIntMax:
|
|
|
|
// FIXME: Return unknown for now.
|
|
|
|
return ArgTypeResult();
|
|
|
|
case AsSizeT:
|
|
|
|
// FIXME: How to get the corresponding unsigned
|
|
|
|
// version of size_t?
|
|
|
|
return ArgTypeResult();
|
|
|
|
case AsPtrDiff:
|
|
|
|
// FIXME: How to get the corresponding unsigned
|
|
|
|
// version of ptrdiff_t?
|
|
|
|
return ArgTypeResult();
|
|
|
|
}
|
2010-01-30 09:02:18 +08:00
|
|
|
|
|
|
|
if (CS.isDoubleArg())
|
|
|
|
return Ctx.DoubleTy;
|
2010-01-30 06:59:32 +08:00
|
|
|
|
|
|
|
// FIXME: Handle other cases.
|
2010-01-30 07:00:35 +08:00
|
|
|
return ArgTypeResult();
|
2010-01-30 06:59:32 +08:00
|
|
|
}
|
|
|
|
|