[PATCH] D139148: Fix nullptr dereference found by Coverity static analysis tool

Sindhu Chittireddy via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 2 00:43:30 PST 2022


schittir updated this revision to Diff 479541.
schittir added a comment.

Add more nullptr checks per Shafik's comments


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D139148/new/

https://reviews.llvm.org/D139148

Files:
  clang/lib/Sema/SemaInit.cpp


Index: clang/lib/Sema/SemaInit.cpp
===================================================================
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -5962,9 +5962,10 @@
     if (Kind.getKind() == InitializationKind::IK_Direct ||
         (Kind.getKind() == InitializationKind::IK_Copy &&
          (Context.hasSameUnqualifiedType(SourceType, DestType) ||
-          S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, DestType))))
-      TryConstructorInitialization(S, Entity, Kind, Args,
-                                   DestType, DestType, *this);
+          (Initializer &&
+           S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, DestType)))))
+      TryConstructorInitialization(S, Entity, Kind, Args, DestType, DestType,
+                                   *this);
     //     - Otherwise (i.e., for the remaining copy-initialization cases),
     //       user-defined conversion sequences that can convert from the source
     //       type to the destination type or (when a conversion function is
@@ -6027,8 +6028,8 @@
     bool NeedAtomicConversion = false;
     if (const AtomicType *Atomic = DestType->getAs<AtomicType>()) {
       if (Context.hasSameUnqualifiedType(SourceType, Atomic->getValueType()) ||
-          S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType,
-                          Atomic->getValueType())) {
+          (Initializer && S.IsDerivedFrom(Initializer->getBeginLoc(),
+                                          SourceType, Atomic->getValueType())) {
         DestType = Atomic->getValueType();
         NeedAtomicConversion = true;
       }
@@ -6045,7 +6046,7 @@
   //    - Otherwise, if the initialization is direct-initialization, the source
   //    type is std::nullptr_t, and the destination type is bool, the initial
   //    value of the object being initialized is false.
-  if (!SourceType.isNull() && SourceType->isNullPtrType() &&
+  if (!SourceType.isNull() && SourceType->isNullPtrType() && Initializer &&
       DestType->isBooleanType() &&
       Kind.getKind() == InitializationKind::IK_Direct) {
     AddConversionSequenceStep(
@@ -6095,11 +6096,11 @@
     DeclAccessPair dap;
     if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) {
       AddZeroInitializationStep(Entity.getType());
-    } else if (Initializer->getType() == Context.OverloadTy &&
+    } else if (Initializer && Initializer->getType() == Context.OverloadTy &&
                !S.ResolveAddressOfOverloadedFunction(Initializer, DestType,
                                                      false, dap))
       SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
-    else if (Initializer->getType()->isFunctionType() &&
+    else if (Initializer && Initializer->getType()->isFunctionType() &&
              isExprAnUnaddressableFunction(S, Initializer))
       SetFailed(InitializationSequence::FK_AddressOfUnaddressableFunction);
     else


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139148.479541.patch
Type: text/x-patch
Size: 2934 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221202/c93b5223/attachment.bin>


More information about the cfe-commits mailing list