Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cQpKx-0002vS-F4 for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Jan 2017 05:51:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cQpKw-0006gX-EB for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Jan 2017 05:51:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cQpKv-0006fw-80 for pgsql-hackers@postgresql.org; Tue, 10 Jan 2017 05:51:09 +0000 Received: from mail-qk0-x229.google.com ([2607:f8b0:400d:c09::229]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1cQpKr-0001kS-Bh for pgsql-hackers@postgresql.org; Tue, 10 Jan 2017 05:51:08 +0000 Received: by mail-qk0-x229.google.com with SMTP id u25so548370166qki.2 for ; Mon, 09 Jan 2017 21:51:04 -0800 (PST) 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=15JJ2HVpN37ioPcJIRevYkkTeVepSckTCyNy8EWBrVg=; b=DvHBPIvBGkgekiWwTizIwW1F+8VxPc6KMc4AqBukD8ydHa2Mw5A4CUCaKEzSk4djz2 XAlgmz5sCE3kpOM0PI8497GZ5o2m+N4LITwem+nEt83tE+8PKhBy3cKe7GzzbUmFAMW0 a53gjVebXT7pPXhlMk/dAfTQtzBUGqcchYohk/ielBJyTk/xlYfrKQ8B1oaqRJpwnTEM bXHkFJgcdQ8rvXDEeMAahVKBKbzKiNuDSvxFX8R9uSEKi8Zhwo2lnVCtpE1vRtZf61Xf Nwn3DnC7Pi3movLYpVu3/d4IfwkXKiP375rwZ9edPteH8oAqU8h2smHXo+pufqF7aGFs bsjg== 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=15JJ2HVpN37ioPcJIRevYkkTeVepSckTCyNy8EWBrVg=; b=oVIwfbgHxxNLHtgnyox7xM+/qoxe0JuSsH63nixj2YEQQsj3Qbs+FyjwS3P4FDV4nO nys2yqdylyS6SVe1iiE8SecjNxGMKpqTxe+3YmUgTBFTAnpV5152l63+1oKvTbW2VyY5 KoIlyRgJXS9sZNizXGQBKYhlm0iWaDUDaryb8fRoDJjk8q75glrz6kMGuq+XQ2zWDadE nnIANFO1LPRTohlQNeoUg9jBD7DcWIkwsLQl+M5M/IMs0kKkTPEHGYP2fMq4CSXozbJY woq8SGPu7EOnWLYe/F7A3P2r/s+dJ6kXnYsOqZ5GmAb3jBLjncRD60YoOwqIibBRO4ED BfCw== X-Gm-Message-State: AIkVDXLh8e5rNTByXOiMvJTAEu4U/09vTROdr0ajsGJjEvoa0ypJ9lzEqWBjaaptoC8nheIe X-Received: by 10.55.75.134 with SMTP id y128mr1158703qka.134.1484027463544; Mon, 09 Jan 2017 21:51:03 -0800 (PST) Received: from alvin.alvh.no-ip.org ([181.42.144.42]) by smtp.gmail.com with ESMTPSA id i187sm678641qkd.20.2017.01.09.21.51.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Jan 2017 21:51:02 -0800 (PST) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id 5F8FC1590; Tue, 10 Jan 2017 02:50:57 -0300 (CLST) Date: Tue, 10 Jan 2017 02:50:57 -0300 From: Alvaro Herrera To: Daniel Verite Cc: Tomas Vondra , Craig Ringer , Tom Lane , Robert Haas , Jim Nasby , Ronan Dunklau , pgsql-hackers Subject: Re: pg_dump / copy bugs with "big lines" ? Message-ID: <20170110055057.utnaxegvbuwslpex@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="yyabnzzuclxzmixj" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1706e85e-60d2-494e-8a64-9af1e1b2186e@manitou-mail.org> User-Agent: NeoMutt/20161104 (1.7.1) X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --yyabnzzuclxzmixj Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Daniel Verite wrote: > My tests are OK too but I see an issue with the code in > enlargeStringInfo(), regarding integer overflow. > The bit of comment that says: > > Note we are assuming here that limit <= INT_MAX/2, else the above > loop could overflow. > > is obsolete, it's now INT_MAX instead of INT_MAX/2. I would keep this comment but use UINT_MAX/2 instead. > There's a related problem here: > newlen = 2 * str->maxlen; > while (needed > newlen) > newlen = 2 * newlen; > str->maxlen is an int going up to INT_MAX so [2 * str->maxlen] now > *will* overflow when [str->maxlen > INT_MAX/2]. > Eventually it somehow works because of this: > if (newlen > limit) > newlen = limit; > but newlen is wonky (when resulting from int overflow) > before being brought back to limit. Yeah, you're right. We also need to cast "needed" to Size in the while test; and the repalloc_huge() call no longer needs a cast. I propose the attached. Not sure if we also need to cast the assignment to str->maxlen in the last line. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --yyabnzzuclxzmixj Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: attachment; filename="fix-stringinfo.patch" Content-Transfer-Encoding: quoted-printable commit 1327c2eea9c7ca074d88bb167bd4c35338d2de0b=1B[m Author: Alvaro Herrera AuthorDate: Tue Jan 10 02:46:42 2017 -0300 CommitDate: Tue Jan 10 02:46:42 2017 -0300 Fix overflow check in StringInfo =20=20=20=20 A thinko I introduced in fa2fa9955280. Also, amend a similarly broken comment. =20=20=20=20 Report and patch by Daniel V=E9rit=E9. Discussion: https://postgr.es/m/1706e85e-60d2-494e-8a64-9af1e1b2186e@ma= nitou-mail.org diff --git a/src/backend/lib/stringinfo.c b/src/backend/lib/stringinfo.c index bdc204e..11d751a 100644 --- a/src/backend/lib/stringinfo.c +++ b/src/backend/lib/stringinfo.c @@ -313,19 +313,19 @@ enlargeStringInfo(StringInfo str, int needed) * for efficiency, double the buffer size each time it overflows. * Actually, we might need to more than double it if 'needed' is big... */ - newlen =3D 2 * str->maxlen; - while (needed > newlen) + newlen =3D 2 * (Size) str->maxlen; + while ((Size) needed > newlen) newlen =3D 2 * newlen; =20 /* * Clamp to the limit in case we went past it. Note we are assuming here - * that limit <=3D INT_MAX/2, else the above loop could overflow. We will + * that limit <=3D UINT_MAX/2, else the above loop could overflow. We wi= ll * still have newlen >=3D needed. */ if (newlen > limit) newlen =3D limit; =20 - str->data =3D (char *) repalloc_huge(str->data, (Size) newlen); + str->data =3D (char *) repalloc_huge(str->data, newlen); =20 str->maxlen =3D newlen; } --yyabnzzuclxzmixj Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --yyabnzzuclxzmixj--