From db3062c60bb8bb65bfa8b3df21a51916746cd3b1 Mon Sep 17 00:00:00 2001
From: nillerusr <nillerusr@gmail.com>
Date: Wed, 16 Nov 2022 13:28:39 +0300
Subject: [PATCH] WIP: fix some memaccess-class warnings

---
 mathlib/polyhedron.cpp                  |  9 +++----
 public/materialsystem/imaterialsystem.h |  9 +++----
 public/mathlib/simdvectormatrix.h       | 18 ++++++++------
 public/mathlib/ssemath.h                |  7 ++++++
 public/studio.h                         | 10 ++++----
 public/tier0/platform.h                 |  6 ++---
 public/tier1/memhelpers.h               | 32 +++++++++++++++++++++++++
 public/vphysics_interface.h             |  4 ----
 studiorender/r_studiolight.cpp          |  4 ++--
 studiorender/studiorendercontext.cpp    |  3 ++-
 tier0/cpu.cpp                           |  3 ---
 tier2/camerautils.cpp                   |  8 ++++++-
 12 files changed, 78 insertions(+), 35 deletions(-)
 create mode 100644 public/tier1/memhelpers.h

diff --git a/mathlib/polyhedron.cpp b/mathlib/polyhedron.cpp
index 51af4ca6ad..f4b9e44e65 100644
--- a/mathlib/polyhedron.cpp
+++ b/mathlib/polyhedron.cpp
@@ -11,6 +11,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include "tier1/utlvector.h"
+#include "tier1/memhelpers.h"
 #ifdef COMPILER_MSVC
 #include <new>
 #endif
@@ -318,10 +319,10 @@ CPolyhedron *ClipPolyhedron( const CPolyhedron *pExistingPolyhedron, const float
 														pExistingPolyhedron->iPolygonCount );
 		}
 
-		memcpy( pReturn->pVertices, pExistingPolyhedron->pVertices, sizeof( Vector ) * pReturn->iVertexCount );
-		memcpy( pReturn->pLines, pExistingPolyhedron->pLines, sizeof( Polyhedron_IndexedLine_t ) * pReturn->iLineCount );
-		memcpy( pReturn->pIndices, pExistingPolyhedron->pIndices, sizeof( Polyhedron_IndexedLineReference_t ) * pReturn->iIndexCount );
-		memcpy( pReturn->pPolygons, pExistingPolyhedron->pPolygons, sizeof( Polyhedron_IndexedPolygon_t ) * pReturn->iPolygonCount );
+		memutils::copy( pReturn->pVertices, pExistingPolyhedron->pVertices, pReturn->iVertexCount );
+		memutils::copy( pReturn->pLines, pExistingPolyhedron->pLines, pReturn->iLineCount );
+		memutils::copy( pReturn->pIndices, pExistingPolyhedron->pIndices, pReturn->iIndexCount );
+		memutils::copy( pReturn->pPolygons, pExistingPolyhedron->pPolygons, pReturn->iPolygonCount );
 
 		return pReturn;
 	}
diff --git a/public/materialsystem/imaterialsystem.h b/public/materialsystem/imaterialsystem.h
index 407d8b8d17..7d71c31f1c 100644
--- a/public/materialsystem/imaterialsystem.h
+++ b/public/materialsystem/imaterialsystem.h
@@ -30,7 +30,7 @@
 #include "materialsystem/deformations.h"
 #include "materialsystem/imaterialsystemhardwareconfig.h"
 #include "materialsystem/IColorCorrection.h"
-
+#include "tier1/memhelpers.h"
 
 //-----------------------------------------------------------------------------
 // forward declarations
