pgsql: Fix integer-overflow corner cases in substring() functions. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Fix integer-overflow corner cases in substring() functions.
Date
Msg-id E1kwZLV-0002z3-Dq@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
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_11_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/50a420bee0c28595974a07a0f2175d677fb6a642

Modified Files
--------------
src/backend/access/heap/tuptoaster.c  |  27 ++++++++-
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 |  43 ++++++++++++-
src/test/regress/sql/bit.sql          |   8 +++
src/test/regress/sql/strings.sql      |  13 +++-
7 files changed, 195 insertions(+), 61 deletions(-)


pgsql-committers by date:

Previous
From: Thomas Munro
Date:
Subject: pgsql: Remove unused function prototypes.
Next
From: Thomas Munro
Date:
Subject: pgsql: Rename "enum blacklist" to "uncommitted enums".