[PATCH] D51543: [Sema] Fix uninitialized OverloadCandidate::FoundDecl member

Jan Korous via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 31 08:16:29 PDT 2018


jkorous created this revision.
jkorous added reviewers: arphaman, vsapsai, dexonsmith, rsmith.
jkorous added a project: clang.
Herald added subscribers: cfe-commits, mgorny.

It's rather error-prone to leave that member variable uninitialized. The DeclAccessPair seems to be intentionally POD and it seems the make() method is supposed to be called as a constructor.

I take DeclAccessPair interface as given so the only options are to either change OverloadCandidate interface or actually initialize the member variable it to some sane default. I suggest the easiest option.


Repository:
  rC Clang

https://reviews.llvm.org/D51543

Files:
  Sema/CMakeLists.txt
  Sema/OverloadTest.cpp
  clang/Sema/Overload.h


Index: Sema/OverloadTest.cpp
===================================================================
--- /dev/null
+++ Sema/OverloadTest.cpp
@@ -0,0 +1,21 @@
+//=== unittests/Sema/CodeCompleteTest.cpp - Code Complete tests ==============//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Sema/Overload.h"
+#include "gtest/gtest.h"
+
+namespace {
+
+TEST(SemaOverloadTest, FoundDeclIsInitializedInOverloadCandidate) {
+  clang::OverloadCandidate foo;
+  EXPECT_EQ(foo.FoundDecl.getDecl(), nullptr);
+  EXPECT_EQ(foo.FoundDecl.getAccess(), clang::AS_none);
+}
+
+} // namespace
Index: Sema/CMakeLists.txt
===================================================================
--- Sema/CMakeLists.txt
+++ Sema/CMakeLists.txt
@@ -5,6 +5,7 @@
 add_clang_unittest(SemaTests
   ExternalSemaSourceTest.cpp
   CodeCompleteTest.cpp
+  OverloadTest.cpp
   )
 
 target_link_libraries(SemaTests
Index: clang/Sema/Overload.h
===================================================================
--- clang/Sema/Overload.h
+++ clang/Sema/Overload.h
@@ -728,6 +728,11 @@
 
   /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
   struct OverloadCandidate {
+
+    OverloadCandidate()
+    : FoundDecl( DeclAccessPair::make(nullptr, AS_none))
+    { }
+
     /// Function - The actual function that this candidate
     /// represents. When NULL, this is a built-in candidate
     /// (C++ [over.oper]) or a surrogate for a conversion to a


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51543.163535.patch
Type: text/x-patch
Size: 1673 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180831/7c0be0f6/attachment.bin>


More information about the cfe-commits mailing list