diff options
author | Daniel Dunbar <daniel@zuster.org> | 2009-05-18 17:30:52 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2009-05-18 17:30:52 +0000 |
commit | 70f924df8a8184161b130ba73049295831687e18 (patch) | |
tree | 05291102095fc30c467e4e165a41248e318694ef /clang/lib/Basic/SourceManager.cpp | |
parent | b81294d9892c1bde188323eff0bf6e92d1b47ced (diff) | |
download | llvm-70f924df8a8184161b130ba73049295831687e18.zip llvm-70f924df8a8184161b130ba73049295831687e18.tar.gz llvm-70f924df8a8184161b130ba73049295831687e18.tar.bz2 |
Avoid potential out-of-bounds access in SourceManager::getLineNumber.
- Chris, please see added FIXMEs.
llvm-svn: 72019
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r-- | clang/lib/Basic/SourceManager.cpp | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 9ca00f5..7d2d0ae 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -729,11 +729,22 @@ unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const { unsigned QueriedFilePos = FilePos+1; + // FIXME: I would like to be convinced that this code is worth being as + // complicated as it is, binary search isn't that slow. + // + // If it is worth being optimized, then in my opinion it could be more + // performant, simpler, and more obviously correct by just "galloping" outward + // from the queried file position. In fact, this could be incorporated into a + // generic algorithm such as lower_bound_with_hint. + // + // If someone gives me a test case where this matters, and I will do it! - DWD + // If the previous query was to the same file, we know both the file pos from // that query and the line number returned. This allows us to narrow the // search space from the entire file to something near the match. if (LastLineNoFileIDQuery == FID) { if (QueriedFilePos >= LastLineNoFilePos) { + // FIXME: Potential overflow? SourceLineCache = SourceLineCache+LastLineNoResult-1; // The query is likely to be nearby the previous one. Here we check to @@ -753,7 +764,8 @@ unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const { } } } else { - SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; + if (LastLineNoResult < Content->NumLines) + SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; } } |