gsrvtarget: Silence false positive NULL pointer dereference

scan-build thinks there could be a `NULL` pointer dereference of
`t->data` here. It’s wrong, so add an assertion to try and help it
understand the control flow.

The loop is exited as soon as a target is found whose weight is greater
than or equal to a random value between 0 and the sum of all the weights
in the set of remaining targets in the loop. By definition, the last
target in the loop always satisfies this condition, so a target will
always be chosen, and hence `t` will never be `NULL` within the loop.

`t->data` will never be `NULL` by construction of the target list.

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

Helps: #1767
This commit is contained in:
Philip Withnall 2024-04-10 00:08:50 +01:00
parent 066298b6ef
commit a1ff120a98
No known key found for this signature in database
GPG Key ID: DCDF5885B1F3ED73

View File

@ -285,6 +285,7 @@ g_srv_target_list_sort (GList *targets)
val = g_random_int_range (0, sum + 1);
for (t = targets; ; t = t->next)
{
g_assert (t != NULL && t->data != NULL);
weight = ((GSrvTarget *)t->data)->weight;
if (weight >= val)
break;