Skip to content

Commit 2296d7d

Browse files
committed
check for null item array
1 parent 36d6fbe commit 2296d7d

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

code/globalincs/utility.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ int count_items_with_field(const VECTOR_T& item_vector, FIELD_T ITEM_T::* field,
233233
template <typename ITEM_T, typename FIELD_T>
234234
int count_items_with_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const char* str)
235235
{
236+
if (item_array == nullptr)
237+
return 0;
238+
236239
int count = 0;
237240
for (int i = 0; i < num_items; ++i)
238241
{
@@ -250,6 +253,9 @@ int count_items_with_string(const ITEM_T* item_array, int num_items, FIELD_T ITE
250253
template <typename ITEM_T, typename FIELD_T>
251254
int count_items_with_ptr_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const char* str)
252255
{
256+
if (item_array == nullptr)
257+
return 0;
258+
253259
int count = 0;
254260
for (int i = 0; i < num_items; ++i)
255261
{
@@ -267,6 +273,9 @@ int count_items_with_ptr_string(const ITEM_T* item_array, int num_items, FIELD_T
267273
template <typename ITEM_T, typename FIELD_T>
268274
int count_items_with_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const SCP_string& str)
269275
{
276+
if (item_array == nullptr)
277+
return 0;
278+
270279
int count = 0;
271280
for (int i = 0; i < num_items; ++i)
272281
if (lcase_equal(item_array[i].*field, str))
@@ -278,7 +287,7 @@ int count_items_with_string(const ITEM_T* item_array, int num_items, FIELD_T ITE
278287
template <typename ITEM_T>
279288
int count_items_with_string(const ITEM_T* item_array, int num_items, SCP_string ITEM_T::* field, const char* str)
280289
{
281-
if (str == nullptr)
290+
if (item_array == nullptr || str == nullptr)
282291
return 0;
283292

284293
return count_items_with_string(item_array, num_items, field, SCP_string(str));
@@ -287,6 +296,9 @@ int count_items_with_string(const ITEM_T* item_array, int num_items, SCP_string
287296
template <typename ITEM_T, typename FIELD_T>
288297
int count_items_with_field(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const FIELD_T& search)
289298
{
299+
if (item_array == nullptr)
300+
return 0;
301+
290302
int count = 0;
291303
for (int i = 0; i < num_items; ++i)
292304
if (item_array[i].*field == search)
@@ -375,6 +387,9 @@ int find_item_with_field(const VECTOR_T& item_vector, FIELD_T ITEM_T::* field, c
375387
template <typename ITEM_T, typename FIELD_T>
376388
int find_item_with_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const char* str)
377389
{
390+
if (item_array == nullptr)
391+
return -1;
392+
378393
for (int i = 0; i < num_items; ++i)
379394
{
380395
if (str == nullptr && item_array[i].*field == nullptr)
@@ -391,6 +406,9 @@ int find_item_with_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_
391406
template <typename ITEM_T, typename FIELD_T>
392407
int find_item_with_ptr_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const char* str)
393408
{
409+
if (item_array == nullptr)
410+
return -1;
411+
394412
for (int i = 0; i < num_items; ++i)
395413
{
396414
if (str == nullptr && (item_array[i].*field).get() == nullptr)
@@ -407,6 +425,9 @@ int find_item_with_ptr_string(const ITEM_T* item_array, int num_items, FIELD_T I
407425
template <typename ITEM_T, typename FIELD_T>
408426
int find_item_with_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const SCP_string& str)
409427
{
428+
if (item_array == nullptr)
429+
return -1;
430+
410431
for (int i = 0; i < num_items; ++i)
411432
if (lcase_equal(item_array[i].*field, str))
412433
return i;
@@ -417,7 +438,7 @@ int find_item_with_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_
417438
template <typename ITEM_T>
418439
int find_item_with_string(const ITEM_T* item_array, int num_items, SCP_string ITEM_T::* field, const char* str)
419440
{
420-
if (str == nullptr)
441+
if (item_array == nullptr || str == nullptr)
421442
return -1;
422443

423444
return find_item_with_string(item_array, num_items, field, str);
@@ -426,6 +447,9 @@ int find_item_with_string(const ITEM_T* item_array, int num_items, SCP_string IT
426447
template <typename ITEM_T, typename FIELD_T>
427448
int find_item_with_field(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const FIELD_T& search)
428449
{
450+
if (item_array == nullptr)
451+
return -1;
452+
429453
for (int i = 0; i < num_items; ++i)
430454
if (item_array[i].*field == search)
431455
return i;

0 commit comments

Comments
 (0)