pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix integer-overflow corner cases in substring() functions.
Date: Mon, 04 Jan 2021 23:33:05 +0000
Message-ID: <E1kwZLV-0002yx-D3@gemulon.postgresql.org> (raw)

Fix integer-overflow corner cases in substring() functions.

If the substring start index and length overflow when added together,
substring() misbehaved, either throwing a bogus "negative substring
length" error on a case that should succeed, or failing to complain that
a negative length is negative (and instead returning the whole string,
in most cases).  Unsurprisingly, the text, bytea, and bit variants of
the function all had this issue.  Rearrange the logic to ensure that
negative lengths are always rejected, and add an overflow check to
handle the other case.

Also install similar guards into detoast_attr_slice() (nee
heap_tuple_untoast_attr_slice()), since it's far from clear that
no other code paths leading to that function could pass it values
that would overflow.

Patch by myself and Pavel Stehule, per bug #16804 from Rafi Shamim.

Back-patch to v11.  While these bugs are old, the common/int.h
infrastructure for overflow-detecting arithmetic didn't exist before
commit 4d6ad3125, and it doesn't seem like these misbehaviors are bad
enough to justify developing a standalone fix for the older branches.

Discussion: https://postgr.es/m/16804-f4eeeb6c11ba71d4@postgresql.org

Branch
------
REL_13_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/9e7d87ca84b91b0c6d8cb052bb6193881f6861fb

Modified Files
--------------
src/backend/access/common/detoast.c   |  33 +++++++---
src/backend/utils/adt/varbit.c        |  26 ++++----
src/backend/utils/adt/varlena.c       | 110 ++++++++++++++++++++--------------
src/test/regress/expected/bit.out     |  29 +++++++++
src/test/regress/expected/strings.out |  41 +++++++++++++
src/test/regress/sql/bit.sql          |   8 +++
src/test/regress/sql/strings.sql      |  11 ++++
7 files changed, 195 insertions(+), 63 deletions(-)



view thread (4+ messages)  latest in thread

Message-ID: <E1kwZLV-0002yx-D3@gemulon.postgresql.org>
Permalink:  ../E1kwZLV-0002yx-D3@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1kwZLV-0002yx-D3@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Fix integer-overflow corner cases in substring() functions.
  In-Reply-To: <E1kwZLV-0002yx-D3@gemulon.postgresql.org>
  References: <E1kwZLV-0002yx-D3@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox