dynamic-partitions: Add null check for builder lib 10/317310/1
authorSangYoun Kwak <sy.kwak@samsung.com>
Mon, 30 Dec 2024 08:36:08 +0000 (17:36 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Mon, 30 Dec 2024 08:36:08 +0000 (17:36 +0900)
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 <sy.kwak@samsung.com>
src/dynamic-partitions/liblp/builder.cpp

index 1d864410079da73272d87e8b4cb02c23f8048247..34aa7f56293a459256c4a38038aef447c519de3c 100644 (file)
@@ -545,8 +545,17 @@ auto MetadataBuilder::GetFreeRegions() const -> std::vector<Interval> {
 
 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<Interval>& 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)) {