@@ -1563,12 +1563,9 @@ public:
 
 template< class E > inline E* IMatRenderContext::LockRenderDataTyped( int nCount, const E* pSrcData )
 {
-	int nSizeInBytes = nCount * sizeof(E);
-	E *pDstData = (E*)LockRenderData( nSizeInBytes );
+	E *pDstData = (E*)LockRenderData( nCount*sizeof(E) );
 	if ( pSrcData && pDstData )
-	{
-		memcpy( pDstData, pSrcData, nSizeInBytes );
-	}
+		memutils::copy( pDstData, pSrcData, nCount );
 	return pDstData;
 }
 
diff --git a/public/mathlib/simdvectormatrix.h b/public/mathlib/simdvectormatrix.h
index f88cd328ca..f638152b19 100644
--- a/public/mathlib/simdvectormatrix.h
+++ b/public/mathlib/simdvectormatrix.h
@@ -20,6 +20,7 @@
 #include "tier0/dbg.h"
 #include "tier1/utlsoacontainer.h"
 #include "mathlib/ssemath.h"
+#include "tier1/memhelpers.h"
 
 class CSIMDVectorMatrix
 {
@@ -61,19 +62,19 @@ public:
 	// set up storage and fields for m x n matrix. destroys old data
 	void SetSize( int width, int height )
 	{
-		if ( ( ! m_pData ) || ( width != m_nWidth ) || ( height != m_nHeight ) )
+		if ( ( ! m_pData ) || ( width > m_nWidth ) || ( height > m_nHeight ) )
 		{
 			if ( m_pData )
 				delete[] m_pData;
-			
-			m_nWidth = width;
-			m_nHeight = height;
-			
+
 			m_nPaddedWidth = ( m_nWidth + 3) >> 2;
 			m_pData = NULL;
 			if ( width && height )
 				m_pData = new FourVectors[ m_nPaddedWidth * m_nHeight ];
 		}
+
+		m_nWidth = width;
+		m_nHeight = height;
 	}
 
 	CSIMDVectorMatrix( int width, int height )
@@ -86,7 +87,8 @@ public:
 	{
 		SetSize( src.m_nWidth, src.m_nHeight );
 		if ( m_pData )
-			memcpy( m_pData, src.m_pData, m_nHeight*m_nPaddedWidth*sizeof(m_pData[0]) ); 
+			memutils::copy( m_pData, src.m_pData, m_nHeight*m_nPaddedWidth );
+
 		return *this;
 	}
 
@@ -131,7 +133,9 @@ public:
 	void Clear( void )
 	{
 		Assert( m_pData );
-		memset( m_pData, 0, m_nHeight*m_nPaddedWidth*sizeof(m_pData[0]) );
+
+		static FourVectors value{Four_Zeros, Four_Zeros, Four_Zeros};
+		memutils::set( m_pData, value, m_nHeight*m_nPaddedWidth );
 	}
 
 	void RaiseToPower( float power );
diff --git a/public/mathlib/ssemath.h b/public/mathlib/ssemath.h
index af381148f8..8b9def868e 100644
--- a/public/mathlib/ssemath.h
+++ b/public/mathlib/ssemath.h
@@ -2613,6 +2613,13 @@ public:
 		z=src.z;
 	}
 
