Codechange: Split GetItem with GetOrCreateItem. (#10952)

`IniGroup::GetItem()` returns nullptr if the item does not exist, but does not if the create parameter is set to true. Resolve CodeQL warnings with `GetOrCreateItem()` which returns a reference to the item instead.
This commit is contained in:
PeterN
2023-06-05 19:29:52 +01:00
committed by GitHub
parent 3b1407d240
commit 64d6ad50f9
8 changed files with 51 additions and 41 deletions

View File

@@ -82,22 +82,32 @@ IniGroup::~IniGroup()
}
/**
* Get the item with the given name, and if it doesn't exist
* and create is true it creates a new item.
* Get the item with the given name.
* @param name name of the item to find.
* @param create whether to create an item when not found or not.
* @return the requested item or nullptr if not found.
*/
IniItem *IniGroup::GetItem(const std::string &name, bool create)
IniItem *IniGroup::GetItem(const std::string &name)
{
for (IniItem *item = this->item; item != nullptr; item = item->next) {
if (item->name == name) return item;
}
if (!create) return nullptr;
return nullptr;
}
/* otherwise make a new one */
return new IniItem(this, name);
/**
* Get the item with the given name, and if it doesn't exist create a new item.
* @param name name of the item to find.
* @return the requested item.
*/
IniItem &IniGroup::GetOrCreateItem(const std::string &name)
{
for (IniItem *item = this->item; item != nullptr; item = item->next) {
if (item->name == name) return *item;
}
/* Item doesn't exist, make a new one. */
return *(new IniItem(this, name));
}
/**