From ca6cdd26055d6cf740662ed9102d674c7bf3d859 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 2 Jun 2023 12:25:09 +0200 Subject: [PATCH] dissect-image: fix partition label version compare The logic was borked: if we find multiple partitions of the same designator, we should first prefer the better arch, and then prefer the better version, and then the first found. Fix that. Fixes: #27897 --- src/shared/dissect-image.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index becd13f4c5c..cf432e2177a 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -1217,16 +1217,19 @@ static int dissect_image( } if (m->partitions[type.designator].found) { + int c; + /* For most partition types the first one we see wins. Except for the * rootfs and /usr, where we do a version compare of the label, and * let the newest version win. This permits a simple A/B versioning * scheme in OS images. */ - if (compare_arch(type.arch, m->partitions[type.designator].architecture) <= 0) + c = compare_arch(type.arch, m->partitions[type.designator].architecture); + if (c < 0) /* the arch we already found is better than the one we found now */ continue; - - if (!partition_designator_is_versioned(type.designator) || - strverscmp_improved(m->partitions[type.designator].label, label) >= 0) + if (c == 0 && /* same arch? then go by version in label */ + (!partition_designator_is_versioned(type.designator) || + strverscmp_improved(label, m->partitions[type.designator].label) <= 0)) continue; dissected_partition_done(m->partitions + type.designator); -- 2.47.3