pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix jsonb subscripting to cope with toasted subscript values.
3+ messages / 1 participants
[nested] [flat]

* pgsql: Fix jsonb subscripting to cope with toasted subscript values.
@ 2022-12-12 21:18  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2022-12-12 21:18 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix jsonb subscripting to cope with toasted subscript values.

jsonb_get_element() was incautious enough to use VARDATA() and
VARSIZE() directly on an arbitrary text Datum.  That of course
fails if the Datum is short-header, compressed, or out-of-line.
The typical result would be failing to match any element of a
jsonb object, though matching the wrong one seems possible as well.

setPathObject() was slightly brighter, in that it used VARDATA_ANY
and VARSIZE_ANY_EXHDR, but that only kept it out of trouble for
short-header Datums.  push_path() had the same issue.  This could
result in faulty subscripted insertions, though keys long enough to
cause a problem are likely rare in the wild.

Having seen these, I looked around for unsafe usages in the rest
of the adt/json* files.  There are a couple of places where it's not
immediately obvious that the Datum can't be compressed or out-of-line,
so I added pg_detoast_datum_packed() to cope if it is.  Also, remove
some other usages of VARDATA/VARSIZE on Datums we just extracted from
a text array.  Those aren't actively broken, but they will become so
if we ever start allowing short-header array elements, which does not
seem like a terribly unreasonable thing to do.  In any case they are
not great coding examples, and they could also do with comments
pointing out that we're assuming we don't need pg_detoast_datum_packed.

Per report from exe-dealer@yandex.ru.  Patch by me, but thanks to
David Johnston for initial investigation.  Back-patch to v14 where
jsonb subscripting was introduced.

Discussion: https://postgr.es/m/205321670615953@mail.yandex.ru

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/b0feda79fdf02710a6039a324299525845a4a9d4

Modified Files
--------------
src/backend/utils/adt/jsonb_gin.c   |  5 +++--
src/backend/utils/adt/jsonb_op.c    | 10 +++++----
src/backend/utils/adt/jsonfuncs.c   | 41 ++++++++++++++++++++++++++-----------
src/test/regress/expected/jsonb.out | 34 ++++++++++++++++++++++++++++++
src/test/regress/sql/jsonb.sql      | 18 ++++++++++++++++
5 files changed, 90 insertions(+), 18 deletions(-)



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* pgsql: Fix jsonb subscripting to cope with toasted subscript values.
@ 2022-12-12 21:18  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2022-12-12 21:18 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix jsonb subscripting to cope with toasted subscript values.

jsonb_get_element() was incautious enough to use VARDATA() and
VARSIZE() directly on an arbitrary text Datum.  That of course
fails if the Datum is short-header, compressed, or out-of-line.
The typical result would be failing to match any element of a
jsonb object, though matching the wrong one seems possible as well.

setPathObject() was slightly brighter, in that it used VARDATA_ANY
and VARSIZE_ANY_EXHDR, but that only kept it out of trouble for
short-header Datums.  push_path() had the same issue.  This could
result in faulty subscripted insertions, though keys long enough to
cause a problem are likely rare in the wild.

Having seen these, I looked around for unsafe usages in the rest
of the adt/json* files.  There are a couple of places where it's not
immediately obvious that the Datum can't be compressed or out-of-line,
so I added pg_detoast_datum_packed() to cope if it is.  Also, remove
some other usages of VARDATA/VARSIZE on Datums we just extracted from
a text array.  Those aren't actively broken, but they will become so
if we ever start allowing short-header array elements, which does not
seem like a terribly unreasonable thing to do.  In any case they are
not great coding examples, and they could also do with comments
pointing out that we're assuming we don't need pg_detoast_datum_packed.

Per report from exe-dealer@yandex.ru.  Patch by me, but thanks to
David Johnston for initial investigation.  Back-patch to v14 where
jsonb subscripting was introduced.

Discussion: https://postgr.es/m/205321670615953@mail.yandex.ru

Branch
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/a18328bb3395188befc2785dbbe47f0d84baf6d1

Modified Files
--------------
src/backend/utils/adt/jsonb_gin.c   |  5 +++--
src/backend/utils/adt/jsonb_op.c    | 10 +++++----
src/backend/utils/adt/jsonfuncs.c   | 41 ++++++++++++++++++++++++++-----------
src/test/regress/expected/jsonb.out | 34 ++++++++++++++++++++++++++++++
src/test/regress/sql/jsonb.sql      | 18 ++++++++++++++++
5 files changed, 90 insertions(+), 18 deletions(-)



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* pgsql: Fix jsonb subscripting to cope with toasted subscript values.
@ 2022-12-12 21:18  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2022-12-12 21:18 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix jsonb subscripting to cope with toasted subscript values.

jsonb_get_element() was incautious enough to use VARDATA() and
VARSIZE() directly on an arbitrary text Datum.  That of course
fails if the Datum is short-header, compressed, or out-of-line.
The typical result would be failing to match any element of a
jsonb object, though matching the wrong one seems possible as well.

setPathObject() was slightly brighter, in that it used VARDATA_ANY
and VARSIZE_ANY_EXHDR, but that only kept it out of trouble for
short-header Datums.  push_path() had the same issue.  This could
result in faulty subscripted insertions, though keys long enough to
cause a problem are likely rare in the wild.

Having seen these, I looked around for unsafe usages in the rest
of the adt/json* files.  There are a couple of places where it's not
immediately obvious that the Datum can't be compressed or out-of-line,
so I added pg_detoast_datum_packed() to cope if it is.  Also, remove
some other usages of VARDATA/VARSIZE on Datums we just extracted from
a text array.  Those aren't actively broken, but they will become so
if we ever start allowing short-header array elements, which does not
seem like a terribly unreasonable thing to do.  In any case they are
not great coding examples, and they could also do with comments
pointing out that we're assuming we don't need pg_detoast_datum_packed.

Per report from exe-dealer@yandex.ru.  Patch by me, but thanks to
David Johnston for initial investigation.  Back-patch to v14 where
jsonb subscripting was introduced.

Discussion: https://postgr.es/m/205321670615953@mail.yandex.ru

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/d79b76b10ed55ceb25377c0623f2a159ad6117b4

Modified Files
--------------
src/backend/utils/adt/jsonb_gin.c   |  5 +++--
src/backend/utils/adt/jsonb_op.c    | 10 +++++----
src/backend/utils/adt/jsonfuncs.c   | 41 ++++++++++++++++++++++++++-----------
src/test/regress/expected/jsonb.out | 34 ++++++++++++++++++++++++++++++
src/test/regress/sql/jsonb.sql      | 18 ++++++++++++++++
5 files changed, 90 insertions(+), 18 deletions(-)



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2022-12-12 21:18 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-12-12 21:18 pgsql: Fix jsonb subscripting to cope with toasted subscript values. Tom Lane <tgl@sss.pgh.pa.us>
2022-12-12 21:18 pgsql: Fix jsonb subscripting to cope with toasted subscript values. Tom Lane <tgl@sss.pgh.pa.us>
2022-12-12 21:18 pgsql: Fix jsonb subscripting to cope with toasted subscript values. Tom Lane <tgl@sss.pgh.pa.us>

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