From 773f470fcf82012df14af2ca89ea91d5d2f7a85b Mon Sep 17 00:00:00 2001
From: Drake Arconis <lightdrake@gmail.com>
Date: Tue, 19 Aug 2014 19:55:18 -0400
Subject: [PATCH] Fix crashes occuring from C-Casting LLJoint*

---
 indra/llappearance/llavatarappearance.cpp     |  6 +--
 indra/llappearance/llavatarjoint.cpp          | 50 +++++++++++--------
 indra/llappearance/llavatarjointmesh.cpp      |  9 ++--
 .../llappearance/llpolyskeletaldistortion.cpp |  4 +-
 indra/newview/llviewerjoint.cpp               | 19 ++++---
 5 files changed, 52 insertions(+), 36 deletions(-)

diff --git a/indra/llappearance/llavatarappearance.cpp b/indra/llappearance/llavatarappearance.cpp
index 6fdf9e2e07..0a1569a9ba 100755
--- a/indra/llappearance/llavatarappearance.cpp
+++ b/indra/llappearance/llavatarappearance.cpp
@@ -953,19 +953,19 @@ BOOL LLAvatarAppearance::loadSkeletonNode ()
 	mRoot->addChild(mMeshLOD[MESH_ID_SKIRT]);
 	mRoot->addChild(mMeshLOD[MESH_ID_HEAD]);
 
-	LLAvatarJoint *skull = (LLAvatarJoint*)mRoot->findJoint("mSkull");
+	LLAvatarJoint *skull = dynamic_cast<LLAvatarJoint*>(mRoot->findJoint("mSkull"));
 	if (skull)
 	{
 		skull->addChild(mMeshLOD[MESH_ID_HAIR] );
 	}
 
-	LLAvatarJoint *eyeL = (LLAvatarJoint*)mRoot->findJoint("mEyeLeft");
+	LLAvatarJoint *eyeL = dynamic_cast<LLAvatarJoint*>(mRoot->findJoint("mEyeLeft"));
 	if (eyeL)
 	{
 		eyeL->addChild( mMeshLOD[MESH_ID_EYEBALL_LEFT] );
 	}
 
-	LLAvatarJoint *eyeR = (LLAvatarJoint*)mRoot->findJoint("mEyeRight");
+	LLAvatarJoint *eyeR = dynamic_cast<LLAvatarJoint*>(mRoot->findJoint("mEyeRight"));
 	if (eyeR)
 	{
 		eyeR->addChild( mMeshLOD[MESH_ID_EYEBALL_RIGHT] );
diff --git a/indra/llappearance/llavatarjoint.cpp b/indra/llappearance/llavatarjoint.cpp
index 2ee3c65a01..cb887d8569 100644
--- a/indra/llappearance/llavatarjoint.cpp
+++ b/indra/llappearance/llavatarjoint.cpp
@@ -104,8 +104,9 @@ void LLAvatarJoint::setValid( BOOL valid, BOOL recursive )
 		for (child_list_t::iterator iter = mChildren.begin();
 			 iter != mChildren.end(); ++iter)
 		{
-			LLAvatarJoint* joint = (LLAvatarJoint*)(*iter);
-			joint->setValid(valid, TRUE);
+			LLAvatarJoint* joint = dynamic_cast<LLAvatarJoint*>(*iter);
+			if (joint)
+				joint->setValid(valid, TRUE);
 		}
 	}
 
@@ -123,7 +124,8 @@ void LLAvatarJoint::setSkeletonComponents( U32 comp, BOOL recursive )
 			 iter != mChildren.end(); ++iter)
 		{
 			LLAvatarJoint* joint = dynamic_cast<LLAvatarJoint*>(*iter);
-			joint->setSkeletonComponents(comp, recursive);
+			if (joint)
+				joint->setSkeletonComponents(comp, recursive);
 		}
 	}
 }
@@ -137,8 +139,9 @@ void LLAvatarJoint::setVisible(BOOL visible, BOOL recursive)
 		for (child_list_t::iterator iter = mChildren.begin();
 			 iter != mChildren.end(); ++iter)
 		{
-			LLAvatarJoint* joint = (LLAvatarJoint*)(*iter);
-			joint->setVisible(visible, recursive);
+			LLAvatarJoint* joint = dynamic_cast<LLAvatarJoint*>(*iter);
+			if (joint)
+				joint->setVisible(visible, recursive);
 		}
 	}
 }
@@ -149,7 +152,8 @@ void LLAvatarJoint::updateFaceSizes(U32 &num_vertices, U32& num_indices, F32 pix
 		 iter != mChildren.end(); ++iter)
 	{
 		LLAvatarJoint* joint = dynamic_cast<LLAvatarJoint*>(*iter);
-		joint->updateFaceSizes(num_vertices, num_indices, pixel_area);
+		if (joint)
+			joint->updateFaceSizes(num_vertices, num_indices, pixel_area);
 	}
 }
 
@@ -159,7 +163,8 @@ void LLAvatarJoint::updateFaceData(LLFace *face, F32 pixel_area, BOOL damp_wind,
 		 iter != mChildren.end(); ++iter)
 	{
 		LLAvatarJoint* joint = dynamic_cast<LLAvatarJoint*>(*iter);
-		joint->updateFaceData(face, pixel_area, damp_wind, terse_update);
+		if (joint)
+			joint->updateFaceData(face, pixel_area, damp_wind, terse_update);
 	}
 }
 
@@ -169,7 +174,8 @@ void LLAvatarJoint::updateJointGeometry()
 		 iter != mChildren.end(); ++iter)
 	{
 		LLAvatarJoint* joint = dynamic_cast<LLAvatarJoint*>(*iter);
-		joint->updateJointGeometry();
+		if (joint)
+			joint->updateJointGeometry();
 	}
 }
 
