Skip to content

Commit 45f50b9

Browse files
committed
To avoid vector end iterator dereference (this is undefined behaviour). And code simplification.
1 parent fcf0cf0 commit 45f50b9

File tree

13 files changed

+50
-69
lines changed

13 files changed

+50
-69
lines changed

src/Layers/xrRender/DetailManager.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,9 @@ void CDetailManager::UpdateVisibleM()
353353
float R = objects[sp.id]->bv_sphere.R;
354354
float Rq_drcp = R * R * dist_sq_rcp; // reordered expression for 'ssa' calc
355355

356-
SlotItem **siIT = &(*sp.items.begin()), **siEND = &(*sp.items.end());
357-
for (; siIT != siEND; siIT++)
356+
for(auto &siIT : sp.items)
358357
{
359-
SlotItem& Item = *(*siIT);
358+
SlotItem& Item = *siIT;
360359
float scale = Item.scale_calculated = Item.scale * alpha_i;
361360
float ssa = scale * scale * Rq_drcp;
362361
if (ssa < r_ssaDISCARD)
@@ -367,7 +366,7 @@ void CDetailManager::UpdateVisibleM()
367366
if (ssa > r_ssaCHEAP)
368367
vis_id = Item.vis_ID;
369368

370-
sp.r_items[vis_id].push_back(*siIT);
369+
sp.r_items[vis_id].push_back(siIT);
371370

372371
// 2 visible[vis_id][sp.id].push_back(&Item);
373372
}

src/Layers/xrRender/HOM.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ void CHOM::Render_DB(CFrustum& base)
169169
return;
170170

171171
// Prepare
172-
CDB::RESULT* it = xrc.r_begin();
173-
CDB::RESULT* end = xrc.r_end();
172+
auto it = xrc.r_get()->begin();
173+
auto end = xrc.r_get()->end();
174174

175175
Fvector COP = Device.vCameraPosition;
176176
end = std::remove_if(it, end, pred_fb(m_pTris));
@@ -185,10 +185,10 @@ void CHOM::Render_DB(CFrustum& base)
185185
stats.VisibleTriangleCount = 0;
186186

187187
// Perfrom selection, sorting, culling
188-
for (; it != end; it++)
188+
for (auto &it : *xrc.r_get())
189189
{
190190
// Control skipping
191-
occTri& T = m_pTris[it->id];
191+
occTri& T = m_pTris[it.id];
192192
u32 next = _frame + ::Random.randI(3, 10);
193193

194194
// Test for good occluder - should be improved :)
@@ -199,7 +199,7 @@ void CHOM::Render_DB(CFrustum& base)
199199
}
200200

201201
// Access to triangle vertices
202-
CDB::TRI& t = m_pModel->get_tris()[it->id];
202+
CDB::TRI& t = m_pModel->get_tris()[it.id];
203203
Fvector* v = m_pModel->get_verts();
204204
src.clear();
205205
dst.clear();

