]> granicus.if.org Git - clang/commitdiff
Fix use of uninitialized value exposed by r267802. Accessors of an invalid
authorRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 28 Apr 2016 18:26:32 +0000 (18:26 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 28 Apr 2016 18:26:32 +0000 (18:26 +0000)
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
lib/Basic/SourceManager.cpp
lib/Frontend/DiagnosticRenderer.cpp
lib/Frontend/TextDiagnostic.cpp

index 0aeba5e3f3722a02be0e95d9266ac9f0405994e9..006cf3dc950c6770e43d61bb1e4befe6e46df185 100644 (file)
@@ -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; }
 };
 
 
index 92f473a3808213626fd97ed51da7e17b9929c7b1..c2d9e58c2959285b3412711890bd2c2017832d2f 100644 (file)
@@ -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<typename LocType>
+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__
index 302067a2fb05d29a74eb256d7a6481e0ebdcdfad..c4365f9ce0223f06a0b2dc0651fe7f64ce06d77e 100644 (file)
@@ -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)
index fdaf44d77b327fdadde9ba8c0024de6fcb911c5f..977af079a77aa75795b189cd44df11c146825317 100644 (file)
@@ -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