From 513ac508c95f7049f54f779d8c82d48d99a00c8b Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Tue, 23 Oct 2018 13:49:37 +0000 Subject: [PATCH] [clang] Fix a null pointer dereference. 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 | 3 +++ test/Index/complete-switch.c | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 test/Index/complete-switch.c diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp index a6799c5c29..4fed9cca5d 100644 --- a/lib/Sema/SemaCodeComplete.cpp +++ b/lib/Sema/SemaCodeComplete.cpp @@ -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 index 0000000000..9a9438c28d --- /dev/null +++ b/test/Index/complete-switch.c @@ -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 -- 2.50.1