From patchwork Thu Oct 14 06:18:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dominique Martinet X-Patchwork-Id: 1540781 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20210112 header.b=XsysFE1K; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::838; helo=mail-qt1-x838.google.com; envelope-from=swupdate+bncbcwivbv7sugrbl4xt6fqmgqeo3wz2mi@googlegroups.com; receiver=) Received: from mail-qt1-x838.google.com (mail-qt1-x838.google.com [IPv6:2607:f8b0:4864:20::838]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HVK3B12k9z9sXS for ; Thu, 14 Oct 2021 17:18:28 +1100 (AEDT) Received: by mail-qt1-x838.google.com with SMTP id t15-20020ac8588f000000b002a777ee14d0sf3816687qta.14 for ; Wed, 13 Oct 2021 23:18:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1634192303; cv=pass; d=google.com; s=arc-20160816; b=hgAtRRMT3V1Opg0XFAGPDlGMX737ZGqBuDnser6vK8VSJMeMgTHuJorUrr0HKscvAz BO1RHEDChx+wyk6NLrp/Q3GHJb0Azz4mF6Dkilc1M1iBv7QKsoJpdKA4QLh9vMJQ63/e KF2ZbdLbdEoxWpPSKAzZ+idK+5A5YiPSsVQevPcijAPjOQvBU9sArMToYaBNSEhxXjGy OAHkvStuZvpWduYLQmKecQiOSqxoE+LQl17MqZ4hBNL/tD/IYdZqaoxIOJ4aV5hm3TRz CsUqDaWQ6fQWZQ7qnRHH3cjlkrf/UMSJ4d7HxLULANPpRNcfOLNLNUWSILO5EuFRYXSG cgPg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:mime-version:message-id:date :subject:cc:to:from:sender:dkim-signature; bh=oTJxJ2lMPIKkHVYKUwp+x7CRopvtdwrvNp7f9mhGPDI=; b=chybPZZhMVbpwb5y5xOvnCe3/kAZF40aGeuNri9L8JnCbMlb9wAxy6L6jgKCSAg4rB mmkxOM1E43cB9fHfKaZJ+TIxzxb+jhEGOySlU8LgyGDVj4n5dhLjAYi8A+9ciUbXJyDZ hXCKNC7n/Vy7ey0PjvvbkoNFro4f1M5WSvJ980MEsBx6JvSj4QpGcoz56g8w9AZZ0UdR Ui0o8aall4W7M8pDPxkVw4Yvs9ZdFfktzCRK3wHAQ220HVhW4sGIInDWxG14bZMZ3tm5 WdufleSua1HjQPIYl7Q9aj+lXexwmxGtknp+4yXigZPanBAtz5h0Eb1ZKLgY31gns8zM KTOA== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:mime-version :x-original-sender:x-original-authentication-results:precedence :mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=oTJxJ2lMPIKkHVYKUwp+x7CRopvtdwrvNp7f9mhGPDI=; b=XsysFE1Kf8DfzyadCe7qOV/s9FDsty5oYXFKPFdGLTSCAqqPPtO0IxmLgHxTJVtLw0 iUY9WAbMiYeR/5sPQtaPhkOLiqEWndCA8N6MvZcte9a6u1V3TwyWvspPgtcLduRyPmol GuzbHq7TVfYnpj84v2v14HzSzNgh0EnlhwrOMTGR7FLEN83c1wBNnrsA2hV8gdqYIxS8 ECSUCbdgirpXpm85LO+58A2+WvXzoJjViMe86Q/UAGAR7r4IEl08P/pQ/1wBvwIrxWVI +jBjSPAwe69I6X1c6lZbZjgJqLdVg4EbTpcomx0tjdU1lVG43Oaor7Mt32GuckFY6Ym1 qWIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=sender:x-gm-message-state:from:to:cc:subject:date:message-id :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post :list-help:list-archive:list-subscribe:list-unsubscribe; bh=oTJxJ2lMPIKkHVYKUwp+x7CRopvtdwrvNp7f9mhGPDI=; b=aXW2/U1aAO5a38WeqzH7Kvn4pM346p3Yy+xNi7Wt5K1JZeIHzJJlAdK2RzqrbqfUrQ Mo/QJBSJplgSxmikutgAKSXEZKsrygu2SXkRuxqsCRiytZOhThiLaNYhPkUxNXf/PmMH Ubsel44xbD9Q3hEFTYfwDH6j7SRupA1AmVTK5mV1auB7BPOL+ab0kc8jhyRmQizn0vOg mIOMClM6w4+ktwB+1ZlQEmwKBsEGx6CG27rD2HwkqkG9sNbbE9StEwPTjsGjhpLDngJX arcVcNmImXsqpe/MwvVOfaVRIbuF16grKIGUhOPDUu5b65EShmTyftRT+Qc2FRZVgcNe 0E6w== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM533Cchl5kjepOyCgDXFzTzMhZhfJNKzIoAapRQCH0ZJZ5KIfwwQr n3Lhv+W3mD9pnN7h7Af7rRs= X-Google-Smtp-Source: ABdhPJyNvk1CyFamGEwxvY3HXrIFi7sWxoO+5E7oLyOpDsNIrOF9s88yMwQXizyznLhkTV6qbgXGmQ== X-Received: by 2002:a37:a2c1:: with SMTP id l184mr3100143qke.71.1634192303307; Wed, 13 Oct 2021 23:18:23 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:aed:27d8:: with SMTP id m24ls653408qtg.6.gmail; Wed, 13 Oct 2021 23:18:22 -0700 (PDT) X-Received: by 2002:a05:622a:13cc:: with SMTP id p12mr4390495qtk.255.1634192302742; Wed, 13 Oct 2021 23:18:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634192302; cv=none; d=google.com; s=arc-20160816; b=wlvKKRVIUavFAgn13x6JU8jducl5YrtFHDTwvA206fCAqsVKag5n7n1BlFSKyO2Y4w qRnkxrb0tA97oDf8om5dFYZLX/ezKSHeiHDUkPUhrHCIyQzb4on4z/Vutk2lA48reR1I f3GNLxZwSuEr9tYk7gYBJIuOy2U7gwlkylySzRkhu5lpxNer0CjdfZ7MN9ZOJ0R46SvH ogKJfMZydpONvJWdY8vauqR2txYqOOtbzkuxfjVDNvNVQxNhxclhnyuFoQE9F/+Kk3/H P5zF1qwOLuRxXe8xVgSh477GKD7dvOCoX7l2HTtbkaHzrWZIzQiq9KHn9E8x14Orj9rV tYZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from; bh=U011uOVGwznpxKOrtYRwlgT+Vj2T4sMXLuYEyK4l4mw=; b=mANWy9c7mryDnWC5LHyrwtReZs3FBR0iRKbPuq/MDfBKps0HAUMzAHM7zIVtcnoQ+k D8edKqD5k+F4JVTm/7H42tAGF6VSqUWXOmQlxGiWgFp7TrXYPW5xsMjwm8ZgB6jlPLnh LshkQ9Z7QWbPHkqPUH3rYJl+c2wLy3LXbQE/0V1TKuqd3h34FRH86xDgGTnbDhxtqDu1 qIkJdl78U6j2GCFNlfwsfSZ+ZFuf9sBg+tQ1O4TBOho+ANF0YtqegvgZmmIoFlxlfFBS KkEbOs0HnKXMf38zb8C5kAA2F7NMlHcZztDiV91f+UeEyBtHk3qOq7vu/1ThHiCWEPL9 0Z3g== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com Received: from gw2.atmark-techno.com (gw2.atmark-techno.com. [35.74.137.57]) by gmr-mx.google.com with ESMTP id k16si175682qkg.7.2021.10.13.23.18.22 for ; Wed, 13 Oct 2021 23:18:22 -0700 (PDT) Received-SPF: pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) client-ip=35.74.137.57; Received: from gw2.atmark-techno.com (localhost [127.0.0.1]) by gw2.atmark-techno.com (Postfix) with ESMTP id 6884520D3C for ; Thu, 14 Oct 2021 15:18:20 +0900 (JST) Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) by gw2.atmark-techno.com (Postfix) with ESMTPS id 53E1D20D3B for ; Thu, 14 Oct 2021 15:18:20 +0900 (JST) Received: by mail-pl1-f199.google.com with SMTP id p17-20020a170902ead100b0013f7d398f40so518490pld.18 for ; Wed, 13 Oct 2021 23:18:20 -0700 (PDT) X-Received: by 2002:a63:b34e:: with SMTP id x14mr2773296pgt.423.1634192299313; Wed, 13 Oct 2021 23:18:19 -0700 (PDT) X-Received: by 2002:a63:b34e:: with SMTP id x14mr2773288pgt.423.1634192298977; Wed, 13 Oct 2021 23:18:18 -0700 (PDT) Received: from pc-0115 (145.82.198.104.bc.googleusercontent.com. [104.198.82.145]) by smtp.gmail.com with ESMTPSA id c27sm1385182pgb.89.2021.10.13.23.18.18 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Oct 2021 23:18:18 -0700 (PDT) Received: from martinet by pc-0115 with local (Exim 4.94.2) (envelope-from ) id 1mau4H-008QmX-AP; Thu, 14 Oct 2021 15:18:17 +0900 From: Dominique Martinet To: swupdate@googlegroups.com Cc: Dominique Martinet Subject: [swupdate] [PATCH] core/artifacts_version: compare oldstyle versions directly Date: Thu, 14 Oct 2021 15:18:15 +0900 Message-Id: <20211014061815.2009528-1-dominique.martinet@atmark-techno.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Original-Sender: dominique.martinet@atmark-techno.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , version_to_number() has a number of issues: - versions with more than 4 elements had their trailing part ignored (e.g. '1.2.3.4', '1.2.3.4.5', '1.2.3.4.12' are all equal) - version token bigger than 65535 were truncated down to 0xffff (e.g. '1.65536' and '1.0' are equal. '65536' is smaller than '10') workaround the issue by not converting to an intermediate u64. This still fails to handle versions bigger than ULONG_MAX but at least prints a TRACE if that case happens. Signed-off-by: Dominique Martinet --- core/artifacts_versions.c | 73 ++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/core/artifacts_versions.c b/core/artifacts_versions.c index 433125862014..eb605bc403f1 100644 --- a/core/artifacts_versions.c +++ b/core/artifacts_versions.c @@ -161,39 +161,52 @@ static bool is_oldstyle_version(const char* version_string) } /* - * convert a version string into a number - * version string is in the format: + * compare version strings in the following format: * * major.minor.revision.buildinfo * - * but they do not need to have all fields. - * Also major.minor or major.minor.revision are allowed - * The conversion generates a 64 bit value that can be compared + * they do not need to have all fields. */ -static __u64 version_to_number(const char *version_string) +static int compare_oldstyle_version(const char *left_version, + const char *right_version) { - char **versions = NULL; - char **ver; - unsigned int count = 0; - __u64 version = 0; + char **left_versions = NULL, **right_versions = NULL; + size_t i; + int compare; - versions = string_split(version_string, '.'); - for (ver = versions; *ver != NULL; ver++, count ++) { - if (count < 4) { - unsigned long int fld = strtoul(*ver, NULL, 10); - /* check for return of strtoul, mandatory */ - if (fld != ULONG_MAX) { - fld &= 0xffff; - version = (version << 16) | fld; - } + left_versions = string_split(left_version, '.'); + right_versions = string_split(right_version, '.'); + for (i = 0; left_versions[i] != NULL && right_versions[i] != NULL; i++) { + unsigned long int left_ver = strtoul(left_versions[i], NULL, 10); + unsigned long int right_ver = strtoul(right_versions[i], NULL, 10); + if (left_ver == ULONG_MAX) + TRACE("version overflow: %s\n", left_versions[i]); + if (right_ver == ULONG_MAX) + TRACE("version overflow: %s\n", right_versions[i]); + + if (left_ver < right_ver) { + compare = -1; + goto out; + } else if (left_ver > right_ver) { + compare = 1; + goto out; } - free(*ver); } - if ((count < 4) && (count > 0)) - version <<= 16 * (4 - count); - free(versions); + if (left_versions[i] != NULL) + compare = 1; + else if (right_versions[i] != NULL) + compare = -1; + else + compare = 0; +out: + for (i = 0; left_versions[i] != NULL; i++) + free(left_versions[i]); + free(left_versions); + for (i = 0; right_versions[i] != NULL; i++) + free(right_versions[i]); + free(right_versions); - return version; + return compare; } /* @@ -211,18 +224,8 @@ int compare_versions(const char* left_version, const char* right_version) { if (is_oldstyle_version(left_version) && is_oldstyle_version(right_version)) { - __u64 left_u64 = version_to_number(left_version); - __u64 right_u64 = version_to_number(right_version); - DEBUG("Comparing old-style versions '%s' <-> '%s'", left_version, right_version); - TRACE("Parsed: '%llu' <-> '%llu'", left_u64, right_u64); - - if (left_u64 < right_u64) - return -1; - else if (left_u64 > right_u64) - return 1; - else - return 0; + return compare_oldstyle_version(left_version, right_version); } else {