]> granicus.if.org Git - clang/commitdiff
[clang] Fix a null pointer dereference.
authorKadir Cetinkaya <kadircet@google.com>
Tue, 23 Oct 2018 13:49:37 +0000 (13:49 +0000)
committerKadir Cetinkaya <kadircet@google.com>
Tue, 23 Oct 2018 13:49:37 +0000 (13:49 +0000)
Summary:
Sometimes expression inside switch statement can be invalid, for
example type might be incomplete. In those cases code were causing a null
pointer dereference. This patch fixes that.

Reviewers: sammccall, ioeric, hokein

Reviewed By: sammccall

Subscribers: arphaman, cfe-commits

Differential Revision: https://reviews.llvm.org/D53561

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@345029 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaCodeComplete.cpp
test/Index/complete-switch.c [new file with mode: 0644]

index a6799c5c297a2f444ea1a07f859a2c280213a10b..4fed9cca5d1be293dab77c2b5b9bae9a7075bbb4 100644 (file)
@@ -4419,6 +4419,9 @@ void Sema::CodeCompleteCase(Scope *S) {
     return;
 
   SwitchStmt *Switch = getCurFunction()->SwitchStack.back().getPointer();
+  // Condition expression might be invalid, do not continue in this case.
+  if (!Switch->getCond())
+    return;
   QualType type = Switch->getCond()->IgnoreImplicit()->getType();
   if (!type->isEnumeralType()) {
     CodeCompleteExpressionData Data(type);
diff --git a/test/Index/complete-switch.c b/test/Index/complete-switch.c
new file mode 100644 (file)
index 0000000..9a9438c
--- /dev/null
@@ -0,0 +1,10 @@
+void f() {
+  auto foo = bar;
+  switch(foo) {
+    case x:
+      break;
+  }
+}
+
+// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:4:10 %s | FileCheck %s -allow-empty
+// CHECK-NOT: COMPLETION: foo