From 9dee4d943b4dbbc64dbdaaf9052c094a6bb27ff3 Mon Sep 17 00:00:00 2001
From: Jactry Zeng <wine@jactry.com>
Date: Wed, 13 Aug 2014 14:57:52 +0800
Subject: riched20: Fix ME_RunOfsFromCharOfs() when nCharOfs > strlen().

---
 dlls/riched20/run.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/dlls/riched20/run.c b/dlls/riched20/run.c
index 38d0270..ad08b82 100644
--- a/dlls/riched20/run.c
+++ b/dlls/riched20/run.c
@@ -181,9 +181,10 @@ void ME_RunOfsFromCharOfs(ME_TextEditor *editor,
                           int *pOfs)
 {
   ME_DisplayItem *item, *next_item;
+  int endOfs = nCharOfs, len = ME_GetTextLength(editor);
 
   nCharOfs = max(nCharOfs, 0);
-  nCharOfs = min(nCharOfs, ME_GetTextLength(editor));
+  nCharOfs = min(nCharOfs, len);
 
   /* Find the paragraph at the offset. */
   next_item = editor->pBuffer->pFirst->member.para.next_para;
@@ -206,7 +207,11 @@ void ME_RunOfsFromCharOfs(ME_TextEditor *editor,
   nCharOfs -= item->member.run.nCharOfs;
 
   if (ppRun) *ppRun = item;
-  if (pOfs) *pOfs = nCharOfs;
+  if (pOfs) {
+    if (((*ppRun)->member.run.nFlags & MERF_ENDPARA) && endOfs > len)
+      *pOfs = (*ppRun)->member.run.len;
+    else *pOfs = nCharOfs;
+  }
 }
 
 /******************************************************************************
-- 
2.4.2