src/Layers/xrRender/r__dsgraph_render.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ void __fastcall mapNormal_Render(mapNormalItems& N)
2626
{
2727
// *** DIRECT ***
2828
std::sort(N.begin(), N.end(), cmp_normal_items);
29-
_NormalItem *I = &*N.begin(), *E = &*N.end();
30-
for (; I != E; I++)
29+
for(auto &Ni : N)
3130
{
32-
_NormalItem& Ni = *I;
3331
float LOD = calcLOD(Ni.ssa, Ni.pVisual->vis.sphere.R);
3432
#ifdef USE_DX11
3533
RCache.LOD.set_LOD(LOD);
@@ -44,10 +42,8 @@ void __fastcall mapMatrix_Render(mapMatrixItems& N)
4442
{
4543
// *** DIRECT ***
4644
std::sort(N.begin(), N.end(), cmp_matrix_items);
47-
_MatrixItem *I = &*N.begin(), *E = &*N.end();
48-
for (; I != E; I++)
45+
for (auto &Ni : N)
4946
{
50-
_MatrixItem& Ni = *I;
5147
RCache.set_xform_world(Ni.Matrix);
5248
RImplementation.apply_object(Ni.pObject);
5349
RImplementation.apply_lmaterial();

src/Layers/xrRenderPC_R1/LightShadows.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,11 @@ void CLightShadows::render()
491491

492492
// Clip polys by frustum
493493
tess.clear();
494-
for (CDB::RESULT* p = xrc.r_begin(); p != xrc.r_end(); p++)
494+
for (auto &p : *xrc.r_get())
495495
{
496-
VERIFY((p->id >= 0) && (p->id < DB->get_tris_count()));
496+
VERIFY((p.id >= 0) && (p.id < DB->get_tris_count()));
497497
//
498-
CDB::TRI& t = TRIS[p->id];
498+
CDB::TRI& t = TRIS[p.id];
499499
if (t.suppress_shadows)
500500
continue;
501501
sPoly A, B;

src/xrCDB/xrCDB.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ class XRCDB_API COLLIDER
144144
void frustum_query(const MODEL* m_def, const CFrustum& F);
145145

146146
ICF RESULT* r_begin() { return &*rd.begin(); };
147-
ICF RESULT* r_end() { return &*rd.end(); };
147+
//ICF RESULT* r_end() { return &*rd.end(); };
148+
ICF xr_vector<RESULT>* r_get() { return &rd; };
148149
RESULT& r_add();
149150
void r_free();
150151
ICF int r_count() { return rd.size(); };

src/xrCDB/xrXRC.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class XRCDB_API xrXRC
7272
}
7373

7474
IC CDB::RESULT* r_begin() { return CL.r_begin(); };
75-
IC CDB::RESULT* r_end() { return CL.r_end(); };
75+
//IC CDB::RESULT* r_end() { return CL.r_end(); };
76+
IC xr_vector<CDB::RESULT>* r_get() { return CL.r_get(); };
7677
IC void r_free() { CL.r_free(); }
7778
IC int r_count() { return CL.r_count(); };
7879
IC void r_clear() { CL.r_clear(); };

src/xrCDB/xr_area_query.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ bool CObjectSpace::BoxQuery(Fvector const& box_center, Fvector const& box_z_axis
4242

4343
if (out_tris)
4444
{
45-
for (CDB::RESULT* result = xrc.r_begin(); result != xrc.r_end(); ++result)
45+
for (auto &result : *xrc.r_get())
4646
{
47-
out_tris->push_back(result->verts[0]);
48-
out_tris->push_back(result->verts[1]);
49-
out_tris->push_back(result->verts[2]);
47+
out_tris->push_back(result.verts[0]);
48+
out_tris->push_back(result.verts[1]);
49+
out_tris->push_back(result.verts[2]);
5050
}
5151
}
5252

src/xrCDB/xr_area_raypick.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,9 @@ BOOL CObjectSpace::_RayQuery2(collide::rq_results& r_dest, const collide::ray_de
206206
{
207207
xrc.ray_options(R.flags);
208208
xrc.ray_query(&Static, R.start, R.dir, R.range);
209-
if (xrc.r_count())
210-
{
211-
CDB::RESULT* _I = xrc.r_begin();
212-
CDB::RESULT* _E = xrc.r_end();
213-
for (; _I != _E; _I++)
214-
r_temp.append_result(rq_result().set(0, _I->range, _I->id));
215-
}
209+
210+
for(auto &i : *xrc.r_get())
211+
r_temp.append_result(rq_result().set(0, i.range, i.id));
216212
}
217213
// Test dynamic
218214
if (R.tgt & d_mask)
@@ -239,12 +235,10 @@ BOOL CObjectSpace::_RayQuery2(collide::rq_results& r_dest, const collide::ray_de
239235
if (r_temp.r_count())
240236
{
241237
r_temp.r_sort();
242-
collide::rq_result* _I = r_temp.r_begin();
243-
collide::rq_result* _E = r_temp.r_end();
244-
for (; _I != _E; _I++)
238+
for(auto &i : *r_temp.r_get())
245239
{
246-
r_dest.append_result(*_I);
247-
if (!(CB ? CB(*_I, user_data) : TRUE))
240+
r_dest.append_result(i);
241+
if (!(CB ? CB(i, user_data) : TRUE))
248242
return r_dest.r_count();
249243
if (R.flags & (CDB::OPT_ONLYNEAREST | CDB::OPT_ONLYFIRST))
250244
return r_dest.r_count();
@@ -332,12 +326,10 @@ BOOL CObjectSpace::_RayQuery3(collide::rq_results& r_dest, const collide::ray_de
332326
if (r_temp.r_count())
333327
{
334328
r_temp.r_sort();
335-
collide::rq_result* _I = r_temp.r_begin();
336-
collide::rq_result* _E = r_temp.r_end();
337-
for (; _I != _E; _I++)
329+
for (auto &i : *r_temp.r_get())
338330
{
339-
r_dest.append_result(*_I);
340-
if (!(CB ? CB(*_I, user_data) : TRUE))
331+
r_dest.append_result(i);
332+
if (!(CB ? CB(i, user_data) : TRUE))
341333
return r_dest.r_count();
342334
if (R.flags & CDB::OPT_ONLYFIRST)
343335
return r_dest.r_count();

src/xrCDB/xr_collide_defs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ class rq_results
181181

182182
IC int r_count() { return results.size(); }
183183
IC rq_result* r_begin() { return &*results.begin(); }
184-
IC rq_result* r_end() { return &*results.end(); }
184+
//IC rq_result* r_end() { return &*results.end(); }
185+
IC rqVec* r_get() { return &results; }
185186
IC void r_clear() { results.clear_not_free(); }
186187
IC void r_sort() { std::sort(results.begin(), results.end(), r_sort_pred); }
187188
IC rqVec& r_results() { return results; }

src/xrGame/ActorCameras.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,9 @@ ICF void calc_gl_point(Fvector& pt, const Fmatrix& xform, float radius, float an
123123
}
124124
ICF BOOL test_point(const Fvector& pt, xrXRC& xrc, const Fmatrix33& mat, const Fvector& ext)
125125
{
126-
CDB::RESULT* it = xrc.r_begin();
127-
CDB::RESULT* end = xrc.r_end();
128-
for (; it != end; it++)
126+
for (auto &it : *xrc.r_get())
129127
{
130-
CDB::RESULT& O = *it;
128+
CDB::RESULT& O = it;
131129
if (GMLib.GetMaterialByIdx(O.material)->Flags.is(SGameMtl::flPassable))
132130
continue;
133131
if (CDB::TestBBoxTri(mat, pt, ext, O.verts, FALSE))

0 commit comments

Comments
 (0)