diff options
author | Jordan Rose <jordan_rose@apple.com> | 2014-03-20 03:32:39 +0000 |
---|---|---|
committer | Jordan Rose <jordan_rose@apple.com> | 2014-03-20 03:32:39 +0000 |
commit | 177b0a3600d8d3eaf6ccbf573d0bf053c4b24191 (patch) | |
tree | dcb3ee4883bf4c0e8d855f7664a9314107039b94 /clang/lib/Analysis/ScanfFormatString.cpp | |
parent | da0de8a237de508c99f06e93928301e0d2c0f24b (diff) | |
download | llvm-177b0a3600d8d3eaf6ccbf573d0bf053c4b24191.zip llvm-177b0a3600d8d3eaf6ccbf573d0bf053c4b24191.tar.gz llvm-177b0a3600d8d3eaf6ccbf573d0bf053c4b24191.tar.bz2 |
scanf format checking: include the buffer length in the fix-it for %s.
Patch by Zach Davis!
llvm-svn: 204300
Diffstat (limited to 'clang/lib/Analysis/ScanfFormatString.cpp')
-rw-r--r-- | clang/lib/Analysis/ScanfFormatString.cpp | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/clang/lib/Analysis/ScanfFormatString.cpp b/clang/lib/Analysis/ScanfFormatString.cpp index f5ce84f..3ff7f0a 100644 --- a/clang/lib/Analysis/ScanfFormatString.cpp +++ b/clang/lib/Analysis/ScanfFormatString.cpp @@ -379,21 +379,23 @@ ArgType ScanfSpecifier::getArgType(ASTContext &Ctx) const { return ArgType(); } -bool ScanfSpecifier::fixType(QualType QT, const LangOptions &LangOpt, +bool ScanfSpecifier::fixType(QualType QT, QualType RawQT, + const LangOptions &LangOpt, ASTContext &Ctx) { - if (!QT->isPointerType()) - return false; // %n is different from other conversion specifiers; don't try to fix it. if (CS.getKind() == ConversionSpecifier::nArg) return false; + if (!QT->isPointerType()) + return false; + QualType PT = QT->getPointeeType(); // If it's an enum, get its underlying type. - if (const EnumType *ETy = QT->getAs<EnumType>()) - QT = ETy->getDecl()->getIntegerType(); - + if (const EnumType *ETy = PT->getAs<EnumType>()) + PT = ETy->getDecl()->getIntegerType(); + const BuiltinType *BT = PT->getAs<BuiltinType>(); if (!BT) return false; @@ -405,6 +407,15 @@ bool ScanfSpecifier::fixType(QualType QT, const LangOptions &LangOpt, LM.setKind(LengthModifier::AsWideChar); else LM.setKind(LengthModifier::None); + + // 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); + + } return true; } |