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

@@ -23,7 +23,7 @@ extern void CheckExternalFiles();
* @param name the name of the item to fetch.
*/
#define fetch_metadata(name) \
item = metadata->GetItem(name, false); \
item = metadata->GetItem(name); \
if (item == nullptr || !item->value.has_value() || item->value->empty()) { \
Debug(grf, 0, "Base " SET_TYPE "set detail loading: {} field missing.", name); \
Debug(grf, 0, " Is {} readable for the user running OpenTTD?", full_filename); \
@@ -65,7 +65,7 @@ bool BaseSet<T, Tnum_files, Tsearch_in_tars>::FillSetDetails(IniFile *ini, const
fetch_metadata("version");
this->version = atoi(item->value->c_str());
item = metadata->GetItem("fallback", false);
item = metadata->GetItem("fallback");
this->fallback = (item != nullptr && item->value && *item->value != "0" && *item->value != "false");
/* For each of the file types we want to find the file, MD5 checksums and warning messages. */
@@ -75,7 +75,7 @@ bool BaseSet<T, Tnum_files, Tsearch_in_tars>::FillSetDetails(IniFile *ini, const
for (uint i = 0; i < Tnum_files; i++) {
MD5File *file = &this->files[i];
/* Find the filename first. */
item = files->GetItem(BaseSet<T, Tnum_files, Tsearch_in_tars>::file_names[i], false);
item = files->GetItem(BaseSet<T, Tnum_files, Tsearch_in_tars>::file_names[i]);
if (item == nullptr || (!item->value.has_value() && !allow_empty_filename)) {
Debug(grf, 0, "No " SET_TYPE " file for: {} (in {})", BaseSet<T, Tnum_files, Tsearch_in_tars>::file_names[i], full_filename);
return false;
@@ -93,7 +93,7 @@ bool BaseSet<T, Tnum_files, Tsearch_in_tars>::FillSetDetails(IniFile *ini, const
file->filename = path + filename;
/* Then find the MD5 checksum */
item = md5s->GetItem(filename, false);
item = md5s->GetItem(filename);
if (item == nullptr || !item->value.has_value()) {
Debug(grf, 0, "No MD5 checksum specified for: {} (in {})", filename, full_filename);
return false;
@@ -119,8 +119,8 @@ bool BaseSet<T, Tnum_files, Tsearch_in_tars>::FillSetDetails(IniFile *ini, const
}
/* Then find the warning message when the file's missing */
item = origin->GetItem(filename, false);
if (item == nullptr) item = origin->GetItem("default", false);
item = origin->GetItem(filename);
if (item == nullptr) item = origin->GetItem("default");
if (item == nullptr || !item->value.has_value()) {
Debug(grf, 1, "No origin warning message specified for: {}", filename);
file->missing_warning.clear();