@@ -22,82 +22,122 @@ void CCameraRPCs::LoadFunctions()
2222
2323void CCameraRPCs::SetCameraMatrix (NetBitStreamInterface& bitStream)
2424{
25+ if (!m_pCamera)
26+ return ;
27+
2528 if (bitStream.Version () >= 0x5E )
2629 {
2730 uchar ucTimeContext;
28- if (bitStream.Read (ucTimeContext))
29- m_pCamera->SetSyncTimeContext (ucTimeContext);
31+ if (!bitStream.Read (ucTimeContext))
32+ {
33+ return ;
34+ }
35+ m_pCamera->SetSyncTimeContext (ucTimeContext);
3036 }
3137
3238 CVector vecPosition, vecLookAt;
3339 float fRoll = 0 .0f ;
3440 float fFOV = 70 .0f ;
35- if (bitStream.Read (vecPosition.fX ) && bitStream.Read (vecPosition.fY ) && bitStream.Read (vecPosition.fZ ) && bitStream.Read (vecLookAt.fX ) &&
36- bitStream.Read (vecLookAt.fY ) && bitStream.Read (vecLookAt.fZ ))
41+
42+ if (!bitStream.Read (vecPosition.fX ) || !bitStream.Read (vecPosition.fY ) || !bitStream.Read (vecPosition.fZ ) ||
43+ !bitStream.Read (vecLookAt.fX ) || !bitStream.Read (vecLookAt.fY ) || !bitStream.Read (vecLookAt.fZ ))
3744 {
38- bitStream. Read ( fRoll );
39- bitStream. Read ( fFOV );
45+ return ; // Invalid data
46+ }
4047
41- if (!m_pCamera-> IsInFixedMode ())
42- m_pCamera-> ToggleCameraFixedMode ( true );
48+ bitStream. Read ( fRoll );
49+ bitStream. Read ( fFOV );
4350
44- // Put the camera there
45- m_pCamera->SetPosition (vecPosition);
46- m_pCamera->SetFixedTarget (vecLookAt, fRoll );
47- m_pCamera->SetFOV (fFOV );
51+ // Validate float values to prevent potential issues
52+ if (!std::isfinite (fRoll ) || !std::isfinite (fFOV ) ||
53+ !std::isfinite (vecPosition.fX ) || !std::isfinite (vecPosition.fY ) || !std::isfinite (vecPosition.fZ ) ||
54+ !std::isfinite (vecLookAt.fX ) || !std::isfinite (vecLookAt.fY ) || !std::isfinite (vecLookAt.fZ ))
55+ {
56+ return ; // Invalid float values (NaN, infinity, etc.)
4857 }
58+
59+ // Validate camera pointer before use
60+ if (!m_pCamera)
61+ return ;
62+
63+ if (!m_pCamera->IsInFixedMode ())
64+ m_pCamera->ToggleCameraFixedMode (true );
65+
66+ // Put the camera there
67+ m_pCamera->SetPosition (vecPosition);
68+ m_pCamera->SetFixedTarget (vecLookAt, fRoll );
69+ m_pCamera->SetFOV (fFOV );
4970}
5071
5172void CCameraRPCs::SetCameraTarget (NetBitStreamInterface& bitStream)
5273{
74+ if (!m_pCamera)
75+ return ;
76+
5377 if (bitStream.Version () >= 0x5E )
5478 {
5579 uchar ucTimeContext;
56- if (bitStream.Read (ucTimeContext))
57- m_pCamera->SetSyncTimeContext (ucTimeContext);
80+ if (!bitStream.Read (ucTimeContext))
81+ {
82+ return ;
83+ }
84+ m_pCamera->SetSyncTimeContext (ucTimeContext);
5885 }
5986
6087 ElementID targetID;
61- if (bitStream.Read (targetID))
88+ if (!bitStream.Read (targetID))
89+ return ;
90+
91+ // Validate camera pointer
92+ if (!m_pCamera)
93+ return ;
94+
95+ CClientEntity* pEntity = CElementIDs::GetElement (targetID);
96+ if (!pEntity)
97+ return ;
98+
99+ // Check if entity is being deleted - critical memory safety check
100+ if (pEntity->IsBeingDeleted ())
101+ return ;
102+
103+ switch (pEntity->GetType ())
62104 {
63- CClientEntity* pEntity = CElementIDs::GetElement (targetID);
64- if (pEntity)
105+ case CCLIENTPLAYER:
65106 {
66- switch (pEntity->GetType ())
107+ CClientPlayer* pPlayer = static_cast <CClientPlayer*>(pEntity);
108+ if (pPlayer->IsLocalPlayer ())
67109 {
68- case CCLIENTPLAYER:
69- {
70- CClientPlayer* pPlayer = static_cast <CClientPlayer*>(pEntity);
71- if (pPlayer->IsLocalPlayer ())
72- {
73- // Return the focus to the local player
74- m_pCamera->SetFocusToLocalPlayer ();
75- }
76- else
77- {
78- // Put the focus on that player
79- m_pCamera->SetFocus (pPlayer, MODE_CAM_ON_A_STRING, false );
80- }
81- break ;
82- }
83- case CCLIENTPED:
84- case CCLIENTVEHICLE:
85- {
86- m_pCamera->SetFocus (pEntity, MODE_CAM_ON_A_STRING, false );
87- break ;
88- }
89- default :
90- return ;
110+ // Return the focus to the local player
111+ m_pCamera->SetFocusToLocalPlayer ();
91112 }
113+ else
114+ {
115+ // Put the focus on that player
116+ m_pCamera->SetFocus (pPlayer, MODE_CAM_ON_A_STRING, false );
117+ }
118+ break ;
119+ }
120+ case CCLIENTPED:
121+ case CCLIENTVEHICLE:
122+ {
123+ m_pCamera->SetFocus (pEntity, MODE_CAM_ON_A_STRING, false );
124+ break ;
92125 }
126+ default :
127+ // Invalid entity type for camera target
128+ return ;
93129 }
94130}
95131
96132void CCameraRPCs::SetCameraInterior (NetBitStreamInterface& bitStream)
97133{
98- // Read out the camera mode
134+ // Read out the camera interior
99135 unsigned char ucInterior;
100- if (bitStream.Read (ucInterior))
136+ if (!bitStream.Read (ucInterior))
137+ return ;
138+
139+ // Validate game pointer before use
140+ if (g_pGame && g_pGame->GetWorld ())
101141 {
102142 g_pGame->GetWorld ()->SetCurrentArea (ucInterior);
103143 }
@@ -107,26 +147,41 @@ void CCameraRPCs::FadeCamera(NetBitStreamInterface& bitStream)
107147{
108148 unsigned char ucFadeIn;
109149 float fFadeTime = 1 .0f ;
110- if (bitStream.Read (ucFadeIn) && bitStream.Read (fFadeTime ))
111- {
112- g_pClientGame->SetInitiallyFadedOut (false );
150+
151+ if (!bitStream.Read (ucFadeIn) || !bitStream.Read (fFadeTime ))
152+ return ;
153+
154+ // Validate pointers before use
155+ if (!m_pCamera || !g_pClientGame)
156+ return ;
113157
114- if (ucFadeIn)
158+ g_pClientGame->SetInitiallyFadedOut (false );
159+
160+ if (ucFadeIn)
161+ {
162+ m_pCamera->FadeIn (fFadeTime );
163+
164+ // Validate game and HUD pointers
165+ if (g_pGame && g_pGame->GetHud ())
115166 {
116- m_pCamera->FadeIn (fFadeTime );
117167 g_pGame->GetHud ()->SetComponentVisible (HUD_AREA_NAME, !g_pClientGame->GetHudAreaNameDisabled ());
118168 }
119- else
120- {
121- unsigned char ucRed = 0 ;
122- unsigned char ucGreen = 0 ;
123- unsigned char ucBlue = 0 ;
169+ }
170+ else
171+ {
172+ unsigned char ucRed = 0 ;
173+ unsigned char ucGreen = 0 ;
174+ unsigned char ucBlue = 0 ;
124175
125- if (bitStream.Read (ucRed) && bitStream.Read (ucGreen) && bitStream.Read (ucBlue))
126- {
127- m_pCamera->FadeOut (fFadeTime , ucRed, ucGreen, ucBlue);
128- g_pGame->GetHud ()->SetComponentVisible (HUD_AREA_NAME, false );
129- }
176+ if (!bitStream.Read (ucRed) || !bitStream.Read (ucGreen) || !bitStream.Read (ucBlue))
177+ return ;
178+
179+ m_pCamera->FadeOut (fFadeTime , ucRed, ucGreen, ucBlue);
180+
181+ // Validate game and HUD pointers
182+ if (g_pGame && g_pGame->GetHud ())
183+ {
184+ g_pGame->GetHud ()->SetComponentVisible (HUD_AREA_NAME, false );
130185 }
131186 }
132187}
0 commit comments