Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pXOeo-0003Yx-0T for pgsql-bugs@arkaria.postgresql.org; Wed, 01 Mar 2023 15:46:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pXOem-0006Uf-MF for pgsql-bugs@arkaria.postgresql.org; Wed, 01 Mar 2023 15:46:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pXOem-0006UW-Dr for pgsql-bugs@lists.postgresql.org; Wed, 01 Mar 2023 15:46:16 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pXOej-0002vF-As for pgsql-bugs@lists.postgresql.org; Wed, 01 Mar 2023 15:46:16 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 321FkAUM1399977; Wed, 1 Mar 2023 10:46:10 -0500 From: Tom Lane To: anisimow.d@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17816: Invalid memory access in translate function In-reply-to: <17816-70f3d2764e88a108@postgresql.org> References: <17816-70f3d2764e88a108@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Wed, 01 Mar 2023 08:17:56 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1399975.1677685570.1@sss.pgh.pa.us> Date: Wed, 01 Mar 2023 10:46:10 -0500 Message-ID: <1399976.1677685570@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > When running this query in psql with valgrind-enabled server: > select translate('(123)', '()', ''); > I get: > ==00:00:00:05.576 29520== Invalid read of size 1 > ==00:00:00:05.576 29520== at 0x7605BE: pg_utf_mblen (wchar.c:553) > ==00:00:00:05.576 29520== by 0x71CEDE: pg_mblen (mbutils.c:968) > ==00:00:00:05.576 29520== by 0x67B1E3: translate (oracle_compat.c:864) Thanks for the report! > These changes fix the issue: > for (i = 0; i < from_index; i++) > { > if (p >= (to_ptr + tolen)) > break; > p += pg_mblen(p); > } Yeah, I agree, we need to switch the order of those two statements. Will fix. (Might be worth pulling out the common subexpression here? I wonder if all compilers are smart enough to evaluate to_ptr + to_len just once.) regards, tom lane