From 237bb8efd43bcc12dd9594d3e182e548a206f09a Mon Sep 17 00:00:00 2001 From: SangYoun Kwak Date: Mon, 30 Dec 2024 17:36:08 +0900 Subject: [PATCH] dynamic-partitions: Add null check for builder lib The function FindGroup() in MetadataBuilder class can return nullptr if the name(passed by its parameter) is not exist in its group list(vector). To resolve this issue, null checking code is added for the return value of FindGroup() to its caller. Also for the object that gives the name also null-checked since it is passed by parameter an "can" be null. Practically, the return value of FindGroup() cannot be null because the passed name is always exist in this project. Also they are only used in this project. But it is good practice to check if pointer is null or not. Change-Id: I0c42f318d34afec8d1a3b289049a0fba530daa1d Signed-off-by: SangYoun Kwak --- src/dynamic-partitions/liblp/builder.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/dynamic-partitions/liblp/builder.cpp b/src/dynamic-partitions/liblp/builder.cpp index 1d86441..34aa7f5 100644 --- a/src/dynamic-partitions/liblp/builder.cpp +++ b/src/dynamic-partitions/liblp/builder.cpp @@ -545,8 +545,17 @@ auto MetadataBuilder::GetFreeRegions() const -> std::vector { bool MetadataBuilder::ValidatePartitionSizeChange(Partition* partition, uint64_t old_size, uint64_t new_size, bool force_check) { + if (partition == nullptr) { + LERROR << "Partition is nullptr"; + return false; + } + PartitionGroup* group = FindGroup(partition->group_name()); CHECK(group); + if (group == nullptr) { + LERROR << "Partition " << partition->name() << " does not exist"; + return false; + } if (!force_check && new_size <= old_size) { return true; @@ -883,6 +892,11 @@ bool MetadataBuilder::AlignSector(const LpMetadataBlockDevice& block_device, uin bool MetadataBuilder::ResizePartition(Partition* partition, uint64_t requested_size, const std::vector& free_region_hint) { + if (partition == nullptr) { + LERROR << "Partition is nullptr"; + return false; + } + // Align the space needed up to the nearest sector. uint64_t aligned_size; if (!AlignTo(requested_size, geometry_.logical_block_size, &aligned_size)) { -- 2.34.1