Received: by 2002:a05:7412:d008:b0:f9:6acb:47ec with SMTP id bd8csp186496rdb; Tue, 19 Dec 2023 13:18:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IFwQAXnXL3ghcGjiErXz+i3QR194dvfUbGbDZXBb3ERG7M/X9dxxRryeNiLfN3aMUIk4DdS X-Received: by 2002:a05:620a:880c:b0:77f:b795:d0ca with SMTP id qj12-20020a05620a880c00b0077fb795d0camr8858825qkn.80.1703020708170; Tue, 19 Dec 2023 13:18:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703020708; cv=none; d=google.com; s=arc-20160816; b=odTHKVHel02mOZXge63vubCURgvPm6UUO2MHzkDYa0TvjFQMaU8nd3nGa8V4g6mPfj ZJ7zkyb7uHzpabebuoH0lXpNvId7vHHiQvNSn8+bNa7Gg9o8tSr4YwUSEqR6wFI1Cu2V DfRVUNCkDhpkZpf95WL5cIP+sRMxxKl4zV22ow8aCFzmFwAem7XzGqHwbZ5kS9YgZsSt urVALxiiAZ/9RgBaix8gPoxOKU2+8btTmeuDoXO41l79WjV1cxwi0g7kU+mU/mwuCE6W 0ewMHR2az7yWn88NBo+iUyJ5bz/aKPmKQXERd3gD6v1vcwGRwH7da26TmQGcuXNXwvUb f1LA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=ui-outboundreport:content-transfer-encoding:in-reply-to:autocrypt :from:content-language:references:cc:to:subject:user-agent :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :date:message-id:dkim-signature; bh=apZyzmW7lDR/yRXCmEDFL+cPIX46ruqOcFQKx1s+RWE=; fh=cFsz07byehwvRHCn5QGBXE6yEtEjENZtcMGAPniRF3M=; b=nex18M2wsFSMNCYsCrRCd/kiu4bPKHL+MRk/wqBl/XlYv9ncFPN5Rhw02EFdLduar8 l6fb5lvGBPBdlH0kVXZg38R8jbOFmJRX1GC/4+jdfOF5rTkxuwZA/LLYXsdnUSUoNNEc lVaT3k6h5Gr5Qiz3gm9jIQY/Cp6CVUGoD2mfU9cdxmOBNgF4DwW3Ca283x6r9QpxQEJz eJXoJ2D9tzJ/rxwXsccJXQmrKVeV2lxUVj8+CmG8ox5/4zqzMZZkkXcuGIE94k0lbf7n q0gZnwWqFOa2sHV7ZW4866DD8TAlNqfs5g582D0bjcmLuQ5R+C/rH1quDLYDgRArymZm icEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmx.com header.s=s31663417 header.b=bVs8GYgG; spf=pass (google.com: domain of linux-kernel+bounces-5955-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5955-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gmx.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id c25-20020a05620a135900b0077fad36f684si10836024qkl.780.2023.12.19.13.18.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 13:18:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-5955-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@gmx.com header.s=s31663417 header.b=bVs8GYgG; spf=pass (google.com: domain of linux-kernel+bounces-5955-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5955-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gmx.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 67A951C251B2 for ; Tue, 19 Dec 2023 21:18:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BC22D3AC01; Tue, 19 Dec 2023 21:18:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmx.com header.i=quwenruo.btrfs@gmx.com header.b="bVs8GYgG" X-Original-To: linux-kernel@vger.kernel.org Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F9823D0A3; Tue, 19 Dec 2023 21:18:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmx.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.com; s=s31663417; t=1703020686; x=1703625486; i=quwenruo.btrfs@gmx.com; bh=FViu1lMsV+9j+T3nfKE2SFnPEUF8k9aoZeyz9v5+1OY=; h=X-UI-Sender-Class:Date:Subject:To:Cc:References:From: In-Reply-To; b=bVs8GYgGO3yMra5t/9n/QOijZnedp02DM4AYU9gHt3tIxtoLfigf6hrnN0dwIjPn ZVYwnFvDrxKgxXCHyLOHS7+OMjVZaU+DBfHIAOxz5JOb0hcslRkYrekMSeT96i5c7 /kdzXDazebhDLW/AR3fU8x2qJnYEK/EgIXOKLIQQCgOuS+S1o9JA11JLyVhitBcrm 3vDM5XpJT0dVE8uhPzcreGlQSUwMjNN12X5QtIFFhi09mNIYg7CAisj05YxODfZgZ 8b3meOFzfrkAiUXspv45wLMQURWP4nbcQnbxXdvgtWkPYcsMGLJ9xY7brVFj84211 zAJCEC3RFGXT4PHnMQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [172.16.0.153] ([115.64.109.135]) by mail.gmx.net (mrgmx104 [212.227.17.174]) with ESMTPSA (Nemesis) id 1MUowb-1rgThk2zlR-00QmoA; Tue, 19 Dec 2023 22:18:06 +0100 Message-ID: <4fbcab63-347f-4cef-ad35-686844c983ed@gmx.com> Date: Wed, 20 Dec 2023 07:47:58 +1030 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] lib/strtox: introduce kstrtoull_suffix() helper To: David Laight , 'David Disseldorp' , Qu Wenruo Cc: "linux-btrfs@vger.kernel.org" , Andrew Morton , Christophe JAILLET , Andy Shevchenko , "linux-kernel@vger.kernel.org" References: <11da10b4d07bf472cd47410db65dc0e222d61e83.1702628925.git.wqu@suse.com> <20231218235946.32ab7a69@echidna> <8095c6ae5f8d412d8e6ff95707961a08@AcuMS.aculab.com> Content-Language: en-US From: Qu Wenruo Autocrypt: addr=quwenruo.btrfs@gmx.com; keydata= xsBNBFnVga8BCACyhFP3ExcTIuB73jDIBA/vSoYcTyysFQzPvez64TUSCv1SgXEByR7fju3o 8RfaWuHCnkkea5luuTZMqfgTXrun2dqNVYDNOV6RIVrc4YuG20yhC1epnV55fJCThqij0MRL 1NxPKXIlEdHvN0Kov3CtWA+R1iNN0RCeVun7rmOrrjBK573aWC5sgP7YsBOLK79H3tmUtz6b 9Imuj0ZyEsa76Xg9PX9Hn2myKj1hfWGS+5og9Va4hrwQC8ipjXik6NKR5GDV+hOZkktU81G5 gkQtGB9jOAYRs86QG/b7PtIlbd3+pppT0gaS+wvwMs8cuNG+Pu6KO1oC4jgdseFLu7NpABEB AAHNIlF1IFdlbnJ1byA8cXV3ZW5ydW8uYnRyZnNAZ214LmNvbT7CwJQEEwEIAD4CGwMFCwkI BwIGFQgJCgsCBBYCAwECHgECF4AWIQQt33LlpaVbqJ2qQuHCPZHzoSX+qAUCY00iVQUJDToH pgAKCRDCPZHzoSX+qNKACACk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wXswJZfqfy8tmHM85yjObVdIG6AlnrrD In-Reply-To: <8095c6ae5f8d412d8e6ff95707961a08@AcuMS.aculab.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:HICvyFMXfPaUDJN83pc9YiVEgbF7mfJsGXcIKpn86ogTudmZY5W nOHNAopMIJv9F9ysupIj6bk9Oo5vh7ZGInOAgxtYKZ7EoroHKzFJPE8NbvQvwcoVMckohgD eJOJ3QosqxgC3H445qjHbQd9V3jf8OzoDA2PZ9/SWG2WVvohdfEhS+9dMptGK0G3nvLFW05 qkTu/1ijnExIbNkvAraGw== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:GNFMC7xY/eo=;+/M6ZtrlKHgbG9jx1/Xz1D7jeHP /8tCszuOVhKnTyWkb8WvBol2QJI90wtrtFqwR3YwqscLSypQCHEIq0MeXDFWiMOBYtJQ6tL/c RAr/lBzRKeN0+ucatXqSrK24a+HeCRagO46Chnbg7Hs8dtxBIBlcDiwKLiWyXHDKgQHHwCMiu h57ilzZvVzMdBfK4FXedpY0S6k41TNZHPJvMVztI8zhEDOlW2J294sVz0QSq85NWInZp8of/Z +4wpJ9sw8hwXm735YdDCW77q0PHlEARPuZ45ETwDzueUkc0/hacasJrSC27P9VxmSTzDC9bgg 9tM21Rk4Au75CqN3Pk0dxHpq/HFaoMfPI0D6OpL8aMaUZ3/0SS+4qW9M+WP1wPCbfhgZL3pZI 1AxqkT+UR4HoENlWTETQQS7BkUckqbPb/wc9jxA97KJmuxx046yc6DRLzQpLlIafdLeq7WeDt 5anm0lYQyf5bPXUbx6eD/3sy2UJmU8udxBjpKTL9h3DpOUQorpcebe0SGgKKRwIaC3VMn/tXG rN96NfoMCHTiPCsL5QW2DGN9S2tgpRWNCZGijKR+4KyvT07oYT7/EHntFDsfFkA8Qn58swoRM 3+zzXPYW6h23ew4EDIAOnmqvWsnMMGaxT77oH+TX2YkaQhg9a1Evb89urIZaDnDpiQc/Td32P HW0mAIaC0/FczMTqOTD8vnI8JEqciCmfevKv9tn7zU/7t49SRaJizgqw4laJfvEArAQq8nWae Wei0TJ/mMDE1NNkkphtFpJBdvCD/nloPrKZYZNN98k9Xn35QmObW1OySLlAujGoijQaRh6oUP 9Z8jyvuNRObXiMrOueiUDwBqn/9cDcx6FJRMOlFmTMoYZDy8HeRKobbkm8S4f1ltQXL2ApK1T 8js/PjovzFBvAcZ8Qui4KsNWbFa/mRAQr4zxa6lce2nBN/JbCmjMiaDjAGG1az0KnrEcQz5NK wSVfE2irIXtnRK5OPEpWgET6iIw= On 2023/12/20 03:12, David Laight wrote: > From: David Disseldorp >> Sent: 18 December 2023 13:00 >> >> On Fri, 15 Dec 2023 19:09:23 +1030, Qu Wenruo wrote: >> >>> Just as mentioned in the comment of memparse(), the simple_stroull() >>> usage can lead to overflow all by itself. >>> >>> Furthermore, the suffix calculation is also super overflow prone becau= se >>> that some suffix like "E" itself would eat 60bits, leaving only 4 bits >>> available. >>> >>> And that suffix "E" can also lead to confusion since it's using the sa= me >>> char of hex Ox'E'. >>> >>> One simple example to expose all the problem is to use memparse() on >>> "25E". >>> The correct value should be 28823037615171174400, but the suffix E mak= es >>> it super simple to overflow, resulting the incorrect value >>> 10376293541461622784 (9E). > > Some more bikeshed paint :-) > ... >>> + ret =3D _kstrtoull(s, base, &init_value, &endptr); >>> + /* Either already overflow or no number string at all. */ >>> + if (ret < 0) >>> + return ret; >>> + final_value =3D init_value; >>> + /* No suffixes. */ >>> + if (!*endptr) >>> + goto done; > > How about: > suffix =3D *endptr; > if (!strchr(suffixes, suffix)) > return -ENIVAL; > shift =3D strcspn("KkMmGgTtPp", suffix)/2 * 10 + 10; This means the caller has to provide the suffix string in this particular order. For default suffix list it's not that hard as it's already defined as a macro. But for those call sites which needs "E", wrongly located "Ee" can screw up the whole process. > if (shift > 50) > return -EINVAL; > if (value >> (64 - shift)) > return -EOVERFLOW; > value <<=3D shift; > > Although purists might want to multiply by 1000 not 1024. > And SI multipliers are all upper-case - except k. > > ... >>> + /* Overflow check. */ >>> + if (final_value < init_value) >>> + return -EOVERFLOW; > > That is just plain wrong. Indeed, I just found a very simple example to prove it wrong, 4 bit binary 0110, left shift 2, result is 1000, still larger than the original one. Thanks, Qu > > David > > - > Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK= 1 1PT, UK > Registration No: 1397386 (Wales) > >