From a0e44e3dd3e09d24ac2d329aa87f4bd34a59f301 Mon Sep 17 00:00:00 2001
From: Martin Reddy <lynx@lindenlab.com>
Date: Wed, 23 Sep 2009 14:56:02 +0000
Subject: [PATCH] EXT-944 EXT-1026: re-fixing these crashing bugs by making
 sure that we don't use character offsets when mixing utf8 and wide strings.
 Internal JIRAs: DEV-40127 DEV-39966.

---
 indra/llui/lltextbox.cpp | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/indra/llui/lltextbox.cpp b/indra/llui/lltextbox.cpp
index 810626268fc..9de474a4f55 100644
--- a/indra/llui/lltextbox.cpp
+++ b/indra/llui/lltextbox.cpp
@@ -299,8 +299,9 @@ LLWString LLTextBox::getWrappedText(const LLStringExplicit& in_text, F32 max_wid
 	LLUrlMatch match;
 	while ( LLUrlRegistry::instance().findUrl(wstring_to_utf8str(wtext), match))
 	{
-		S32 start = match.getStart();
-		S32 end = match.getEnd() + 1;
+		LLWString wurl = utf8str_to_wstring(match.getUrl());
+		S32 start = wtext.find(wurl);
+		S32 end = start + wurl.size();
 
 		// perform word wrap on the text before the Url
 		final_wtext += wrapText(wtext.substr(0, start), hoffset, line_num, max_width);
@@ -577,13 +578,17 @@ void LLTextBox::updateDisplayTextAndSegments()
 											  boost::bind(&LLTextBox::onUrlLabelUpdated, this, _1, _2)) )
 	{
 		// work out the char offset for the start/end of the url
+		LLWString wurl = utf8str_to_wstring(match.getUrl());
+		S32 url_start = text.find(wurl);
+		S32 url_end = url_start + wurl.size() - 1;
+
 		S32 seg_start = mDisplayText.size();
-		S32 start = seg_start + match.getStart();
-		end = start + match.getLabel().size();
+		S32 start = seg_start + url_start;
+		S32 end = start + match.getLabel().size();
 
 		// create a segment for the text before the Url
 		mSegments.insert(new LLNormalTextSegment(new LLStyle(), seg_start, start, *this));
-		mDisplayText += text.substr(0, match.getStart());
+		mDisplayText += text.substr(0, url_start);
 
 		// create a segment for the Url text
 		LLStyleSP html(new LLStyle);
@@ -599,7 +604,7 @@ void LLTextBox::updateDisplayTextAndSegments()
 		mDisplayText += utf8str_to_wstring(match.getLabel());
 
 		// move on to the rest of the text after the Url
-		text = text.substr(match.getEnd()+1, text.size() - match.getEnd());
+		text = text.substr(url_end+1, text.size() - url_end);
 	}
 
 	// output a segment for the remaining text
-- 
GitLab