libdrgn: type: fix memcpy() undefined behavior

It's undefined behavior to pass NULL to memcpy() even if the length is
zero. See also commit a17215e984 ("libdrgn: dwarf_index: fix memcpy()
undefined behavior").

Signed-off-by: Omar Sandoval <osandov@osandov.com>
This commit is contained in:
Omar Sandoval 2021-04-02 10:56:37 -07:00
parent 7ca157316f
commit c9dc7fd574

View File

@ -1041,13 +1041,16 @@ drgn_type_with_byte_order_impl(struct drgn_type **type,
drgn_type_program(*type));
size_t num_enumerators =
drgn_type_num_enumerators(*type);
if (!drgn_type_enumerator_vector_reserve(&builder.enumerators,
num_enumerators)) {
drgn_enum_type_builder_deinit(&builder);
return &drgn_enomem;
if (num_enumerators) {
if (!drgn_type_enumerator_vector_reserve(&builder.enumerators,
num_enumerators)) {
drgn_enum_type_builder_deinit(&builder);
return &drgn_enomem;
}
memcpy(&builder.enumerators.data,
drgn_type_enumerators(*type),
num_enumerators * sizeof(struct drgn_type_enumerator));
}
memcpy(&builder.enumerators.data, drgn_type_enumerators(*type),
num_enumerators * sizeof(struct drgn_type_enumerator));
err = drgn_enum_type_create(&builder, drgn_type_tag(*type),
compatible_type,
drgn_type_language(*type), type);