+	FourVectors( fltx4 x, fltx4 y, fltx4 z )
+	{
+		this->x=x;
+		this->y=y;
+		this->z=z;
+	}
+
 	FORCEINLINE void operator=( FourVectors const &src )
 	{
 		x=src.x;
diff --git a/public/studio.h b/public/studio.h
index 17ed4b4e63..19aae8b319 100644
--- a/public/studio.h
+++ b/public/studio.h
@@ -3298,17 +3298,19 @@ inline int Studio_LoadVertexes( const vertexFileHeader_t *pTempVvdHdr, vertexFil
 		}
 
 		// copy vertexes
+
+		// TODO(nillerusr): That sucks and needs to be fixed
 		memcpy(
-			(mstudiovertex_t *)((byte *)pNewVvdHdr+pNewVvdHdr->vertexDataStart) + target,
-			(mstudiovertex_t *)((byte *)pTempVvdHdr+pTempVvdHdr->vertexDataStart) + pFixupTable[i].sourceVertexID,
+			(byte*)((mstudiovertex_t *)((byte *)pNewVvdHdr+pNewVvdHdr->vertexDataStart) + target),
+			(byte*)((mstudiovertex_t *)((byte *)pTempVvdHdr+pTempVvdHdr->vertexDataStart) + pFixupTable[i].sourceVertexID),
 			pFixupTable[i].numVertexes*sizeof(mstudiovertex_t) );
 
 		if (bNeedsTangentS)
 		{
 			// copy tangents
 			memcpy(
-				(Vector4D *)((byte *)pNewVvdHdr+pNewVvdHdr->tangentDataStart) + target,
-				(Vector4D *)((byte *)pTempVvdHdr+pTempVvdHdr->tangentDataStart) + pFixupTable[i].sourceVertexID,
+				(byte*)((Vector4D *)((byte *)pNewVvdHdr+pNewVvdHdr->tangentDataStart) + target),
+				(byte*)((Vector4D *)((byte *)pTempVvdHdr+pTempVvdHdr->tangentDataStart) + pFixupTable[i].sourceVertexID),
 				pFixupTable[i].numVertexes*sizeof(Vector4D) );
 		}
 
diff --git a/public/tier0/platform.h b/public/tier0/platform.h
index 83b48b9886..07a3d661c5 100644
--- a/public/tier0/platform.h
+++ b/public/tier0/platform.h
@@ -1266,12 +1266,12 @@ struct CPUInformation
 
 	uint8 m_nLogicalProcessors;		// Number op logical processors.
 	uint8 m_nPhysicalProcessors;	// Number of physical processors
-	
+
 	bool m_bSSE3 : 1,
 		 m_bSSSE3 : 1,
 		 m_bSSE4a : 1,
 		 m_bSSE41 : 1,
-		 m_bSSE42 : 1;	
+		 m_bSSE42 : 1;
 
 	int64 m_Speed;						// In cycles per second.
 
@@ -1280,7 +1280,7 @@ struct CPUInformation
 	uint32 m_nModel;
 	uint32 m_nFeatures[3];
 
-	CPUInformation(): m_Size(0){}
+	CPUInformation() = default;
 };
 
 // Have to return a pointer, not a reference, because references are not compatible with the
