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 1k2c38-0007lt-Bv for pgsql-hackers@arkaria.postgresql.org; Mon, 03 Aug 2020 15:06:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k2c37-0007yF-Ae for pgsql-hackers@arkaria.postgresql.org; Mon, 03 Aug 2020 15:06:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k2c36-0007y8-UN for pgsql-hackers@lists.postgresql.org; Mon, 03 Aug 2020 15:06:49 +0000 Received: from mail-qt1-x82a.google.com ([2607:f8b0:4864:20::82a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1k2c32-0002ni-QJ for pgsql-hackers@lists.postgresql.org; Mon, 03 Aug 2020 15:06:47 +0000 Received: by mail-qt1-x82a.google.com with SMTP id b25so28433486qto.2 for ; Mon, 03 Aug 2020 08:06:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=6nyNEztrcTfFjJe+HeaxljyhUm3S1LBG5sl6XcjcrhI=; b=otkZXTA2G+CARyiU4I/ttgqgb/4THHbk4tq6AhNVEypqii5SB/oajyjC9sVhg3mhXT B6QKqKVoLZ5jBLXCUXILyBgWa+tjPMfHzujnfbHhw0WCLA63VAA3inAzqfndcL3IEyAx bDWNtZk6hyjoBnRjLB+aaRfKU4gHmR6L+nTUJ+NcXzSj2C1QwJeSww8LY5B6IfaGta5x hJXMgZaY9fOzJPeNexa5LlJOoUHclzvN1X4s1E8DVBLi8/llLefMtOso3iO7zUwpwF6g 9ydMURJ8g8+5rH5tkUtSBctFR2bDZzgPi0N8Kch0Xs9HSk6N9Do6JSf3X2x0FaikcH5r Di7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=6nyNEztrcTfFjJe+HeaxljyhUm3S1LBG5sl6XcjcrhI=; b=cVXd51RbkRThFHO44seIs0YQ/u9beSl6i5ueZqqidbGVY8Ezhe+vqiPMC/0Wg2herL oiZf0wyPBr5qqTsdfzB8HTBT2wUnanvJxEnuFRMYJdQ563B8I0Q2eWHmQfm1aeqpPrCk 0ZdCs+zhtaj6ZtpAyxH02K+IKUosctwt/buXlCQrWgIMbcIyvsX6JDQRwMZKQc6uWXM1 BEWZO9XodWY9b/b9o39W02uIlcG9UKBFX/iM5GfwjDwo1HLXxxdUioI/1DwYfzNtUYX2 os9vrnR6My6jEhB8/4RZVjDVY7bFdbK3+G65fh2DJwryFFiQAY9DLQEW1wM8SiLUA5B2 Vreg== X-Gm-Message-State: AOAM5313UzjjmLR2ESM6HiaGJctuZd9X4dUnlZYa2cAo3nBYjDiC9qy5 Ve8+OKMbvjkNmzvFV+ZkwR7TG+e7PzDT/oi7PGQVcE74/7hcqln6jC0AYBJWtbQQSzOvmeMYVUs r3zkCGD7WNMRT0rkKKtmwe8BijON5QtcrZXOW9eXg5YJV+TorX/ctsfyV7GzcUkCiODBoDW6+OS GcTXUO28xbDBNNiw== X-Google-Smtp-Source: ABdhPJwZqBbcpryLPUjvXL+0WbhV/XWSQiKrf9jULiCgko5dwvlTdLQHHbZ9oadH8H/iQ1Q+4Vq+mA== X-Received: by 2002:ac8:71cf:: with SMTP id i15mr17017049qtp.119.1596467203540; Mon, 03 Aug 2020 08:06:43 -0700 (PDT) Received: from perhan.alvh.no-ip.org ([190.95.19.47]) by smtp.gmail.com with ESMTPSA id p189sm19703915qkb.61.2020.08.03.08.06.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2020 08:06:42 -0700 (PDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 01B1D2A39AC; Mon, 3 Aug 2020 11:06:40 -0400 (-04) Date: Mon, 3 Aug 2020 11:06:40 -0400 From: Alvaro Herrera To: Asim Praveen Cc: Georgios , PostgreSQL Hackers Subject: Re: [PATCH] - Provide robust alternatives for replace_string Message-ID: <20200803150640.GA30310@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2020-Aug-03, Asim Praveen wrote: > Thank you Alvaro for reviewing the patch! > > > On 01-Aug-2020, at 7:22 AM, Alvaro Herrera wrote: > > > > What happens if a replacement string happens to be split in the middle > > by the fgets buffering? I think it'll fail to be replaced. This > > applies to both versions. > > Can a string to be replaced be split across multiple lines in the source file? If I understand correctly, fgets reads one line from input file at a time. If I do not, in the worst case, we will get an un-replaced string in the output, such as “@abs_dir@“ and it should be easily detected by a failing diff. I meant what if the line is longer than 1023 chars and the replace marker starts at byte 1021, for example. Then the first fgets would get "@ab" and the second fgets would get "s_dir@" and none would see it as replaceable. > > In the stringinfo version it seemed to me that using pnstrdup is > > possible to avoid copying trailing bytes. > > That’s a good suggestion. Using pnstrdup would look like this: > > --- a/src/test/regress/pg_regress.c > +++ b/src/test/regress/pg_regress.c > @@ -465,7 +465,7 @@ replace_stringInfo(StringInfo string, const char *replace, const char *replaceme > > while ((ptr = strstr(string->data, replace)) != NULL) > { > - char *dup = pg_strdup(string->data); > + char *dup = pnstrdup(string->data, string->maxlen); I was thinking pnstrdup(string->data, ptr - string->data) to avoid copying the chars beyond ptr. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services