From a476a9a22432a8a8053ffeb2db7281268a01fb2c Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 27 Feb 2019 11:41:45 +0700 Subject: [PATCH] JoinLinesHandler: check offset range; report failing handler (EA-137957 - assert: DocumentImpl.createRangeMarker) --- .../editorActions/JoinLinesHandler.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/JoinLinesHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/JoinLinesHandler.java index 284a5213af98..9e47f6a8cadb 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/JoinLinesHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/JoinLinesHandler.java @@ -148,7 +148,7 @@ public class JoinLinesHandler extends EditorActionHandler { if (delegate instanceof JoinRawLinesHandlerDelegate) { rc = ((JoinRawLinesHandlerDelegate)delegate).tryJoinRawLines(doc, psiFile, start, end); if (rc != CANNOT_JOIN) { - caretRestoreOffset.set(rc); + caretRestoreOffset.set(checkOffset(rc, delegate, doc)); break; } } @@ -183,7 +183,7 @@ public class JoinLinesHandler extends EditorActionHandler { docManager.commitDocument(doc); for(JoinLinesHandlerDelegate delegate: JoinLinesHandlerDelegate.EP_NAME.getExtensionList()) { - rc = delegate.tryJoinLines(doc, psiFile, start, end); + rc = checkOffset(delegate.tryJoinLines(doc, psiFile, start, end), delegate, doc); if (rc != CANNOT_JOIN) break; } } @@ -241,6 +241,19 @@ public class JoinLinesHandler extends EditorActionHandler { docManager.commitDocument(doc); } + private static int checkOffset(int offset, JoinLinesHandlerDelegate delegate, DocumentEx doc) { + if (offset == CANNOT_JOIN) return offset; + if (offset < 0) { + LOG.error("Handler returned negative offset: handler class="+delegate.getClass()+"; offset="+offset); + return 0; + } else if (offset > doc.getTextLength()) { + LOG.error("Handler returned an offset which exceeds the document length: handler class=" + delegate.getClass() + + "; offset=" + offset + "; length=" + doc.getTextLength()); + return doc.getTextLength(); + } + return offset; + } + private static class JoinLinesOffsets { int lineEndOffset; int lastNonSpaceOffsetInStartLine;