bugfix(worldbuilder): Check list size before accessing first element in ObjectOptions::_FindOrDont#2306
Conversation
…in ObjectOptions::_FindOrDont.
Greptile Overview
|
| Filename | Overview |
|---|---|
| Generals/Code/Tools/WorldBuilder/src/ObjectOptions.cpp | Added empty list check to prevent debug assertion when accessing front element |
| GeneralsMD/Code/Tools/WorldBuilder/src/ObjectOptions.cpp | Added empty list check to prevent debug assertion when accessing front element |
Last reviewed commit: 2f5d827
Additional Comments (2)
Prompt To Fix With AIThis is a comment left during a code review.
Path: Generals/Code/Tools/WorldBuilder/src/ObjectOptions.cpp
Line: 428:428
Comment:
`GetChildItem` can return NULL when there are no children. Should add null check before pushing to list to prevent processing invalid handles.
```suggestion
HTREEITEM child = m_objectTreeView.GetChildItem(hItem);
if (child) {
itemsToEx.push_back(child);
}
```
How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix With AIThis is a comment left during a code review.
Path: GeneralsMD/Code/Tools/WorldBuilder/src/ObjectOptions.cpp
Line: 428:428
Comment:
`GetChildItem` can return NULL when there are no children. Should add null check before pushing to list to prevent processing invalid handles.
```suggestion
HTREEITEM child = m_objectTreeView.GetChildItem(hItem);
if (child) {
itemsToEx.push_back(child);
}
```
How can I resolve this? If you propose a fix, please make it concise. |
76d8bb1 to
2f5d827
Compare
| itemsToEx.push_back(startPoint); | ||
|
|
||
| while (itemsToEx.front()) { | ||
| while (!itemsToEx.empty() && itemsToEx.front()) { |
There was a problem hiding this comment.
Is HTREEITEM a pointer?
Maybe just do while (!itemsToEx.empty())? Why test for front() here?
There was a problem hiding this comment.
I agree, only testing for empty should resolve this here, as when not empty you should always have a front iterator.
There was a problem hiding this comment.
It's not clear to me if the code can handle a nullptr as element of itemsToEx, so this seemed to me the safest option.
There was a problem hiding this comment.
Is HTREEITEM a pointer?
Yes. typedef struct _TREEITEM *HTREEITEM;
There was a problem hiding this comment.
Is there any evidence that null is added into the container?
There was a problem hiding this comment.
https://learn.microsoft.com/en-us/cpp/mfc/reference/ctreectrl-class?view=msvc-170#getchilditem
https://learn.microsoft.com/en-us/cpp/mfc/reference/ctreectrl-class?view=msvc-170#getnextsiblingitem
can return a nullptr in theory. I don't know if that's possible in our use case.
There was a problem hiding this comment.
Perhaps prevent it from adding null to the list? What would be the point of a null entry in the list? Would it do anything?
There was a problem hiding this comment.
Perhaps prevent it from adding null to the list?
That requires 2 checks, or 3 if we can't guarantee that the function parameter is never null.
What would be the point of a null entry in the list? Would it do anything?
I'm not sure and frankly I'm not sure it's worth my time to find out. I'd assume that replacing the MFC code is going to happen if we decide to revamp the World Builder.
There was a problem hiding this comment.
I suggest add an assert for null in the loop, run World Builder, do whatever it takes to populate the list, then see if that assert ever hits. If it does not, then there is no null in the list and you can just keep the assert and remove the front test.
If you open the map Sand Serpent with the World Builder and click on the green object in the center of the blue circle, you get a debug assertion that accessing the front element of an empty list is not allowed. This PR fixes that.