From bbd071f3d53b3a8169647f8594ce97ccdbc86f36 Mon Sep 17 00:00:00 2001
From: Andrey Kleshchev <andreykproductengine@lindenlab.com>
Date: Mon, 18 Jan 2021 17:10:21 +0200
Subject: [PATCH] SL-14597 ensure that uris don't crash viewer

---
 indra/llcommon/lluriparser.cpp | 51 +++++++++++++++++++++-------------
 1 file changed, 31 insertions(+), 20 deletions(-)

diff --git a/indra/llcommon/lluriparser.cpp b/indra/llcommon/lluriparser.cpp
index c275b90120d..015ff2d7c30 100644
--- a/indra/llcommon/lluriparser.cpp
+++ b/indra/llcommon/lluriparser.cpp
@@ -163,26 +163,37 @@ S32 LLUriParser::normalize()
 	mNormalizedTmp = mTmpScheme;
 	if (!mRes)
 	{
-		mRes = uriNormalizeSyntaxExA(&mUri, URI_NORMALIZE_SCHEME | URI_NORMALIZE_HOST);
-
-		if (!mRes)
-		{
-			S32 chars_required;
-			mRes = uriToStringCharsRequiredA(&mUri, &chars_required);
-
-			if (!mRes)
-			{
-				chars_required++;
-				std::vector<char> label_buf(chars_required);
-				mRes = uriToStringA(&label_buf[0], &mUri, chars_required, NULL);
-
-				if (!mRes)
-				{
-					mNormalizedUri = &label_buf[mTmpScheme ? 7 : 0];
-					mTmpScheme = false;
-				}
-			}
-		}
+        // Uriparser is 3p software we can not directly fix.
+        // On winodws this probably requires SEH handling, but all
+        // crashes so far were on MAC in scope of uriNormalizeSyntaxExA.
+        try
+        {
+            mRes = uriNormalizeSyntaxExA(&mUri, URI_NORMALIZE_SCHEME | URI_NORMALIZE_HOST);
+
+            if (!mRes)
+            {
+                S32 chars_required;
+                mRes = uriToStringCharsRequiredA(&mUri, &chars_required);
+
+                if (!mRes)
+                {
+                    chars_required++;
+                    std::vector<char> label_buf(chars_required);
+                    mRes = uriToStringA(&label_buf[0], &mUri, chars_required, NULL);
+
+                    if (!mRes)
+                    {
+                        mNormalizedUri = &label_buf[mTmpScheme ? 7 : 0];
+                        mTmpScheme = false;
+                    }
+                }
+            }
+        }
+        catch (...)
+        {
+            // At this point mNormalizedUri should contain http+unmodified input string.
+            LL_WARNS() << "Uriparser crashed processing: " << mNormalizedUri << LL_ENDL;
+        }
 	}
 
 	if(mTmpScheme)
-- 
GitLab