garray: Add assertions to help static analysis

In both these cases, the static analyser (Coverity) was worrying that
the array `data`/`pdata` wasn’t allocated before an element was written
to. That was a false positive: all the necessary conditions are met in
both cases for `g_{ptr_,}array_maybe_expand()` to always allocate the
array.

But it makes things a bit easier for the analyser if we add an assertion
to double-check that.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Coverity CID: #1474426, #1489512
This commit is contained in:
Philip Withnall 2022-11-29 12:16:11 +00:00
parent 5f945e5464
commit 8b863cfd78

View File

@ -285,6 +285,7 @@ g_array_sized_new (gboolean zero_terminated,
if (array->zero_terminated || reserved_size != 0)
{
g_array_maybe_expand (array, reserved_size);
g_assert (array->data != NULL);
g_array_zero_terminate (array);
}
@ -1120,7 +1121,10 @@ ptr_array_new (guint reserved_size,
if (G_LIKELY (reserved_size < G_MAXUINT) &&
null_terminated)
reserved_size++;
g_ptr_array_maybe_expand (array, reserved_size);
g_assert (array->pdata != NULL);
if (null_terminated)
{
/* don't use ptr_array_maybe_null_terminate(). It helps the compiler