Skip to content

Commit

Permalink
Clean up static analyzer warnings.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
milseman committed Dec 12, 2014
1 parent 5271cab commit 3f0e883
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions include/llvm/ADT/BitVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ class BitVector {

// 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));

Expand Down
5 changes: 4 additions & 1 deletion include/llvm/ADT/SmallBitVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,11 @@ class SmallBitVector {
}

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;
Expand Down
5 changes: 4 additions & 1 deletion lib/MC/MCAsmStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/MC/MCObjectStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
8 changes: 4 additions & 4 deletions lib/MC/MCParser/COFFAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions lib/Support/ScaledNumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions lib/Support/raw_ostream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/TableGen/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion utils/TableGen/CodeGenDAGPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") ||
Expand Down
2 changes: 1 addition & 1 deletion utils/TableGen/CodeGenInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
1 change: 1 addition & 0 deletions utils/TableGen/CodeGenRegisters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ void CodeGenRegister::buildObjectGraph(CodeGenRegBank &RegBank) {
}

const std::string &CodeGenRegister::getName() const {
assert(TheDef && "no def");
return TheDef->getName();
}

Expand Down

0 comments on commit 3f0e883

Please sign in to comment.