From 26428bd2ba8df757091e2a9732dc12ba1a042fc4 Mon Sep 17 00:00:00 2001 From: Scott Sumner <30118311+sasumner@users.noreply.github.com> Date: Thu, 12 Nov 2020 20:25:22 -0500 Subject: [PATCH] Prevent corruption possibility when using -p command line parameter Fix #9142, close #9143 --- PowerEditor/src/Notepad_plus.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index 0ad5cf4a..626b64f1 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -5918,24 +5918,28 @@ std::vector Notepad_plus::loadCommandlineParams(const TCHAR * co } if (ln != -1 || cpos != -1) - { //we have to move the cursor manually + { + //we have to move the cursor manually int iView = currentView(); //store view since fileswitch can cause it to change switchToFile(bufID); //switch to the file. No deferred loading, but this way we can easily move the cursor to the right position if (cpos != -1) { + // make sure not jumping into the middle of a multibyte character + // or into the middle of a CR/LF pair for Windows files + auto before = _pEditView->execute(SCI_POSITIONBEFORE, cpos); + cpos = static_cast(_pEditView->execute(SCI_POSITIONAFTER, before)); _pEditView->execute(SCI_GOTOPOS, cpos); } - else - if (cn == -1) + else if (cn == -1) { _pEditView->execute(SCI_GOTOLINE, ln-1); } - else - { - auto pos = _pEditView->execute(SCI_FINDCOLUMN, ln-1, cn-1); - _pEditView->execute(SCI_GOTOPOS, pos); - } + else + { + auto pos = _pEditView->execute(SCI_FINDCOLUMN, ln-1, cn-1); + _pEditView->execute(SCI_GOTOPOS, pos); + } _pEditView->scrollPosToCenter(_pEditView->execute(SCI_GETCURRENTPOS));