From patchwork Fri Oct 15 04:18:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dominique Martinet X-Patchwork-Id: 1541292 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=VbSEWFAj; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::940; helo=mail-ua1-x940.google.com; envelope-from=swupdate+bncbcwivbv7sugrbm4cusfqmgqeiuh7mwa@googlegroups.com; receiver=) Received: from mail-ua1-x940.google.com (mail-ua1-x940.google.com [IPv6:2607:f8b0:4864:20::940]) (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 4HVtLx4dfBz9ssD for ; Fri, 15 Oct 2021 15:19:04 +1100 (AEDT) Received: by mail-ua1-x940.google.com with SMTP id i9-20020ab029c90000b02902aa59690c5asf3981109uaq.3 for ; Thu, 14 Oct 2021 21:19:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1634271539; cv=pass; d=google.com; s=arc-20160816; b=Dl01p0lOeppPCEITWls7nplmWVi2MeHfk2H5hsZ5NTZn+BshvVudwHfroCzqjysjxn MoWy4LX4TsM1chFZQNoMWK0xmqftzg/oOx9ehSgC8eQ7BIBjqDdoqbFb/O57+AonULDB HO4peKn2/rmnrROjX3QXyrPw1SdFFaTuZou6cpTXHXS5vAc4ZGfgSQ6agu0GdqqXbtJK xvV3FgZ4CNfq2aPLU0pCEtKmvAeeVWRjGHF7BEsUO6XAZll2w+BJgUpiz3vBJIpdl/mW uHzLw9RpipzLPY2cmf4qx0+Yml43Azfl39jUw7jKLpgtS0fXk10yjiy4zJSCls7vJnyD RU1g== 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:references:in-reply-to :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=AyOmAeqCCcEjxXk4NsXPrCda9PmWIPT+HTYf6cXr54s=; b=kiZI8Vn2BrdwLCLt3pWSD5qf1X8dPeqaVuZbMfKRWS1gvCU7ndAulJIJHRLeEDtFNo EY1tZhpGBGCjJzFqQfJ9iVCCveeKENN/f/ydp0u4+MIwxr1f9k1YRBUjNOT+8PmCfnyO hs+GZ32T/veyIfEQsHeTQL3xPuFWPW5+xEtemtJpBrZyCgqO6HN8Le3DiHhQZ+i3Yk/A 75MByCpxxkXMTzrIQ2E/ZIn83RnjNmFdVdLNed5uoJERX8yfXH2aiTKvIx8Fie68cbBq YBnnN6NCsdnTa2Ugul8LUkt4BRFcX/fT6QP7VlCTGYmFGJCvFoglDYZdAj7HRK/fynVu XDxA== 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:in-reply-to:references :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=AyOmAeqCCcEjxXk4NsXPrCda9PmWIPT+HTYf6cXr54s=; b=VbSEWFAj7jPX14um/Q7AYh0cGlzjcXSaFK+Pjxilz7X/Zvb2qgZeFDHCrj9oCAIu6p 7nAsNXONf+9EveQqkdC4IVzpnBIbCzAbiZqNS7VAVLBdJQ/P6vCXG1/qPw/HPsPL5zJm mmcK2fW8PZfe1qxj78GmKCilOJ5KJ92bOibiRA+wghL826Hn3Z8WdqywpylzBtkm7tD9 fGHgw9chvh8IB8kkPIH+umlg3A2p+Ykiaj9KupXXW5lyeS4O3X5VEq+82eP2iASMR83o gWxnFDfEF3/6B7pAUrywclBWOXrB0TfA4b7CWYo7+LQrLaOaWOsGGWVwv9ZnXggFyTG7 AlSQ== 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 :in-reply-to:references: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=AyOmAeqCCcEjxXk4NsXPrCda9PmWIPT+HTYf6cXr54s=; b=BR8UG5TYamk7/4XbIdFCRT4OIhdSeANysiK7BT5YA8PWkGGeOb0aVXSzLgh1u1cOgd StohBRYAYHYRf2ivbE+Ks8oN1jOUaqjWPBAb33EI69iGNrSVBfqg79nXl3poPE9l+8E2 nCHS68OOnzlN8Xa+xwYYXFGUySd24ouoePnRmaqxLnXpflwIztBbkadOAsSH+dmXpaN1 8csDD6glPfyC6aLuSt5/CVjrK3T5/0gFotz/99FUXNWnDqLfCu/W88XV5ZY28D4b7UiX UqLW/fvIFWMorWIEMLkk/j7O05VJ72Vx1OT4wWW1BMY0Vz32A7TL0pxtDR0zQvhL2/0p yBKA== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM533aLqR2EJRg8UA7yh2QkFKkJZ/fGBIrozow+jTHfdvo85bBDZUB njlU3pMEN854Knrn0mfqLBs= X-Google-Smtp-Source: ABdhPJxyh8sRqziV9Ixj6VvB7TTpGL5htm2l0JhXWUr4gSB060vm6LO1x3lFWDewSo+GESmmaNlH2Q== X-Received: by 2002:ab0:3d09:: with SMTP id f9mr11700344uax.135.1634271539443; Thu, 14 Oct 2021 21:18:59 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:ab0:61d6:: with SMTP id m22ls1491265uan.3.gmail; Thu, 14 Oct 2021 21:18:59 -0700 (PDT) X-Received: by 2002:a9f:3012:: with SMTP id h18mr10869836uab.56.1634271539060; Thu, 14 Oct 2021 21:18:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634271539; cv=none; d=google.com; s=arc-20160816; b=NmnNi8kkyWGuTTrunuVS0CY8jvmFk/CE4vTcG2xHDXymgZrRlEBZeDJAXoGQ4i39Uo qQeuOWlJENVvNLm+NIdTji9GmAmGBuqR5/KzfNglA//sqorybay7Aq4NuWwYthP2Smfp WraFsshFwX+MgVSsG7CIcf5Zp6wqlI0PjMtN75dD7afOSJ5pDehniwf+SIH+IYtVlwX1 5ok9knhNp6cNrhbD2ztU6SeywsZGkSukCwHCwuGvu5jHrTZPmZveM3CY+7vfic7obO0P OdXa/mpUu7ZaYa3vjfW/3Y7BCcZORIDEBQgmqOs7Pfm4uCHiXJ2u3TiW5XmSU+WeSEmW O1RQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=SjilrwA3wOpcrJEkU3sqANHAvQnbedfdqVr1W3zkK7Q=; b=Bpn+aSz4eQKM11MyDgHX8rVirvwS3ARXFfLAG7PtBj/scD0RdaKWiCJQCCYFU1WDp1 oqWrj4Ghi5VvdQzQ29LUi7J+UoiRch6UUV6s1xv95AfLX98EEo7lFeDpJbP1L7YRSPHg 3e9JA/r/2eh9DhqZlLI4ck4E8qYjzHoL8+4tkDWAuNO6qXN6pXx/kMvWGk4ymULOPHyK WCkyTIVyQpydj5bwErOD144vqbQvRDE/bjh1KgDoS1U/PNBEeOSK3kQ001Y7IWjiNJ0O jbqJWlFhuMIPtrt0OAkXE6LASFVTdVOPQTnFWsHtPXM1uz8+dNfGa5ddDplu9mAP6JYj CajA== 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 c19si341437uad.1.2021.10.14.21.18.58 for ; Thu, 14 Oct 2021 21:18:58 -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 AD38620D3C for ; Fri, 15 Oct 2021 13:18:56 +0900 (JST) Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) by gw2.atmark-techno.com (Postfix) with ESMTPS id 90D0320D3B for ; Fri, 15 Oct 2021 13:18:56 +0900 (JST) Received: by mail-pj1-f69.google.com with SMTP id s34-20020a17090a2f2500b001a0e1134a7aso3415197pjd.1 for ; Thu, 14 Oct 2021 21:18:56 -0700 (PDT) X-Received: by 2002:a17:90b:3b85:: with SMTP id pc5mr10954914pjb.74.1634271535643; Thu, 14 Oct 2021 21:18:55 -0700 (PDT) X-Received: by 2002:a17:90b:3b85:: with SMTP id pc5mr10954892pjb.74.1634271535376; Thu, 14 Oct 2021 21:18:55 -0700 (PDT) Received: from pc-0115 (162.198.187.35.bc.googleusercontent.com. [35.187.198.162]) by smtp.gmail.com with ESMTPSA id c4sm452996pfv.144.2021.10.14.21.18.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Oct 2021 21:18:55 -0700 (PDT) Received: from martinet by pc-0115 with local (Exim 4.94.2) (envelope-from ) id 1mbEgH-008uHz-8o; Fri, 15 Oct 2021 13:18:53 +0900 From: Dominique Martinet To: swupdate@googlegroups.com Cc: Dominique Martinet Subject: [swupdate] [PATCH v2 1/3] compare_versions: make is_oldstyle_version fail when number > 65535 Date: Fri, 15 Oct 2021 13:18:49 +0900 Message-Id: <20211015041851.2122952-1-dominique.martinet@atmark-techno.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211014061815.2009528-1-dominique.martinet@atmark-techno.com> References: <20211014061815.2009528-1-dominique.martinet@atmark-techno.com> 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: , is_oldstyle_version would accept any string comprised of digits and dots. Parse the actual version in is_oldstyle_version to detect large numbers and fall back to semver if a large number was given, as semver can handle bigger values. Note that if this happens on the 4th level, this result in that number actually being ignored as semver only handles up to major.minor.patch levels. Also print trace messages in case of parse failure (too many digits or number too big) to facilitate debugging. Signed-off-by: Dominique Martinet --- So here is a version that checks directly that each field does not overflow 16 bits and falls back to semver. Honestly this looks more complicated than the first patch I had sent, so I would rather not merge this -- the fact that the 4th field just starts disappearing if any of the field goes > 65535 is also hard to understand for users so I will just enforce the limit in our image creation tool and have users relying on date try something else (adding dots or setting it in prerelease part of semver) Let's drop this one. The later 2 patches are valid regardless. One is pure style, feel free to ignore it, I was just annoyed by it. Then documentation as promised. core/artifacts_versions.c | 61 +++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/core/artifacts_versions.c b/core/artifacts_versions.c index 433125862014..14abdb420498 100644 --- a/core/artifacts_versions.c +++ b/core/artifacts_versions.c @@ -147,19 +147,6 @@ void get_sw_versions(swupdate_cfg_handle __attribute__ ((__unused__))*handle, } #endif -static const char ACCEPTED_CHARS[] = "0123456789."; - -static bool is_oldstyle_version(const char* version_string) -{ - while (*version_string) - { - if (strchr(ACCEPTED_CHARS, *version_string) == NULL) - return false; - ++version_string; - } - return true; -} - /* * convert a version string into a number * version string is in the format: @@ -170,7 +157,7 @@ static bool is_oldstyle_version(const char* version_string) * Also major.minor or major.minor.revision are allowed * The conversion generates a 64 bit value that can be compared */ -static __u64 version_to_number(const char *version_string) +static bool version_to_number(const char *version_string, __u64 *version_number) { char **versions = NULL; char **ver; @@ -178,22 +165,43 @@ static __u64 version_to_number(const char *version_string) __u64 version = 0; versions = string_split(version_string, '.'); - for (ver = versions; *ver != NULL; ver++, count ++) { + 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; + if (fld > 0xffff) { + TRACE("Version %s had an element > 65535, falling back to semver", + version_string); + return false; } + version = (version << 16) | fld; } free(*ver); } - if ((count < 4) && (count > 0)) + if (count >= 4) { + TRACE("Version %s had more than 4 numbers, trailing numbers will be ignored", + version_string); + } else if (count > 0) { version <<= 16 * (4 - count); + } free(versions); - return version; + *version_number = version; + return true; +} + +static const char ACCEPTED_CHARS[] = "0123456789."; + +static bool is_oldstyle_version(const char *version_string, __u64 *version_number) +{ + const char *ver = version_string; + while (*ver) + { + if (strchr(ACCEPTED_CHARS, *ver) == NULL) + return false; + ++ver; + } + return version_to_number(version_string, version_number);; } /* @@ -209,12 +217,15 @@ static __u64 version_to_number(const char *version_string) */ 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); + __u64 left_u64; + __u64 right_u64; + + if (is_oldstyle_version(left_version, &left_u64) + && is_oldstyle_version(right_version, &right_u64)) + { + DEBUG("Comparing old-style versions '%s' <-> '%s'", + left_version, right_version); TRACE("Parsed: '%llu' <-> '%llu'", left_u64, right_u64); if (left_u64 < right_u64)