summaryrefslogtreecommitdiff
path: root/lib/Analysis/ScanfFormatString.cpp
diff options
context:
space:
mode:
authorJordan Rose <jordan_rose@apple.com>2014-03-20 03:32:39 +0000
committerJordan Rose <jordan_rose@apple.com>2014-03-20 03:32:39 +0000
commit863a5e8b570d4ae0cfe22bf985a89ac33a508593 (patch)
tree6a3289520a4fd34dbb45b92668d1f62065154b4d /lib/Analysis/ScanfFormatString.cpp
parentfa2fda07c1d03cc695a93faae652c09eb464fc1d (diff)
downloadclang-863a5e8b570d4ae0cfe22bf985a89ac33a508593.tar.gz
clang-863a5e8b570d4ae0cfe22bf985a89ac33a508593.tar.bz2
clang-863a5e8b570d4ae0cfe22bf985a89ac33a508593.tar.xz
scanf format checking: include the buffer length in the fix-it for %s.
Patch by Zach Davis! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204300 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis/ScanfFormatString.cpp')
-rw-r--r--lib/Analysis/ScanfFormatString.cpp23
1 files changed, 17 insertions, 6 deletions
diff --git a/lib/Analysis/ScanfFormatString.cpp b/lib/Analysis/ScanfFormatString.cpp
index f5ce84fe36..3ff7f0ad2e 100644
--- a/lib/Analysis/ScanfFormatString.cpp
+++ b/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;
}