diff --git a/public/tier1/memhelpers.h b/public/tier1/memhelpers.h
new file mode 100644
index 0000000000..898cafb2ef
--- /dev/null
+++ b/public/tier1/memhelpers.h
@@ -0,0 +1,32 @@
+// ======= Copyright nillerusr, 2022 =======
+
+// Helper аunctions for setting/сopying memory ( specially for non-POD types )
+// FUCK STL
+
+#ifndef MEMHELPERS_H
+#define MEMHELPERS_H
+
+namespace memutils
+{
+	template<typename T>
+	inline void copy( T *dest, const T *src, size_t n )
+	{
+		do
+		{
+			--n;
+			*(dest+n) = *(src+n);
+	        } while( n );
+	}
+
+	template<typename T>
+	inline void set( T *dest, T value, size_t n )
+	{
+		do
+		{
+			--n;
+			*(dest+n) = value;
+		} while( n );
+	}
+}
+
+#endif //MEMHELPERS_H
diff --git a/public/vphysics_interface.h b/public/vphysics_interface.h
index 11af63575d..3bbf35e719 100644
--- a/public/vphysics_interface.h
+++ b/public/vphysics_interface.h
@@ -1039,10 +1039,6 @@ struct fluidparams_t
 //-----------------------------------------------------------------------------
 struct springparams_t
 {
-	springparams_t()
-	{
-		memset( this, 0, sizeof(*this) );
-	}
 	float	constant;		// spring constant
 	float	naturalLength;// relaxed length
 	float	damping;		// damping factor
diff --git a/studiorender/r_studiolight.cpp b/studiorender/r_studiolight.cpp
index fefc06e172..2650b45304 100644
--- a/studiorender/r_studiolight.cpp
+++ b/studiorender/r_studiolight.cpp
@@ -36,7 +36,7 @@ int CopyLocalLightingState( int nMaxLights, LightDesc_t *pDest, int nLightCount,
 	for( int i = 0; i < nLightCount; i++ )
 	{
 		LightDesc_t *pLight = &pDest[i];
-		memcpy( pLight, &pSrc[i], sizeof( LightDesc_t ) );
+		*pLight = pSrc[i];
 		pLight->m_Flags = 0;
 		if( pLight->m_Attenuation0 != 0.0f )
 		{
@@ -539,4 +539,4 @@ void CStudioRenderContext::ComputeLightingConstDirectional( const Vector* pAmbie
 	// color from the ambient cube calculated in ComputeLightingCommon
 	int index = ComputeLightingCommon( pAmbient, lightCount, pLights, pt, normal, m_pLightPos, lighting );
 	R_LightEffectsWorldFunctionTableConstDirectional::functions[index]( pLights, m_pLightPos, normal, lighting, flDirectionalAmount );
-}
\ No newline at end of file
+}
diff --git a/studiorender/studiorendercontext.cpp b/studiorender/studiorendercontext.cpp
index 62a7c8d32d..78bccf7b1b 100644
--- a/studiorender/studiorendercontext.cpp
+++ b/studiorender/studiorendercontext.cpp
@@ -19,6 +19,7 @@
 #include "tier1/callqueue.h"
 #include "cmodel.h"
 #include "tier0/vprof.h"
+#include "tier1/memhelpers.h"
 
 // memdbgon must be the last include file in a .cpp file!!!
 #include "tier0/memdbgon.h"
@@ -1994,7 +1995,7 @@ void CStudioRenderContext::SetAmbientLightColors( const Vector *pColors )
 
 void CStudioRenderContext::SetAmbientLightColors( const Vector4D *pColors )
 {
-	memcpy( m_RC.m_LightBoxColors, pColors, 6 * sizeof(Vector4D) );
+	memutils::copy( &m_RC.m_LightBoxColors[0], pColors, 6 );
 
 	// FIXME: Would like to get this into the render thread, but there's systemic confusion
 	// about whether to set lighting state here or in the material system
diff --git a/tier0/cpu.cpp b/tier0/cpu.cpp
index a602347c40..f5c29fdaf4 100644
--- a/tier0/cpu.cpp
+++ b/tier0/cpu.cpp
@@ -498,9 +498,6 @@ const CPUInformation* GetCPUInformation()
 	if ( pi.m_Size == sizeof(pi) )
 		return &pi;
 
-	// Redundant, but just in case the user somehow messes with the size.
-	memset(&pi, 0x0, sizeof(pi));
-
 	// Fill out the structure, and return it:
 	pi.m_Size = sizeof(pi);
 
diff --git a/tier2/camerautils.cpp b/tier2/camerautils.cpp
index 41d1dc741d..4395b21ca4 100644
--- a/tier2/camerautils.cpp
+++ b/tier2/camerautils.cpp
@@ -69,7 +69,13 @@ void ComputeProjectionMatrix( VMatrix *pCameraToProjection, const Camera_t &came
 	float halfHeight = tan( flFOV * M_PI / 360.0 );
 	float halfWidth = flApsectRatio * halfHeight;
 #endif
-	memset( pCameraToProjection, 0, sizeof( VMatrix ) );
+	pCameraToProjection->Init(
+		0.f, 0.f, 0.f, 0.f,
+		0.f, 0.f, 0.f, 0.f,
+		0.f, 0.f, 0.f, 0.f,
+		0.f, 0.f, 0.f, 0.f
+	);
+
 	pCameraToProjection->m[0][0]  = 1.0f / halfWidth;
 	pCameraToProjection->m[1][1]  = 1.0f / halfHeight;
 	pCameraToProjection->m[2][2] = flZFar / ( flZNear - flZFar );