dynamic-partitions: Modify to cast before multiply 89/307189/1 accepted/tizen/unified/20240307.075324 accepted/tizen/unified/toolchain/20240311.065624 accepted/tizen/unified/x/20240308.033508
authorSangYoun Kwak <sy.kwak@samsung.com>
Wed, 6 Mar 2024 02:27:50 +0000 (11:27 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Wed, 6 Mar 2024 02:45:39 +0000 (11:45 +0900)
Previously, some variables(which are int32_t) are multiplied then casted
to int64_t. It is pointless since it can cause casting after overflow.
To prevent overflow, expression is modified to cast first operand and
then perform multiplication.

This solves a bug reported by SVACE with the following WGIDs:
701511 701817 701826 711926

Change-Id: Ic51b4f608d36850162e42330a7162e41e4b73f6e
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
src/dynamic-partitions/liblp/utility.cpp
src/dynamic-partitions/testlib/metadataio.cpp

index 7884010421f7ef59a032c6ef1556718864bb38ad..5a74aa733f172bbb573142c7ba4c2a0a08d30524 100644 (file)
@@ -54,7 +54,7 @@ int64_t GetBackupGeometryOffset() {
 int64_t GetPrimaryMetadataOffset(const LpMetadataGeometry& geometry, uint32_t slot_number) {
     CHECK(slot_number < geometry.metadata_slot_count);
     int64_t offset = LP_PARTITION_RESERVED_BYTES + (LP_METADATA_GEOMETRY_SIZE * 2) +
-                     geometry.metadata_max_size * slot_number;
+                     int64_t(geometry.metadata_max_size) * slot_number;
     return offset;
 }
 
@@ -62,12 +62,12 @@ int64_t GetBackupMetadataOffset(const LpMetadataGeometry& geometry, uint32_t slo
     CHECK(slot_number < geometry.metadata_slot_count);
     int64_t start = LP_PARTITION_RESERVED_BYTES + (LP_METADATA_GEOMETRY_SIZE * 2) +
                     int64_t(geometry.metadata_max_size) * geometry.metadata_slot_count;
-    return start + int64_t(geometry.metadata_max_size * slot_number);
+    return start + int64_t(geometry.metadata_max_size) * slot_number;
 }
 
 uint64_t GetTotalMetadataSize(uint32_t metadata_max_size, uint32_t max_slots) {
     return LP_PARTITION_RESERVED_BYTES +
-           (LP_METADATA_GEOMETRY_SIZE + metadata_max_size * max_slots) * 2;
+           (LP_METADATA_GEOMETRY_SIZE + int64_t(metadata_max_size) * max_slots) * 2;
 }
 
 const LpMetadataBlockDevice* GetMetadataSuperBlockDevice(const LpMetadata& metadata) {
index 441417496fe024e34f1dab5cd54eac5be19e61ab..66e365ccba2b525225f925fe7ef48c67a7c892df 100644 (file)
@@ -33,7 +33,7 @@ void addPartitions(android::fs_mgr::LpMetadata &metadata, int number_of_groups,
             e.num_sectors = 10240;
             e.target_type = 0;
             // add offset equall to idx * PART_SIZE as a number of 512B sectors (1024 * 1024 / 512)
-            e.target_data = 2048 + idx * PART_SIZE * 2048;
+            e.target_data = 2048 + uint64_t(idx) * PART_SIZE * 2048;
             e.target_source = 0;
 
             metadata.extents.push_back(e);
@@ -439,4 +439,4 @@ void metadataio::readMetadata(android::fs_mgr::LpMetadata &m, const std::string
     std::stringstream metadata_stream;
     metadata_stream << s;
     metadata_stream >> m;
-}
\ No newline at end of file
+}