commonlib/device_tree: Fix memory leak in fdt_unflatten()

When the passed `blob` is not a valid FDT, the memory allocated for
`tree` should be freed. Move the allocation after the fdt_is_valid()
check to avoid the problem.

Also remove the unnecessary cast to 'const struct fdt_header *'.

Change-Id: If591172cd511ae2a1ca9c26f2addef8d67fd0b69
Signed-off-by: Yu-Ping Wu <yupingso@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/88948
Reviewed-by: Nicholas Sudsgaard <devel+coreboot@nsudsgaard.com>
Reviewed-by: Paul Menzel <paulepanter@mailbox.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Patrick Rudolph <patrick.rudolph@9elements.com>
This commit is contained in:
Yu-Ping Wu 2025-08-26 16:43:03 +08:00 committed by Yu-Ping Wu
commit 8a52418e9a

View file

@ -766,13 +766,13 @@ bool fdt_is_valid(const void *blob)
struct device_tree *fdt_unflatten(const void *blob)
{
struct device_tree *tree = xzalloc(sizeof(*tree));
const struct fdt_header *header = (const struct fdt_header *)blob;
tree->header = header;
if (!fdt_is_valid(blob))
return NULL;
const struct fdt_header *header = blob;
struct device_tree *tree = xzalloc(sizeof(*tree));
tree->header = header;
uint32_t struct_offset = be32toh(header->structure_offset);
uint32_t strings_offset = be32toh(header->strings_offset);
uint32_t reserve_offset = be32toh(header->reserve_map_offset);