From: Chris Lattner Date: Thu, 23 Aug 2007 14:29:07 +0000 (+0000) Subject: fix a segfault in cases where there are no cases. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f334850cd2fc2d4ac9b585f80d97b4f0097a0348;p=clang fix a segfault in cases where there are no cases. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41317 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Sema/SemaStmt.cpp b/Sema/SemaStmt.cpp index c660b71742..ee3a8e6a7f 100644 --- a/Sema/SemaStmt.cpp +++ b/Sema/SemaStmt.cpp @@ -274,16 +274,18 @@ Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) { // Sort all the scalar case values so we can easily detect duplicates. std::stable_sort(CaseVals.begin(), CaseVals.end()); - for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { - if (CaseVals[i].first == CaseVals[i+1].first) { - // If we have a duplicate, report it. - Diag(CaseVals[i+1].second->getLHS()->getLocStart(), - diag::err_duplicate_case, CaseVals[i].first.toString()); - Diag(CaseVals[i].second->getLHS()->getLocStart(), - diag::err_duplicate_case_prev); - // FIXME: We really want to remove the bogus case stmt from the substmt, - // but we have no way to do this right now. - CaseListIsErroneous = true; + if (!CaseVals.empty()) { + for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { + if (CaseVals[i].first == CaseVals[i+1].first) { + // If we have a duplicate, report it. + Diag(CaseVals[i+1].second->getLHS()->getLocStart(), + diag::err_duplicate_case, CaseVals[i].first.toString()); + Diag(CaseVals[i].second->getLHS()->getLocStart(), + diag::err_duplicate_case_prev); + // FIXME: We really want to remove the bogus case stmt from the substmt, + // but we have no way to do this right now. + CaseListIsErroneous = true; + } } } diff --git a/test/Sema/switch.c b/test/Sema/switch.c index ce195b175f..a55831d462 100644 --- a/test/Sema/switch.c +++ b/test/Sema/switch.c @@ -15,3 +15,7 @@ void foo(int X) { } } +void test3(void) { + switch (0); +} +