Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp2223114pxb; Sat, 21 Nov 2020 13:43:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJy8qQnlILucgOVuDG5hktH63rmtC4gGCqUe83frWERX1ugg8jT5PaCudWn4F6gs3bsRF9to X-Received: by 2002:a17:907:4270:: with SMTP id nx24mr11507675ejb.296.1605995015819; Sat, 21 Nov 2020 13:43:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605995015; cv=none; d=google.com; s=arc-20160816; b=rm+8Abo4DFEfIJNlhAuiJ0AtyNjK2t9dseLFEDXNEV8Ti53bF2DCEMvUgfDGdDOK7d wNMYmmOxI9Jb0WRcLGyP+Lo8Ae8ismPhzI0wR8tDJJbyTZ7Y32XLmkWoF/iLSnAvO6wL 2y3hNjFVAy7aoU9ACnubCJz8ScVHIiAcSZZMoah8OGOfbW5dEV/m1GWXj1ST6da5fk5G FW6rw+3nN3td+Oh6ptvc5GeX7UREyCqgiVjCxwFQoQQgWZ/H9/02sqfrl/fCQoRF7Hzr Rh4tWRD9rOn2YNXJzKpAA1xSRWv/9MgYs64e5Q4ppITKqrFEKq7w3o0I94m5ilGPDPv6 Pizg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=0J78ONxskOfwW3kAl7JvGSRKJdmHf4/sMvVrPtGe0jM=; b=OIQKOS2ufVbwi3YJnXdlfT/K7i1pPjMuWee1ppRf0gUnmfqGlgNP/CUZxA2nf/V5nE oilKiK36KP9DQkQpE7ntOUIUSHEnAOgZ04774CKh4YuADXvHf8BlyaB3uM3pc/supmAq AeAKBf9U3tnaSIw+vgOowXM9v69pd+yfs2uYE6nDorJyCqSbIWJU043zgQ6OCSwNikBk NCrT8RGVlGO2iEXxPjZh4xN7ddqVxkdTz3+BDSXiHcge4wLqfyZ0IFdQ5Nmjr26KekTx 3WoXcstKsPw2HYmQ4xKXx17BpKqtBFUQMkXyVdhjKW26o46jgY9YbdQR6gkCW0PDBSFs tejQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=inS7sVPD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l63si4361480ede.607.2020.11.21.13.43.12; Sat, 21 Nov 2020 13:43:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=inS7sVPD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728585AbgKUVkl (ORCPT + 99 others); Sat, 21 Nov 2020 16:40:41 -0500 Received: from mail.kernel.org ([198.145.29.99]:44532 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728402AbgKUVkk (ORCPT ); Sat, 21 Nov 2020 16:40:40 -0500 Received: from kicinski-fedora-pc1c0hjn.dhcp.thefacebook.com (unknown [163.114.132.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 11FEB21D7A; Sat, 21 Nov 2020 21:40:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605994840; bh=Ovwoe+hlcKIY8SdSV622ELvlp3mbfc7w6Hr/DYwRing=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=inS7sVPD3LmeHiLYOlxWCFtEyeTDs3UtYN0OYPrOQ/s/JJVzioZbPIbqrkbMTiZOJ 00fNSPy5wqFOciPfh/0PulvFhimTWnmOL/60u4ntBHIiSCEcW6M/vC2rnW+Dr+XiKl uLya/6wnv+HpdWqw5CmHk+TUfm7z4Yq2b5/qaGgI= Date: Sat, 21 Nov 2020 13:40:39 -0800 From: Jakub Kicinski To: Cc: , , Subject: Re: [PATCH v2 net] ptp: clockmatrix: bug fix for idtcm_strverscmp Message-ID: <20201121134039.42586c72@kicinski-fedora-pc1c0hjn.dhcp.thefacebook.com> In-Reply-To: <1605757824-3292-1-git-send-email-min.li.xe@renesas.com> References: <1605757824-3292-1-git-send-email-min.li.xe@renesas.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 18 Nov 2020 22:50:24 -0500 min.li.xe@renesas.com wrote: > From: Min Li > > Feed kstrtou8 with NULL terminated string. > > Changes since v1: > -Only strcpy 15 characters to leave 1 space for '\0' > > Signed-off-by: Min Li > -static int idtcm_strverscmp(const char *ver1, const char *ver2) > +static int idtcm_strverscmp(const char *version1, const char *version2) > { > u8 num1; > u8 num2; > int result = 0; > + char ver1[16]; > + char ver2[16]; > + char *cur1; > + char *cur2; > + char *next1; > + char *next2; > + > + strncpy(ver1, version1, 15); > + strncpy(ver2, version2, 15); > + cur1 = ver1; > + cur2 = ver2; Now there is no guarantee that the buffer is null terminated. You need to use strscpy(ver... 16) or add ver[15] = '\0'; > /* loop through each level of the version string */ > while (result == 0) { > + next1 = strchr(cur1, '.'); > + next2 = strchr(cur2, '.'); > + > + /* kstrtou8 could fail for dot */ > + if (next1) { > + *next1 = '\0'; > + next1++; > + } > + > + if (next2) { > + *next2 = '\0'; > + next2++; > + } > + > /* extract leading version numbers */ > - if (kstrtou8(ver1, 10, &num1) < 0) > + if (kstrtou8(cur1, 10, &num1) < 0) > return -1; > > - if (kstrtou8(ver2, 10, &num2) < 0) > + if (kstrtou8(cur2, 10, &num2) < 0) > return -1; > > /* if numbers differ, then set the result */ > - if (num1 < num2) > + if (num1 < num2) { > result = -1; Why do you set the result to something instead of just returning that value? The kstrtou8() checks above just return value directly... If you use a return you can save yourself all the else branches. > - else if (num1 > num2) > + } else if (num1 > num2) { > result = 1; > - else { > + } else { > /* if numbers are the same, go to next level */ > - ver1 = strchr(ver1, '.'); > - ver2 = strchr(ver2, '.'); > - if (!ver1 && !ver2) > + if (!next1 && !next2) > break; > - else if (!ver1) > + else if (!next1) { > result = -1; > - else if (!ver2) > + } else if (!next2) { > result = 1; > - else { > - ver1++; > - ver2++; > + } else { > + cur1 = next1; > + cur2 = next2; > } > } > } > + > return result; > } >