[PATCH] D117579: Add assert on End iteration distance to Rewriter::getRewrittenText.

ahedberg via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 18 10:01:31 PST 2022


ahedberg created this revision.
ahedberg requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

I currently have code that is crashing in the second std::advance call,
and it was not straightforward to identify the problem, as the first line
of the stacktrace is in RopePieceBTreeIterator::operator++:

- SIGILL; stack trace: ***

PC: clang/include/clang/Rewrite/Core/RewriteRope.h:119 clang::RopePieceBTreeIterator::operator++()

  ../include/c++/v1/__iterator/advance.h:35 std::__u::__advance<>()
  ../include/c++/v1/__iterator/advance.h:65 std::__u::advance<>()
  clang/lib/Rewrite/Rewriter.cpp:228 clang::Rewriter::getRewrittenText()
  clang/include/clang/Rewrite/Core/Rewriter.h:106 clang::Rewriter::getRewrittenText()

Adding an assertion produces a friendlier error message for the caller.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117579

Files:
  clang/lib/Rewrite/Rewriter.cpp


Index: clang/lib/Rewrite/Rewriter.cpp
===================================================================
--- clang/lib/Rewrite/Rewriter.cpp
+++ clang/lib/Rewrite/Rewriter.cpp
@@ -223,6 +223,7 @@
   RewriteBuffer::iterator Start = RB.begin();
   std::advance(Start, StartOff);
   RewriteBuffer::iterator End = Start;
+  assert(EndOff >= StartOff && "Invalid iteration distance");
   std::advance(End, EndOff-StartOff);
 
   return std::string(Start, End);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117579.400896.patch
Type: text/x-patch
Size: 456 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220118/637e992c/attachment.bin>


More information about the cfe-commits mailing list