Clean up static analyzer warnings.

Clang's static analyzer found several potential cases of undefined
behavior, use of un-initialized values, and potentially null pointer
dereferences in tablegen, Support, MC, and ADT. This cleans them up
with specific assertions on the assumptions of the code.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224154 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Michael Ilseman 2014-12-12 21:48:03 +00:00
parent 5271cab7c9
commit 3f0e8837be
11 changed files with 24 additions and 12 deletions

View File

@ -450,6 +450,7 @@ public:
// Grow the bitvector to have enough elements.
Capacity = RHSWords;
assert(Capacity > 0 && "negative capacity?");
BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord));

View File

@ -292,8 +292,11 @@ public:
}
SmallBitVector &set(unsigned Idx) {
if (isSmall())
if (isSmall()) {
assert(Idx <= std::numeric_limits<uintptr_t>::digits &&
"undefined behavior");
setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
}
else
getPointer()->set(Idx);
return *this;

View File

@ -682,7 +682,10 @@ void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
// We truncate our partial emission to fit within the bounds of the
// emission domain. This produces nicer output and silences potential
// truncation warnings when round tripping through another assembler.
ValueToEmit &= ~0ULL >> (64 - EmissionSize * 8);
uint64_t Shift = 64 - EmissionSize * 8;
assert(Shift < std::numeric_limits<unsigned long long>::digits &&
"undefined behavior");
ValueToEmit &= ~0ULL >> Shift;
EmitIntValue(ValueToEmit, EmissionSize);
Emitted += EmissionSize;
}

View File

@ -405,7 +405,9 @@ void MCObjectStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) {
}
void MCObjectStreamer::EmitZeros(uint64_t NumBytes) {
unsigned ItemSize = getCurrentSection().first->isVirtualSection() ? 0 : 1;
const MCSection *Sec = getCurrentSection().first;
assert(Sec && "need a section");
unsigned ItemSize = Sec->isVirtualSection() ? 0 : 1;
insert(new MCFillFragment(0, ItemSize, NumBytes));
}

View File

@ -582,7 +582,7 @@ bool COFFAsmParser::ParseSEHDirectiveHandlerData(StringRef, SMLoc) {
}
bool COFFAsmParser::ParseSEHDirectivePushReg(StringRef, SMLoc L) {
unsigned Reg;
unsigned Reg = 0;
if (ParseSEHRegisterNumber(Reg))
return true;
@ -595,7 +595,7 @@ bool COFFAsmParser::ParseSEHDirectivePushReg(StringRef, SMLoc L) {
}
bool COFFAsmParser::ParseSEHDirectiveSetFrame(StringRef, SMLoc L) {
unsigned Reg;
unsigned Reg = 0;
int64_t Off;
if (ParseSEHRegisterNumber(Reg))
return true;
@ -636,7 +636,7 @@ bool COFFAsmParser::ParseSEHDirectiveAllocStack(StringRef, SMLoc) {
}
bool COFFAsmParser::ParseSEHDirectiveSaveReg(StringRef, SMLoc L) {
unsigned Reg;
unsigned Reg = 0;
int64_t Off;
if (ParseSEHRegisterNumber(Reg))
return true;
@ -663,7 +663,7 @@ bool COFFAsmParser::ParseSEHDirectiveSaveReg(StringRef, SMLoc L) {
// FIXME: This method is inherently x86-specific. It should really be in the
// x86 backend.
bool COFFAsmParser::ParseSEHDirectiveSaveXMM(StringRef, SMLoc L) {
unsigned Reg;
unsigned Reg = 0;
int64_t Off;
if (ParseSEHRegisterNumber(Reg))
return true;

View File

@ -169,8 +169,7 @@ static std::string toStringAPFloat(uint64_t D, int E, unsigned Precision) {
int Shift = 63 - (NewE - E);
assert(Shift <= LeadingZeros);
assert(Shift == LeadingZeros || NewE == ScaledNumbers::MaxScale);
assert((Shift & (1u << std::numeric_limits<int>::digits)) == 0 &&
"undefined behavior");
assert(Shift >= 0 && Shift < 64 && "undefined behavior");
D <<= Shift;
E = NewE;

View File

@ -312,6 +312,7 @@ raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
// than the buffer. Directly write the chunk that is a multiple of the
// preferred buffer size and put the remainder in the buffer.
if (LLVM_UNLIKELY(OutBufCur == OutBufStart)) {
assert(NumBytes != 0 && "undefined behavior");
size_t BytesToWrite = Size - (Size % NumBytes);
write_impl(Ptr, BytesToWrite);
size_t BytesRemaining = Size - BytesToWrite;

View File

@ -812,7 +812,7 @@ Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
return VarInit::get(MCName, RV->getType());
}
}
assert(CurRec && "NULL pointer");
if (Record *D = (CurRec->getRecords()).getDef(Name))
return DefInit::get(D);

View File

@ -2569,8 +2569,10 @@ FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
I->error("set destination should be a register!");
DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue());
if (!Val)
if (!Val) {
I->error("set destination should be a register!");
continue;
}
if (Val->getDef()->isSubClassOf("RegisterClass") ||
Val->getDef()->isSubClassOf("ValueType") ||

View File

@ -537,7 +537,7 @@ bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
// If both are Operands with the same MVT, allow the conversion. It's
// up to the user to make sure the values are appropriate, just like
// for isel Pat's.
if (InstOpRec->isSubClassOf("Operand") &&
if (InstOpRec->isSubClassOf("Operand") && ADI &&
ADI->getDef()->isSubClassOf("Operand")) {
// FIXME: What other attributes should we check here? Identical
// MIOperandInfo perhaps?

View File

@ -146,6 +146,7 @@ void CodeGenRegister::buildObjectGraph(CodeGenRegBank &RegBank) {
}
const std::string &CodeGenRegister::getName() const {
assert(TheDef && "no def");
return TheDef->getName();
}