gvariant: Zero-initialise GVariantBuilder children under static analysis

scan-build can’t link the types used in `g_variant_builder_init()` with
the (same) types used in `g_variant_builder_end()`, so ends up assuming
that the children have not been initialised.

At runtime, this is prevented by the precondition checks on
`GVSB()->offset` in `g_variant_builder_end()`. scan-build doesn’t notice
that though.

Avoid a scan-build warning by zero-initialising the children array when
running static analysis. Doing this unconditionally would be an
unnecessary performance hit.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall 2022-06-07 11:08:14 +01:00
parent 423bcab9f4
commit 504727c317

View File

@ -3484,8 +3484,19 @@ g_variant_builder_init (GVariantBuilder *builder,
g_assert_not_reached ();
}
#ifdef G_ANALYZER_ANALYZING
/* Static analysers cant couple the code in g_variant_builder_init() to the
* code in g_variant_builder_end() by GVariantType, so end up assuming that
* @offset and @children mismatch and that uninitialised memory is accessed
* from @children. At runtime, this is caught by the preconditions at the top
* of g_variant_builder_end(). Help the analyser by zero-initialising the
* memory to avoid a false positive. */
GVSB(builder)->children = g_new0 (GVariant *,
GVSB(builder)->allocated_children);
#else
GVSB(builder)->children = g_new (GVariant *,
GVSB(builder)->allocated_children);
#endif
}
static void