Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f1222908 authored by David Anderson's avatar David Anderson
Browse files

liblp: Always copy geometry to LpMetadata.

Callers of ParseMetadata must manually copy geometry to the final
LpMetadata structure, which is error-prone. Instead, force callers to
pass geometry to ParseMetadata to ensure it is always propagated.

Bug: N/A
Test: liblp_test gtest

Change-Id: I5b24b9d94ab1857db600c40bf6d3c9d8aaa47368
parent d1d06109
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -38,12 +38,7 @@ std::unique_ptr<LpMetadata> ReadFromImageFile(int fd) {
        PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset " << LP_METADATA_GEOMETRY_SIZE;
        return nullptr;
    }
    std::unique_ptr<LpMetadata> metadata = ParseMetadata(fd);
    if (!metadata) {
        return nullptr;
    }
    metadata->geometry = geometry;
    return metadata;
    return ParseMetadata(geometry, fd);
}

std::unique_ptr<LpMetadata> ReadFromImageFile(const char* file) {
+6 −9
Original line number Diff line number Diff line
@@ -171,7 +171,7 @@ static bool ValidateMetadataHeader(const LpMetadataHeader& header) {

// Parse and validate all metadata at the current position in the given file
// descriptor.
std::unique_ptr<LpMetadata> ParseMetadata(int fd) {
std::unique_ptr<LpMetadata> ParseMetadata(const LpMetadataGeometry& geometry, int fd) {
    // First read and validate the header.
    std::unique_ptr<LpMetadata> metadata = std::make_unique<LpMetadata>();
    if (!android::base::ReadFully(fd, &metadata->header, sizeof(metadata->header))) {
@@ -181,6 +181,7 @@ std::unique_ptr<LpMetadata> ParseMetadata(int fd) {
    if (!ValidateMetadataHeader(metadata->header)) {
        return nullptr;
    }
    metadata->geometry = geometry;

    LpMetadataHeader& header = metadata->header;

@@ -231,7 +232,6 @@ std::unique_ptr<LpMetadata> ParseMetadata(int fd) {

        metadata->extents.push_back(extent);
    }

    return metadata;
}

@@ -242,7 +242,7 @@ std::unique_ptr<LpMetadata> ReadPrimaryMetadata(int fd, const LpMetadataGeometry
        PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset " << offset;
        return nullptr;
    }
    return ParseMetadata(fd);
    return ParseMetadata(geometry, fd);
}

std::unique_ptr<LpMetadata> ReadBackupMetadata(int fd, const LpMetadataGeometry& geometry,
@@ -252,7 +252,7 @@ std::unique_ptr<LpMetadata> ReadBackupMetadata(int fd, const LpMetadataGeometry&
        PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset " << offset;
        return nullptr;
    }
    return ParseMetadata(fd);
    return ParseMetadata(geometry, fd);
}

std::unique_ptr<LpMetadata> ReadMetadata(int fd, uint32_t slot_number) {
@@ -268,14 +268,11 @@ std::unique_ptr<LpMetadata> ReadMetadata(int fd, uint32_t slot_number) {

    // Read the priamry copy, and if that fails, try the backup.
    std::unique_ptr<LpMetadata> metadata = ReadPrimaryMetadata(fd, geometry, slot_number);
    if (!metadata) {
        metadata = ReadBackupMetadata(fd, geometry, slot_number);
    }
    if (metadata) {
        metadata->geometry = geometry;
    }
        return metadata;
    }
    return ReadBackupMetadata(fd, geometry, slot_number);
}

std::unique_ptr<LpMetadata> ReadMetadata(const char* block_device, uint32_t slot_number) {
    android::base::unique_fd fd(open(block_device, O_RDONLY));
+1 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ std::unique_ptr<LpMetadata> ReadMetadata(int fd, uint32_t slot_number);

// Helper functions for manually reading geometry and metadata.
bool ReadLogicalPartitionGeometry(int fd, LpMetadataGeometry* geometry);
std::unique_ptr<LpMetadata> ParseMetadata(int fd);
std::unique_ptr<LpMetadata> ParseMetadata(const LpMetadataGeometry& geometry, int fd);

// These functions assume a valid geometry and slot number.
std::unique_ptr<LpMetadata> ReadPrimaryMetadata(int fd, const LpMetadataGeometry& geometry,