From patchwork Mon Oct 18 23:31:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dominique Martinet X-Patchwork-Id: 1542913 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=N9smC865; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::c39; helo=mail-oo1-xc39.google.com; envelope-from=swupdate+bncbcwivbv7sugrbz4hxcfqmgqe4jztrni@googlegroups.com; receiver=) Received: from mail-oo1-xc39.google.com (mail-oo1-xc39.google.com [IPv6:2607:f8b0:4864:20::c39]) (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 4HYCnk5LxCz9sP7 for ; Tue, 19 Oct 2021 10:31:54 +1100 (AEDT) Received: by mail-oo1-xc39.google.com with SMTP id w1-20020a4a2741000000b002b6eb5b596csf726962oow.9 for ; Mon, 18 Oct 2021 16:31:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1634599912; cv=pass; d=google.com; s=arc-20160816; b=BuypFMBz5zvAfQh1Cm3IuBLWs/d0f0m00H/xhZeqLxVu8PZzQ9/OEEfp2NNsN/goh+ Df7kZsGTz1xBinzTqSHThN1pkw5e1+l8Ft7u3Ldq1vur+p4s/qJ2p/+K+dNWuizwFOak CeVK+jv8PZ6sGlfV1EzUuGsYC0gnpzik5YARiEcl5URxjMYUEiYohJSahnqyGLTi3xF6 f6XfJ4yAmEo6XFKqmikWsaAdOUc0mGxZ48ld2m93lolhPt6qxjnYGiQcKLbr8pYdVwRg IdTRgCpSg27USoMLOOM6kA7MDTfTWjbRAU98VXzgRyWTj4+iq7R60wZEfWesTM08sgqz y5Hw== 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=GklayRKbcP+jy97hhIn5wZjDlI88ODaa5SQVXJxmthw=; b=EBmlkim5n+8hAjawtHZdObRgWMRmP9CLUO7o6YKpYKK8DV0wdYlGbJAC1BwjyPnPme TErW2R+J5I1riivJxhx0P4aFavqUIoBoQyLamGwv46MJX7jS9zrO4l0W+TUaLwTTQpd9 BQ1+VjidffgCxUsAD9yeFODhwID9yI5EXBu8wmpslGdCIzP/FeQBpTnYtf7A7GiO4YxK BiOMlMSgyUkwb7yVUYiw0x68Hc//R9YRfX5Qh03jlJFmp34ZCHJx6/ykd6g1yyDjyIHu sXIRjPVWtAxJbmroA4N58rHU57nz+omOz9xnYB/bVNW49WgNT7qE83+WGuedSW1XlAu5 Zhjw== 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=GklayRKbcP+jy97hhIn5wZjDlI88ODaa5SQVXJxmthw=; b=N9smC865Z5BGoumCZYmr6MjhFOWQSCd8BvquMv5jvW+5i7AT3HYcdbi2KPli4mV8tL 1KnluI6rJH5QzpeAX/qGVSgUpA8UaGNbmKSAUd+BPMgwjMuKCOn4ptsWdwwU7aVl6opQ rn1pU0oyba55WeuerYiW5vuLDn+ZzJt9DIzijeVswuw+Nk3yom1khNj3hxZ/HvoIIXZ7 ztRYR+xiOB5kPMsa1T+iag4tUhl6U8PMdnaBiSGPENtytMw/S3Dqf/0I7NS4m7kbOWo6 yJPzbUSnmDYjaY0+1BrRpdbYWpBdk1gIz9jZkjh/bZTVdSavKEjEtIjBclAP/GmB8b8X 8Iug== 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=GklayRKbcP+jy97hhIn5wZjDlI88ODaa5SQVXJxmthw=; b=B9bkwswfN7+aL03bOSISCzVHpf+HaVXQ3ez4zPiEpqF9Z9vLL0TDy+2GUzjNqx+nNa PuqZ3JjjUl4IJtiA3SNEhqznkb8Ac3AiN4qqRfYYxWCVgOiiRVQ863L4OP8rPlIvo4xN MYHbJTJmbSMI2BOwFcsuwleluGrEp1j9Rw7LAL5t6pDzP0eGJp5jKRK3d9olbNTsl1Yg fyhQzjGLGK9UEb3OKvq1vYyi6gucHavI6Gj25l8VYOkpdmmf+wTsh5Ndp8Uos1Jqx0hY eyCsTVlczsDL92psKq831Dy2uuCejPABeDUyHIYmRAV4VRhOXifNBoRX1Cpe+fdnVZHD Q8mA== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM530hw5+0XdJNk3oFMQQiFg3bIrUkXEGxIzYdiE0rcQ69p7PK2pLw Y1+vtEpTFLOMDuYrTsfmxjU= X-Google-Smtp-Source: ABdhPJzL1F1iNhj3P1bXcjJlb7APeYfMAT+DV/UI84FG20IgrIB6G3snsgpjMqgQ4ib1tLI8/MpkTw== X-Received: by 2002:a05:6830:1c26:: with SMTP id f6mr2342953ote.28.1634599912081; Mon, 18 Oct 2021 16:31:52 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a9d:6155:: with SMTP id c21ls5500898otk.10.gmail; Mon, 18 Oct 2021 16:31:51 -0700 (PDT) X-Received: by 2002:a05:6830:1c2f:: with SMTP id f15mr2415086ote.63.1634599911480; Mon, 18 Oct 2021 16:31:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634599911; cv=none; d=google.com; s=arc-20160816; b=WM+oTYg/Hvfr4eIHTYFENGRv11Ow6gI5zt7P7yNByAfFaXysYeYs98XX23kJKHHJ6A UtnwB8XLUiJZ0HQXdDQAbe2BlaO7lkfHNrGfWH5O5wniaz4D9MviSgLavYpWSwWUOkio GMZs4k4/9psDreWqHeoc0K0DT4vtM9BH3z7yPtEQnWrw568y+6NYQyqorVjwT47+S+LD /8f1KdnKoadpPU+9lsa2Rxgzm+Bg1I12NZYx4ZVhD5sQJUUqI5yEERaWcQBB0lcwoqgt skb5D4k+xFwpZPGVQPxrcfJXfh5kjPkHYwYVwdO1LGmITfR7MFSq0i0YkQROxYCwZNwO kDHA== 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=atwkhB/2qS3WXhU3NaE2lH+PT1x1wFmdihFMpIZdpwo=; b=R4BcggCbYgK3+GP9/94gfybLkjWpWHL79/I5Tf1r0YWMfoQTx01Vs7Lfav7jw9xRQW a6lqs0b2ApbfjxdRVXvpk3TTn2b5XAXnGXMKrxFNsaw37InwJ3DDcmEcnvdHsckNDz1c sUjJL/L+vtCnwE578W/o2VBHx7qY56CtykJQyKgZXBp50cig/GGBpuAbKsOfN/26yJ9S e9Uz6KcOqrlSjUKPNQ4ZCpzsscjyj3r9gdp65LBbVeIkFRZf/1hyV6nju6YWUfo9L4zB 8VZqZgzqfDKJnq0Hn9HZ02aFgfGH7HCSp7YRZTBRbNBvppkONjGieVxFBzj7MSBSoeJo 8ijQ== 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 o24si540023otp.2.2021.10.18.16.31.50 for ; Mon, 18 Oct 2021 16:31:51 -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 8E6FF20D41 for ; Tue, 19 Oct 2021 08:31:49 +0900 (JST) Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by gw2.atmark-techno.com (Postfix) with ESMTPS id 7AB2E20D41 for ; Tue, 19 Oct 2021 08:31:49 +0900 (JST) Received: by mail-pg1-f197.google.com with SMTP id g18-20020a631112000000b00299f5f53824so1070809pgl.2 for ; Mon, 18 Oct 2021 16:31:49 -0700 (PDT) X-Received: by 2002:a17:902:7106:b0:13f:aaf4:3e91 with SMTP id a6-20020a170902710600b0013faaf43e91mr14661592pll.84.1634599908509; Mon, 18 Oct 2021 16:31:48 -0700 (PDT) X-Received: by 2002:a17:902:7106:b0:13f:aaf4:3e91 with SMTP id a6-20020a170902710600b0013faaf43e91mr14661574pll.84.1634599908248; Mon, 18 Oct 2021 16:31:48 -0700 (PDT) Received: from pc-0115 (76.125.194.35.bc.googleusercontent.com. [35.194.125.76]) by smtp.gmail.com with ESMTPSA id o127sm14224424pfb.216.2021.10.18.16.31.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 Oct 2021 16:31:47 -0700 (PDT) Received: from martinet by pc-0115 with local (Exim 4.94.2) (envelope-from ) id 1mcc6c-00A0f7-QE; Tue, 19 Oct 2021 08:31:46 +0900 From: Dominique Martinet To: swupdate@googlegroups.com, Stefano Babic Cc: Dominique Martinet Subject: [swupdate] [PATCH v2] compare_versions: make is_oldstyle_version fail when number > 65535 Date: Tue, 19 Oct 2021 08:31:42 +0900 Message-Id: <20211018233142.2385778-1-dominique.martinet@atmark-techno.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: References: 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 --- v1->v2: - fix memory leak on early return - use free_string_array() core/artifacts_versions.c | 74 +++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/core/artifacts_versions.c b/core/artifacts_versions.c index 433125862014..e93248d8b955 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,30 +157,52 @@ 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; unsigned int count = 0; + bool valid = false; __u64 version = 0; 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; - } + for (ver = versions; *ver != NULL && count < 4; ver++, count++) { + unsigned long int fld = strtoul(*ver, NULL, 10); + /* check for return of strtoul, mandatory */ + if (fld > 0xffff) { + TRACE("Version %s had an element > 65535, falling back to semver", + version_string); + goto out; } - free(*ver); + version = (version << 16) | fld; } - 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); + } + *version_number = version; + valid = true; - return version; +out: + free_string_array(versions); + + return valid; +} + +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 +218,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)