From ac83a3cdbfa3dad298ab3d87fb608b9b08176e57 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 25 Jun 2013 20:34:17 +0000 Subject: [PATCH] Fix null pointer dereference if we redeclare an unprototyped function. Patch by WenHan Gu! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@184875 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDecl.cpp | 20 +++++++++++--------- test/Sema/vfprintf-valid-redecl.c | 8 +++++++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 43db2962a7..a5092d5a38 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -8732,17 +8732,19 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D) { const FunctionDecl *PossibleZeroParamPrototype = 0; if (ShouldWarnAboutMissingPrototype(FD, PossibleZeroParamPrototype)) { Diag(FD->getLocation(), diag::warn_missing_prototype) << FD; - + if (PossibleZeroParamPrototype) { - // We found a declaration that is not a prototype, + // 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()) - 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()) + Diag(PossibleZeroParamPrototype->getLocation(), + diag::note_declaration_not_a_prototype) + << PossibleZeroParamPrototype + << FixItHint::CreateInsertion(FTL.getRParenLoc(), "void"); + } } } diff --git a/test/Sema/vfprintf-valid-redecl.c b/test/Sema/vfprintf-valid-redecl.c index 5c5ce8d12b..5c78874feb 100644 --- a/test/Sema/vfprintf-valid-redecl.c +++ b/test/Sema/vfprintf-valid-redecl.c @@ -1,7 +1,13 @@ // RUN: %clang_cc1 %s -fsyntax-only -pedantic -verify // expected-no-diagnostics -// PR4290 +// PR16344 +// Clang has defined 'vfprint' in builtin list. If the following line occurs before any other +// `vfprintf' in this file, and we getPreviousDecl()->getTypeSourceInfo() on it, then we will +// get a null pointer since the one in builtin list doesn't has valid TypeSourceInfo. +int vfprintf(void) { return 0; } + +// PR4290 // The following declaration is compatible with vfprintf, so we shouldn't // warn. int vfprintf(); -- 2.40.0