@@ -183,23 +189,26 @@ BOOL LLAvatarJoint::updateLOD(F32 pixel_area, BOOL activate)
 		 iter != mChildren.end(); ++iter)
 	{
 		LLAvatarJoint* joint = dynamic_cast<LLAvatarJoint*>(*iter);
-		F32 jointLOD = joint->getLOD();
-		
-		if (found_lod || jointLOD == DEFAULT_AVATAR_JOINT_LOD)
+		if (joint)
 		{
-			// we've already found a joint to enable, so enable the rest as alternatives
-			lod_changed |= joint->updateLOD(pixel_area, TRUE);
-		}
-		else
-		{
-			if (pixel_area >= jointLOD || sDisableLOD)
+			F32 jointLOD = joint->getLOD();
+
+			if (found_lod || jointLOD == DEFAULT_AVATAR_JOINT_LOD)
 			{
+				// we've already found a joint to enable, so enable the rest as alternatives
 				lod_changed |= joint->updateLOD(pixel_area, TRUE);
-				found_lod = TRUE;
 			}
 			else
 			{
-				lod_changed |= joint->updateLOD(pixel_area, FALSE);
+				if (pixel_area >= jointLOD || sDisableLOD)
+				{
+					lod_changed |= joint->updateLOD(pixel_area, TRUE);
+					found_lod = TRUE;
+				}
+				else
+				{
+					lod_changed |= joint->updateLOD(pixel_area, FALSE);
+				}
 			}
 		}
 	}
@@ -212,7 +221,8 @@ void LLAvatarJoint::dump()
 		 iter != mChildren.end(); ++iter)
 	{
 		LLAvatarJoint* joint = dynamic_cast<LLAvatarJoint*>(*iter);
-		joint->dump();
+		if (joint)
+			joint->dump();
 	}
 }
 
diff --git a/indra/llappearance/llavatarjointmesh.cpp b/indra/llappearance/llavatarjointmesh.cpp
index 520ad775db..d32915caab 100644
--- a/indra/llappearance/llavatarjointmesh.cpp
+++ b/indra/llappearance/llavatarjointmesh.cpp
@@ -98,7 +98,7 @@ BOOL LLSkinJoint::setupSkinJoint( LLAvatarJoint *joint)
 	while (joint)
 	{
 		rootSkinOffset += joint->getSkinOffset();
-		joint = (LLAvatarJoint*)joint->getParent();
+		joint = dynamic_cast<LLAvatarJoint*>(joint->getParent());
 	}
 
 	mRootToJointSkinOffset = -rootSkinOffset;
@@ -362,8 +362,11 @@ void LLAvatarJointMesh::setupJoint(LLAvatarJoint* current_joint)
 	for (LLJoint::child_list_t::iterator iter = current_joint->mChildren.begin();
 		 iter != current_joint->mChildren.end(); ++iter)
 	{
-		LLAvatarJoint* child_joint = (LLAvatarJoint*)(*iter);
-		setupJoint(child_joint);
+		LLAvatarJoint* child_joint = dynamic_cast<LLAvatarJoint*>(*iter);
+		if (child_joint)
+		{
+			setupJoint(child_joint);
+		}
 	}
 }
 
diff --git a/indra/llappearance/llpolyskeletaldistortion.cpp b/indra/llappearance/llpolyskeletaldistortion.cpp
index ea29cbd451..29f54b3a42 100644
--- a/indra/llappearance/llpolyskeletaldistortion.cpp
+++ b/indra/llappearance/llpolyskeletaldistortion.cpp
@@ -164,8 +164,8 @@ BOOL LLPolySkeletalDistortion::setInfo(LLPolySkeletalDistortionInfo *info)
                 for (LLJoint::child_list_t::iterator iter = joint->mChildren.begin();
                      iter != joint->mChildren.end(); ++iter)
                 {
-                        LLAvatarJoint* child_joint = (LLAvatarJoint*)(*iter);
-                        if (child_joint->inheritScale())
+                        LLAvatarJoint* child_joint = dynamic_cast<LLAvatarJoint*>(*iter);
+						if (child_joint && child_joint->inheritScale())
                         {
                                 LLVector3 childDeformation = LLVector3(child_joint->getScale());
                                 childDeformation.scaleVec(bone_info->mScaleDeformation);
diff --git a/indra/newview/llviewerjoint.cpp b/indra/newview/llviewerjoint.cpp
index e46299f9d2..173f39738f 100755
--- a/indra/newview/llviewerjoint.cpp
+++ b/indra/newview/llviewerjoint.cpp
@@ -143,21 +143,24 @@ U32 LLViewerJoint::render( F32 pixelArea, BOOL first_pass, BOOL is_dummy )
 	// render children
 	//----------------------------------------------------------------
 	for (child_list_t::iterator iter = mChildren.begin();
-		 iter != mChildren.end(); ++iter)
+		iter != mChildren.end(); ++iter)
 	{
 		LLAvatarJoint* joint = dynamic_cast<LLAvatarJoint*>(*iter);
-		F32 jointLOD = joint->getLOD();
-		if (pixelArea >= jointLOD || sDisableLOD)
+		if (joint)
 		{
-			triangle_count += joint->render( pixelArea, TRUE, is_dummy );
-
-			if (jointLOD != DEFAULT_AVATAR_JOINT_LOD)
+			F32 jointLOD = joint->getLOD();
+			if (pixelArea >= jointLOD || sDisableLOD)
 			{
-				break;
+				triangle_count += joint->render(pixelArea, TRUE, is_dummy);
+
+				if (jointLOD != DEFAULT_AVATAR_JOINT_LOD)
+				{
+					break;
+				}
 			}
+
 		}
 	}
-
 	return triangle_count;
 }
 
-- 
GitLab