From 6d3be57d334f02f4781dcaede9746976bd3643a4 Mon Sep 17 00:00:00 2001
From: Kelly Washington <kelly@lindenlab.com>
Date: Wed, 25 Apr 2012 11:26:54 -0700
Subject: [PATCH] SEC-995 FIX viewer is easily spammed to death by chat removed
 a couple more unnecessary string copies from unfortunate LLSD behavior.
 reviewed with simon, post review from Richard.

---
 indra/llcommon/llsd.cpp               |  1 +
 indra/llmessage/llsdmessagereader.cpp |  2 +-
 indra/llui/lltextbase.cpp             | 16 ++++++++++++++--
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/indra/llcommon/llsd.cpp b/indra/llcommon/llsd.cpp
index e295e3c621d..8276ec836a7 100644
--- a/indra/llcommon/llsd.cpp
+++ b/indra/llcommon/llsd.cpp
@@ -269,6 +269,7 @@ namespace
 		virtual LLSD::UUID		asUUID() const	{ return LLUUID(mValue); }
 		virtual LLSD::Date		asDate() const	{ return LLDate(mValue); }
 		virtual LLSD::URI		asURI() const	{ return LLURI(mValue); }
+		virtual int				size() const	{ return mValue.size(); }
 	};
 	
 	LLSD::Integer	ImplString::asInteger() const
diff --git a/indra/llmessage/llsdmessagereader.cpp b/indra/llmessage/llsdmessagereader.cpp
index 3d8ca2ad9f1..a6fccd2a566 100644
--- a/indra/llmessage/llsdmessagereader.cpp
+++ b/indra/llmessage/llsdmessagereader.cpp
@@ -276,7 +276,7 @@ S32 getElementSize(const LLSD& llsd)
 	case LLSD::TypeReal:
 		return sizeof(F64);
 	case LLSD::TypeString:
-		return llsd.asString().size();
+		return llsd.size();
 	case LLSD::TypeUUID:
 		return sizeof(LLUUID);
 	case LLSD::TypeDate:
diff --git a/indra/llui/lltextbase.cpp b/indra/llui/lltextbase.cpp
index c2cd590acb7..813f919505e 100644
--- a/indra/llui/lltextbase.cpp
+++ b/indra/llui/lltextbase.cpp
@@ -280,11 +280,23 @@ bool LLTextBase::truncate()
 	if (getLength() >= S32(mMaxTextByteLength / 4))
 	{	
 		// Have to check actual byte size
-		S32 utf8_byte_size = getViewModel()->getValue().asString().size();
+		S32 utf8_byte_size = 0;
+		LLSD value = getViewModel()->getValue();
+		if (value.type() == LLSD::TypeString)
+		{
+			// save a copy for strings.
+			utf8_byte_size = value.size();
+		}
+		else
+		{
+			// non string LLSDs need explicit conversion to string
+			utf8_byte_size = value.asString().size();
+		}
+
 		if ( utf8_byte_size > mMaxTextByteLength )
 		{
 			// Truncate safely in UTF-8
-			std::string temp_utf8_text = getViewModel()->getValue().asString();
+			std::string temp_utf8_text = value.asString();
 			temp_utf8_text = utf8str_truncate( temp_utf8_text, mMaxTextByteLength );
 			LLWString text = utf8str_to_wstring( temp_utf8_text );
 			// remove extra bit of current string, to preserve formatting, etc.
-- 
GitLab