From: Steve Naroff Date: Tue, 3 Mar 2009 00:45:38 +0000 (+0000) Subject: Fix crash on invalid X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c97fb9a394ce2cc5e664fcb472e93553528378ad;p=clang Fix crash on invalid git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@65909 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 39b211f64b..e7dff6d5b3 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -785,9 +785,11 @@ Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) { QualType FnRetType; if (FunctionDecl *FD = getCurFunctionDecl()) FnRetType = FD->getResultType(); - else - FnRetType = getCurMethodDecl()->getResultType(); - + else if (ObjCMethodDecl *MD = getCurMethodDecl()) + FnRetType = MD->getResultType(); + else // If we don't have a function/method context, bail. + return StmtError(); + if (FnRetType->isVoidType()) { if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns) unsigned D = diag::ext_return_has_expr; diff --git a/test/SemaObjC/method-no-context.m b/test/SemaObjC/method-no-context.m new file mode 100644 index 0000000000..484322e29e --- /dev/null +++ b/test/SemaObjC/method-no-context.m @@ -0,0 +1,4 @@ +// RUN: clang -fsyntax-only -verify %s + +- im0 { int a; return 0; // expected-error{{missing context for method declaration}} +// expected-error{{expected '}'}}