gcc10: first_index_to_prune may be used uninitialized
When compiling cogl-1.22.0 with gcc 10.3.0 on NetBSD-9.99.81/amd64, I see a compilation error:
cogl-pipeline.c: In function ‘_cogl_pipeline_prune_to_n_layers’:
cogl-pipeline.c:1689:10: error: ‘state.first_index_to_prune’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
1689 | if (layer->index >= state.first_index_to_prune)
| ^
The corresponding code does not make it clear to me what a good initial value would be:
CoglPipelinePruneLayersInfo state;
...
state.keep_n = n;
state.current_pos = 0;
_cogl_pipeline_foreach_layer_internal (pipeline,
update_prune_layers_info_cb,
&state);
where
static CoglBool
update_prune_layers_info_cb (CoglPipelineLayer *layer, void *user_data)
{
CoglPipelinePruneLayersInfo *state = user_data;
if (state->current_pos == state->keep_n)
{
state->first_index_to_prune = layer->index;
return FALSE;
}
state->current_pos++;
return TRUE;
}
and then later
for (l = pipeline->layer_differences; l; l = next)
{
CoglPipelineLayer *layer = l->data;
next = l->next; /* we're modifying the list we're iterating */
if (layer->index >= state.first_index_to_prune)
_cogl_pipeline_remove_layer_difference (pipeline, layer, FALSE);
}