Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sP5JJ-002onn-D1 for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Jul 2024 19:06:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sP5JH-00BThK-E4 for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Jul 2024 19:06:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sP5JG-00BThB-Jb for pgsql-hackers@lists.postgresql.org; Wed, 03 Jul 2024 19:06:31 +0000 Received: from fhigh8-smtp.messagingengine.com ([103.168.172.159]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sP5JE-000GTl-FX for pgsql-hackers@postgresql.org; Wed, 03 Jul 2024 19:06:30 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 7858C1140211; Wed, 3 Jul 2024 15:06:25 -0400 (EDT) Received: from imap48 ([10.202.2.98]) by compute6.internal (MEProxy); Wed, 03 Jul 2024 15:06:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1720033585; x=1720119985; bh=LhorTBgoF2 yFf+iIwLAjBfcuYGTLuDqRKBs4/wudUzI=; b=OP5jzyH2ZM3wZ+ht9mvE1EFebG YW48+e0IkQzMRKZWggtDGoUsu9ZzH9ixzdGl+WcML0WN6uBS9vgY+JJAhtdz8Gp2 Cw0nXac/wqsU2F1OsC47QdcAwjFW2tJOQ/7EgLQEGAJ/74O18ob8jJNd9903AWhw L70RuXNvA1Q3oy7VPWVhVTaPZqPRgOkJOgZRVnVUl4iCJgDDKcqFbEqY/1a/4gLP 2PZqywKTyzhLn/dk1kmufvUzcbHTwr9/LrfN0ULKVPd8Jo2Ao53i4sxS2qdNTGuC 8ZOQTRBxesFR9EIaWcFKgetJ0yMuuiIyWcuHI5MKFzIRBU8XsF99eZrLQdUw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1720033585; x=1720119985; bh=LhorTBgoF2yFf+iIwLAjBfcuYGTL uDqRKBs4/wudUzI=; b=BhA0O++hNrPGk6vEz77Oo9gDIV+Z/WrjeB+UFqK7ntiu 3ks0J42MnLs2PI3Q47b2iFvf/jax64LkcV1AmkfGicpSLJ5ORlD2idnf6+nkcxni QyLvgT2kdxT+3kXMyyJGzhJmY1qU0j+Bqb2RWn8dE2SUJCAGqPjmL9kJIMcu9nzr yZNwwcY+13wkvUXVLJxcqi87XIulG26CV/FYesNQyDA6l/XMdWym4F0xV1fJFVJu T7M+UbptKdD2PlFFlpLsbRR/FKKeHp7AeOcWrweU0fNhaJ2sj5wPLLeOqAoDPgsN QWqQV+Olpu9U5g2X22kIUo79g9Pq2HrwGcn2Xjb3XQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudejgddufedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvfevufgtsehttdertderredtnecuhfhrohhmpedflfho vghlucflrggtohgsshhonhdfuceojhhovghlsegtohhmphhilhgvrhdrohhrgheqnecugg ftrfgrthhtvghrnhephfefudegffdvtdetleeuhfffkeeltddukefhkeekieelleffhfef tdffjefgjeefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrh homhepjhhovghlsegtohhmphhilhgvrhdrohhrgh X-ME-Proxy: Feedback-ID: ic6394509:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id D2EEA31A0066; Wed, 3 Jul 2024 15:06:24 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-566-g3812ddbbc-fm-20240627.001-g3812ddbb MIME-Version: 1.0 Message-Id: In-Reply-To: <018dd6e8-8f21-4b3b-8d80-2d128f6d00b4@app.fastmail.com> References: <44d2ffca-d560-4919-b85a-4d07060946aa@app.fastmail.com> <87zfr1e1uj.fsf@wibble.ilmari.org> <9ff90ff9-5ac1-4529-b2f4-f93b37734b9c@app.fastmail.com> <81348268-b9fd-4308-bf52-056070aa7c2a@app.fastmail.com> <8e909218-1965-4515-99e3-4bb5b625e004@app.fastmail.com> <0459ded5-6daf-4bf0-9ab0-e1f11ab15ec6@app.fastmail.com> <4e54bf4f-59fe-4c9a-b446-a2b64cbfd584@app.fastmail.com> <27d42b25-f328-4231-9db0-3811dc77c8cf@app.fastmail.com> <33c51978-8ee8-438c-9804-e46c0ca8f3f9@app.fastmail.com> <018dd6e8-8f21-4b3b-8d80-2d128f6d00b4@app.fastmail.com> Date: Wed, 03 Jul 2024 21:05:51 +0200 From: "Joel Jacobson" To: "Dean Rasheed" Cc: =?UTF-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= , pgsql-hackers Subject: Re: Optimize numeric multiplication for one and two base-NBASE digit multiplicands. Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Jul 3, 2024, at 15:48, Joel Jacobson wrote: > On Wed, Jul 3, 2024, at 13:17, Dean Rasheed wrote: >> On Tue, 2 Jul 2024 at 21:10, Joel Jacobson wrote: >>> >>> I found the bug in the case 3 code, >>> and it turns out the same type of bug also exists in the case 2 code: >>> >>> case 2: >>> newdig = (int) var1digits[1] * var2digits[res_ndigits - 4]; >>> >>> The problem here is that res_ndigits could become less than 4, >> >> Yes. It can't be less than 3 though (per an earlier test), so the case >> 2 code was correct. > > Hmm, I don't see how the case 2 code can be correct? > If, like you say, res_ndigits can't be less than 3, that means it can > be 3, right? > And if res_ndigits=3 then `var2digits[res_ndigits - 4]` would try to > access `var2digits[-1]`. Here is an example on how to trigger the bug: ``` case 2: if (res_ndigits - 4 < 0) { printf("var1=%s\n",get_str_from_var(var1)); printf("var2=%s\n",get_str_from_var(var2)); printf("rscale=%d\n", rscale); printf("res_ndigits - 4 < 0 => var2digits[%d]=%d\n", res_ndigits - 4, var2digits[res_ndigits - 4]); } ``` Running through my tests, I hit lots of cases, including: var1=0.10968501 var2=0.903728177113 rscale=0 res_ndigits - 4 < 0 => var2digits[-1]=-31105 All of the spotted cases had rscale=0. If we know that mul_var() will never be called with rscale=0 when dealing with decimal inputs, perhaps we should enforce this with an Assert(), to prevent the otherwise possible out-of-bounds access (negative indexing) and provide early detection? Regards, Joel