From 17fa2487df460a27d35c53d5a5620119e36ae67b Mon Sep 17 00:00:00 2001
From: WenHan Gu <Wenhan.gu@mediatek.com>
Date: Mon, 17 Jun 2013 20:47:59 +0800
Subject: [PATCH] [Bugfix] De-reference pointer after null check.

Not all FunctionDecl has this valid pointer. For example, functions in
builtin lists doesn't has valid one. if we has only one line:

  "void vfprintf() {}"

in source, we will get Segmentation fault since this null-pointer dereference.

test.c:1:6: warning: incompatible redeclaration of library function 'vfprintf' [-Wincompatible-library-redeclaration]
void vfprintf(void) {}
     ^
test.c:1:6: note: 'vfprintf' is a builtin with type 'int ()'
0  clang           0x0000000002454ef2 llvm::sys::PrintStackTrace(_IO_FILE*) + 34
...
---
 lib/Sema/SemaDecl.cpp |   15 ++++++++-------
 1 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 92e0042..fd6747e 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -8708,13 +8708,14 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D) {
     if (PossibleZeroParamPrototype) {
       // We found a declaration that is not a prototype, 
       // but that could be a zero-parameter prototype
-      TypeSourceInfo* TI = PossibleZeroParamPrototype->getTypeSourceInfo();
-      TypeLoc TL = TI->getTypeLoc();
-      if (FunctionNoProtoTypeLoc FTL = TL.getAs<FunctionNoProtoTypeLoc>())
-        Diag(PossibleZeroParamPrototype->getLocation(), 
-             diag::note_declaration_not_a_prototype)
-          << PossibleZeroParamPrototype 
-          << FixItHint::CreateInsertion(FTL.getRParenLoc(), "void");
+      if (TypeSourceInfo* TI = PossibleZeroParamPrototype->getTypeSourceInfo()) {
+        TypeLoc TL = TI->getTypeLoc();
+        if (FunctionNoProtoTypeLoc FTL = TL.getAs<FunctionNoProtoTypeLoc>())
+          Diag(PossibleZeroParamPrototype->getLocation(),
+               diag::note_declaration_not_a_prototype)
+            << PossibleZeroParamPrototype
+            << FixItHint::CreateInsertion(FTL.getRParenLoc(), "void");
+      }
     }
   }
 
-- 
1.7.0.4

