From 32ef06ab969153673d13a987a350b33869a52c4d Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 28 Apr 2016 18:26:32 +0000 Subject: [PATCH] Fix use of uninitialized value exposed by r267802. Accessors of an invalid PresumedLoc should not be called. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@267914 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/SourceLocation.h | 8 ++++---- lib/Basic/SourceManager.cpp | 8 +++++--- lib/Frontend/DiagnosticRenderer.cpp | 3 ++- lib/Frontend/TextDiagnostic.cpp | 6 +++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/include/clang/Basic/SourceLocation.h b/include/clang/Basic/SourceLocation.h index 0aeba5e3f3..006cf3dc95 100644 --- a/include/clang/Basic/SourceLocation.h +++ b/include/clang/Basic/SourceLocation.h @@ -373,22 +373,22 @@ public: /// \brief Return the presumed filename of this location. /// /// This can be affected by \#line etc. - const char *getFilename() const { return Filename; } + const char *getFilename() const { assert(isValid()); return Filename; } /// \brief Return the presumed line number of this location. /// /// This can be affected by \#line etc. - unsigned getLine() const { return Line; } + unsigned getLine() const { assert(isValid()); return Line; } /// \brief Return the presumed column number of this location. /// /// This cannot be affected by \#line, but is packaged here for convenience. - unsigned getColumn() const { return Col; } + unsigned getColumn() const { assert(isValid()); return Col; } /// \brief Return the presumed include location of this location. /// /// This can be affected by GNU linemarker directives. - SourceLocation getIncludeLoc() const { return IncludeLoc; } + SourceLocation getIncludeLoc() const { assert(isValid()); return IncludeLoc; } }; diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp index 92f473a380..c2d9e58c29 100644 --- a/lib/Basic/SourceManager.cpp +++ b/lib/Basic/SourceManager.cpp @@ -1160,7 +1160,8 @@ unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos, // isInvalid - Return the result of calling loc.isInvalid(), and // if Invalid is not null, set its value to same. -static bool isInvalid(SourceLocation Loc, bool *Invalid) { +template +static bool isInvalid(LocType Loc, bool *Invalid) { bool MyInvalid = Loc.isInvalid(); if (Invalid) *Invalid = MyInvalid; @@ -1183,8 +1184,9 @@ unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc, unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc, bool *Invalid) const { - if (isInvalid(Loc, Invalid)) return 0; - return getPresumedLoc(Loc).getColumn(); + PresumedLoc PLoc = getPresumedLoc(Loc); + if (isInvalid(PLoc, Invalid)) return 0; + return PLoc.getColumn(); } #ifdef __SSE2__ diff --git a/lib/Frontend/DiagnosticRenderer.cpp b/lib/Frontend/DiagnosticRenderer.cpp index 302067a2fb..c4365f9ce0 100644 --- a/lib/Frontend/DiagnosticRenderer.cpp +++ b/lib/Frontend/DiagnosticRenderer.cpp @@ -167,7 +167,8 @@ void DiagnosticRenderer::emitIncludeStack(SourceLocation Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, const SourceManager &SM) { - SourceLocation IncludeLoc = PLoc.getIncludeLoc(); + SourceLocation IncludeLoc = + PLoc.isInvalid() ? SourceLocation() : PLoc.getIncludeLoc(); // Skip redundant include stacks altogether. if (LastIncludeLoc == IncludeLoc) diff --git a/lib/Frontend/TextDiagnostic.cpp b/lib/Frontend/TextDiagnostic.cpp index fdaf44d77b..977af079a7 100644 --- a/lib/Frontend/TextDiagnostic.cpp +++ b/lib/Frontend/TextDiagnostic.cpp @@ -883,7 +883,7 @@ void TextDiagnostic::emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc, void TextDiagnostic::emitIncludeLocation(SourceLocation Loc, PresumedLoc PLoc, const SourceManager &SM) { - if (DiagOpts->ShowLocation && PLoc.getFilename()) + if (DiagOpts->ShowLocation && PLoc.isValid()) OS << "In file included from " << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; else @@ -893,7 +893,7 @@ void TextDiagnostic::emitIncludeLocation(SourceLocation Loc, void TextDiagnostic::emitImportLocation(SourceLocation Loc, PresumedLoc PLoc, StringRef ModuleName, const SourceManager &SM) { - if (DiagOpts->ShowLocation && PLoc.getFilename()) + if (DiagOpts->ShowLocation && PLoc.isValid()) OS << "In module '" << ModuleName << "' imported from " << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; else @@ -904,7 +904,7 @@ void TextDiagnostic::emitBuildingModuleLocation(SourceLocation Loc, PresumedLoc PLoc, StringRef ModuleName, const SourceManager &SM) { - if (DiagOpts->ShowLocation && PLoc.getFilename()) + if (DiagOpts->ShowLocation && PLoc.isValid()) OS << "While building module '" << ModuleName << "' imported from " << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; else -- 2.40.0