From 0da238081a8bbbe55246f3d9fabd688c26de0514 Mon Sep 17 00:00:00 2001 From: Kristof Umann Date: Tue, 14 Aug 2018 08:20:51 +0000 Subject: [PATCH] [analyzer][UninitializedObjectChecker] Void pointers are casted back to their dynamic type in note message Differential Revision: https://reviews.llvm.org/D49228 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@339653 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../UninitializedPointee.cpp | 42 ++++++++++++++++++- .../cxx-uninitialized-object-ptr-ref.cpp | 12 +++--- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/lib/StaticAnalyzer/Checkers/UninitializedObject/UninitializedPointee.cpp b/lib/StaticAnalyzer/Checkers/UninitializedObject/UninitializedPointee.cpp index 6c83b2e146..c855f97de2 100644 --- a/lib/StaticAnalyzer/Checkers/UninitializedObject/UninitializedPointee.cpp +++ b/lib/StaticAnalyzer/Checkers/UninitializedObject/UninitializedPointee.cpp @@ -60,6 +60,32 @@ public: } }; +/// Represents a void* field that needs to be casted back to its dynamic type +/// for a correct note message. +class NeedsCastLocField final : public FieldNode { + QualType CastBackType; + +public: + NeedsCastLocField(const FieldRegion *FR, const QualType &T) + : FieldNode(FR), CastBackType(T) {} + + virtual void printNoteMsg(llvm::raw_ostream &Out) const override { + Out << "uninitialized pointee "; + } + + virtual void printPrefix(llvm::raw_ostream &Out) const override { + Out << "static_cast" << '<' << CastBackType.getAsString() << ">("; + } + + virtual void printNode(llvm::raw_ostream &Out) const override { + Out << getVariableName(getDecl()) << ')'; + } + + virtual void printSeparator(llvm::raw_ostream &Out) const override { + Out << "->"; + } +}; + } // end of anonymous namespace // Utility function declarations. @@ -122,6 +148,10 @@ bool FindUninitializedFields::isPointerOrReferenceUninit( QualType DynT = DynTInfo.getType(); + // If the static type of the field is a void pointer, we need to cast it back + // to the dynamic type before dereferencing. + bool NeedsCastBack = isVoidPointer(FR->getDecl()->getType()); + if (isVoidPointer(DynT)) { IsAnyFieldInitialized = true; return false; @@ -160,11 +190,16 @@ bool FindUninitializedFields::isPointerOrReferenceUninit( const TypedValueRegion *R = RecordV->getRegion(); - if (DynT->getPointeeType()->isStructureOrClassType()) + if (DynT->getPointeeType()->isStructureOrClassType()) { + if (NeedsCastBack) + return isNonUnionUninit(R, LocalChain.add(NeedsCastLocField(FR, DynT))); return isNonUnionUninit(R, LocalChain.add(LocField(FR))); + } if (DynT->getPointeeType()->isUnionType()) { if (isUnionUninit(R)) { + if (NeedsCastBack) + return addFieldToUninits(LocalChain.add(NeedsCastLocField(FR, DynT))); return addFieldToUninits(LocalChain.add(LocField(FR))); } else { IsAnyFieldInitialized = true; @@ -185,8 +220,11 @@ bool FindUninitializedFields::isPointerOrReferenceUninit( "At this point FR must either have a primitive dynamic type, or it " "must be a null, undefined, unknown or concrete pointer!"); - if (isPrimitiveUninit(DerefdV)) + if (isPrimitiveUninit(DerefdV)) { + if (NeedsCastBack) + return addFieldToUninits(LocalChain.add(NeedsCastLocField(FR, DynT))); return addFieldToUninits(LocalChain.add(LocField(FR))); + } IsAnyFieldInitialized = true; return false; diff --git a/test/Analysis/cxx-uninitialized-object-ptr-ref.cpp b/test/Analysis/cxx-uninitialized-object-ptr-ref.cpp index 2d5ebff536..eb7ac2590d 100644 --- a/test/Analysis/cxx-uninitialized-object-ptr-ref.cpp +++ b/test/Analysis/cxx-uninitialized-object-ptr-ref.cpp @@ -292,7 +292,7 @@ void fCyclicVoidPointerTest() { } struct IntDynTypedVoidPointerTest1 { - void *vptr; // expected-note{{uninitialized pointee 'this->vptr'}} + void *vptr; // expected-note{{uninitialized pointee 'static_cast(this->vptr)'}} int dontGetFilteredByNonPedanticMode = 0; IntDynTypedVoidPointerTest1(void *vptr) : vptr(vptr) {} // expected-warning{{1 uninitialized field}} @@ -305,8 +305,8 @@ void fIntDynTypedVoidPointerTest1() { struct RecordDynTypedVoidPointerTest { struct RecordType { - int x; // expected-note{{uninitialized field 'this->vptr->x'}} - int y; // expected-note{{uninitialized field 'this->vptr->y'}} + int x; // expected-note{{uninitialized field 'static_cast(this->vptr)->x'}} + int y; // expected-note{{uninitialized field 'static_cast(this->vptr)->y'}} }; void *vptr; @@ -322,9 +322,9 @@ void fRecordDynTypedVoidPointerTest() { struct NestedNonVoidDynTypedVoidPointerTest { struct RecordType { - int x; // expected-note{{uninitialized field 'this->vptr->x'}} - int y; // expected-note{{uninitialized field 'this->vptr->y'}} - void *vptr; // expected-note{{uninitialized pointee 'this->vptr->vptr'}} + int x; // expected-note{{uninitialized field 'static_cast(this->vptr)->x'}} + int y; // expected-note{{uninitialized field 'static_cast(this->vptr)->y'}} + void *vptr; // expected-note{{uninitialized pointee 'static_cast(static_cast(this->vptr)->vptr)'}} }; void *vptr; -- 2.50.1