Skip to content

Commit 81c93ed

Browse files
committed
To avoid vector end iterator dereference (this is undefined behaviour). And code simplification.
1 parent 34f1b6a commit 81c93ed

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

src/Layers/xrRender/WallmarksEngine.cpp

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ CWallmarksEngine::~CWallmarksEngine()
7070
void CWallmarksEngine::clear()
7171
{
7272
{
73-
for (auto p_it = marks.begin(); p_it != marks.end(); p_it++)
73+
for (auto &p_it : marks)
7474
{
75-
for (auto m_it = (*p_it)->static_items.begin(); m_it != (*p_it)->static_items.end(); m_it++)
76-
static_wm_destroy(*m_it);
77-
xr_delete(*p_it);
75+
for (auto &m_it : p_it->static_items)
76+
static_wm_destroy(m_it);
77+
xr_delete(p_it);
7878
}
7979
marks.clear();
8080
}
@@ -110,13 +110,13 @@ void CWallmarksEngine::static_wm_render(CWallmarksEngine::static_wallmark* W, FV
110110
int aC = iFloor(a * 255.f);
111111
clamp(aC, 0, 255);
112112
u32 C = color_rgba(128, 128, 128, aC);
113-
FVF::LIT* S = &*W->verts.begin();
114-
FVF::LIT* E = &*W->verts.end();
115-
for (; S != E; S++, V++)
113+
114+
for (auto &i : W->verts)
116115
{
117-
V->p.set(S->p);
116+
V->p.set(i.p);
118117
V->color = C;
119-
V->t.set(S->t);
118+
V->t.set(i.t);
119+
V++;
120120
}
121121
}
122122
//--------------------------------------------------------------------------------
@@ -255,10 +255,8 @@ void CWallmarksEngine::AddWallmark_internal(
255255
Fbox bb;
256256
bb.invalidate();
257257

258-
FVF::LIT* I = &*W->verts.begin();
259-
FVF::LIT* E = &*W->verts.end();
260-
for (; I != E; I++)
261-
bb.modify(I->p);
258+
for (auto &i : W->verts)
259+
bb.modify(i.p);
262260
bb.getsphere(W->bounds.P, W->bounds.R);
263261
}
264262

@@ -268,15 +266,13 @@ void CWallmarksEngine::AddWallmark_internal(
268266
wm_slot* slot = FindSlot(hShader);
269267
if (slot)
270268
{
271-
auto it = slot->static_items.begin();
272-
auto end = slot->static_items.end();
273-
for (; it != end; it++)
269+
for (auto &it : slot->static_items)
274270
{
275-
static_wallmark* wm = *it;
271+
static_wallmark* wm = it;
276272
if (wm->bounds.P.similar(W->bounds.P, 0.02f))
277273
{ // replace
278274
static_wm_destroy(wm);
279-
*it = W;
275+
it = W;
280276
return;
281277
}
282278
}
@@ -394,16 +390,17 @@ void CWallmarksEngine::Render()
394390

395391
lock.Enter(); // Physics may add wallmarks in parallel with rendering
396392

397-
for (auto slot_it = marks.begin(); slot_it != marks.end(); slot_it++)
393+
for (auto &slot_it : marks)
398394
{
399395
u32 w_offset;
400396
FVF::LIT *w_verts, *w_start;
401397
BeginStream(hGeom, w_offset, w_verts, w_start);
402-
wm_slot* slot = *slot_it;
398+
wm_slot* slot = slot_it;
399+
403400
// static wallmarks
404-
for (auto w_it = slot->static_items.begin(); w_it != slot->static_items.end();)
401+
for (auto &w_it : slot->static_items)
405402
{
406-
static_wallmark* W = *w_it;
403+
static_wallmark* W = w_it;
407404
if (RImplementation.ViewBase.testSphere_dirty(W->bounds.P, W->bounds.R))
408405
{
409406
RImplementation.BasicStats.StaticWMCount++;
@@ -429,12 +426,9 @@ void CWallmarksEngine::Render()
429426
if (W->ttl <= EPS)
430427
{
431428
static_wm_destroy(W);
432-
*w_it = slot->static_items.back();
429+
w_it = slot->static_items.back();
433430
slot->static_items.pop_back();
434-
}
435-
else
436-
{
437-
w_it++;
431+
break;
438432
}
439433
}
440434
// Flush stream
@@ -443,10 +437,9 @@ void CWallmarksEngine::Render()
443437
BeginStream(hGeom, w_offset, w_verts, w_start);
444438

445439
// dynamic wallmarks
446-
for (xr_vector<intrusive_ptr<CSkeletonWallmark>>::iterator w_it = slot->skeleton_items.begin();
447-
w_it != slot->skeleton_items.end(); w_it++)
440+
for (auto &w_it : slot->skeleton_items)
448441
{
449-
intrusive_ptr<CSkeletonWallmark> W = *w_it;
442+
intrusive_ptr<CSkeletonWallmark> W = w_it;
450443
if (!W)
451444
{
452445
continue;

0 commit comments

Comments
 (0)