Skip to content

[ZH] Check vector size before accessing first element in World Builder MapObjectProps and ScorchOptions #1117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Caball009
Copy link

@Caball009 Caball009 commented Jun 19, 2025

Reproduction steps: Start World Builder (debug build) and tick "Enabled" in the "Object Properties" window. This will result in a debug assertion: "front() called on empty vector".

Notice how a pointer to the first element of m_allSelectedDicts is taken without checking whether the vector is empty or not (line 1436):

/// Move data from dialog controls to object
void MapObjectProps::_SelectableToDict(void)
{
getAllSelectedDicts();
CButton *owner = (CButton*) GetDlgItem(IDC_MAPOBJECT_Selectable);
Bool isChecked = (owner->GetCheck() == 1);
Bool isTristate = (owner->GetCheck() == 2);
CWorldBuilderDoc* pDoc = CWorldBuilderDoc::GetActiveDoc();
if ( pDoc != NULL )
{
Dict newDict;
if (isTristate) {
newDict.remove(TheKey_objectSelectable);
} else {
newDict.setBool(TheKey_objectSelectable, isChecked);
}
DictItemUndoable *pUndo = new DictItemUndoable(&m_allSelectedDicts.front(), newDict, newDict.getNthKey(0), m_allSelectedDicts.size());
pDoc->AddAndDoUndoable(pUndo);
REF_PTR_RELEASE(pUndo); // belongs to pDoc now.
// Update is called by Do
}
}

I chose to not add size checks everywhere but keep the code functionally the same except calling front() on an empty vector. As long as the vector is empty, the code in DictItemUndoable::DictItemUndoable won't attempt to dereference the front pointer so it can be NULL.

Would need to be replicated in Generals.

TODO

  • Replicate in Generals

@Caball009 Caball009 added Bug Something is not working right, typically is user facing Minor Severity: Minor < Major < Critical < Blocker WorldBuilder Relates to World Builder labels Jun 19, 2025
@@ -256,4 +256,9 @@ void ScorchOptions::getAllSelectedDicts(void)
}
m_allSelectedDicts.push_back(pMapObj->getProperties());
}
}

Dict** ScorchOptions::getFrontSelectedDicts()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix function name and body.

Dict** ScorchOptions::getAllSelectedDictsData()
{
#if defined(USING_STLPORT) || __cplusplus < 201103L
  return !m_allSelectedDicts.empty() ? &m_allSelectedDicts.front() : NULL;
#else
  return m_allSelectedDicts.data();
#endif
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed and ported to Generals.

@xezon xezon added this to the Code foundation build up milestone Jun 20, 2025
Copy link

@xezon xezon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@@ -1740,6 +1740,15 @@ void MapObjectProps::InitSound(void)

} // end InitSound

Dict** MapObjectProps::getAllSelectedDictsData()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code placed at a consistent location with Generals?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something is not working right, typically is user facing Minor Severity: Minor < Major < Critical < Blocker WorldBuilder Relates to World Builder
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants