[cfe-dev] uninitialized variable

McDowell, Raymond C. via cfe-dev cfe-dev at lists.llvm.org
Thu Sep 15 11:47:55 PDT 2016


I am writing a checker to check that a variable is given a value before it is accessed.  So I created a checkLocation call-back:
void checkLocation(SVal L, bool IsLoad, const Stmt* S, CheckerContext &ChCtx) const {
    if (!IsLoad) return;  // location value is not being accessed

    const MemRegion* MemReg = L.getAsRegion();
    if (!MemReg) return;  // L is not a memory location

    const VarRegion* VarReg = MemReg->getAs<VarRegion>();
    if (!VarReg) return;  // L is not a variable location

    SVal VarVal = ChCtx.getState()->getSVal(VarReg);
    if (VarVal.isUndef()) {
        // Variable value is undefined; report error
        EmitReport(S->getSourceRange(), ChCtx, "Variable may not have been assigned a value");
    }
}

This works fine for scalar variables like ints, but I've discovered that a struct variable will be defined even if it hasn't been given a value.  So my plan is to check the struct fields to see if they've been given values.

My question is:  are there other cases where VarVal.isUndef() will return false even though the variable has not been given a value?

Ray
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160915/72cd5d90/attachment.html>


More information about the cfe-dev mailing list