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-0002z0-DS@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_12_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/7c98759e0485d26303f2572ce7724b6026fddd9c
Modified Files
--------------
src/backend/access/heap/tuptoaster.c | 31 ++++++++--
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(+), 61 deletions(-)
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-0002z0-DS@gemulon.postgresql.org>
References: <E1kwZLV-0002z0-DS@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