pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #19910: json(b)_to_tsvector() nested array filter gives internal error XX000 "unexpected end of flag array"
3+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2026-11-18 9b937d1d1d  Improve error messages in parse_jsonb_index_flags().
* BUG #19910: json(b)_to_tsvector() nested array filter gives internal error XX000 "unexpected end of flag array"
@ 2026-11-17 05:03 PG Bug reporting form <noreply@postgresql.org>
  2026-11-17 15:41 ` Re: BUG #19910: json(b)_to_tsvector() nested array filter gives internal error XX000 "unexpected end of flag array" Tom Lane <tgl@sss.pgh.pa.us>
  2026-11-17 19:00 ` Re: BUG #19910: json(b)_to_tsvector() nested array filter gives internal error XX000 "unexpected end of flag array" Alexander Lakhin <exclusion@gmail.com>
  0 siblings, 2 replies; 3+ messages in thread

From: PG Bug reporting form @ 2026-11-17 05:03 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: hackerzheng666@gmail.com

The following bug has been logged on the website:

Bug reference:      19910
Logged by:          Zheng Hacker
Email address:      hackerzheng666@gmail.com
PostgreSQL version: 19rc2
Operating system:   Linux x86_64 (Ubuntu 22.04), clang 14, cassert
Description:        

A filter argument that contains a nested array is rejected with an
internal-error message (SQLSTATE XX000, raised by elog) instead of the
invalid_parameter_value error that every other bad filter gets:

select jsonb_to_tsvector('english'::regconfig, '{"a":"b c"}'::jsonb,
'[[1]]'::jsonb);
ERROR:  XX000: unexpected end of flag array
LOCATION:  parse_jsonb_index_flags, jsonfuncs.c:5643

select json_to_tsvector('english'::regconfig, '{}'::json, '[[1]]'::jsonb);
ERROR:  XX000: unexpected end of flag array

For comparison, other invalid filters give a proper error with a hint:

select jsonb_to_tsvector('english'::regconfig, '{"a":"b c"}'::jsonb,
'["nope"]'::jsonb);
ERROR:  22023: wrong flag in flag array: "nope"
HINT:  Possible values are: "string", "numeric", "boolean", "key", and
"all".

select jsonb_to_tsvector('english'::regconfig, '{"a":"b c"}'::jsonb,
'{"x":1}'::jsonb);
ERROR:  22023: wrong flag type, only arrays and scalars are allowed

select jsonb_to_tsvector('english'::regconfig, '{"a":"b c"}'::jsonb,
'[1]'::jsonb);   -- per the code: "flag array element is not a string"

Cause, from reading src/backend/utils/adt/jsonfuncs.c,
parse_jsonb_index_flags(): the loop

    while ((type = JsonbIteratorNext(&it, &v, false)) == WJB_ELEM)
    {
        if (v.type != jbvString) ereport(ERROR, ... "flag array element is
not a string" ...);
        ...
    }
    /* expect end of array now */
    if (type != WJB_END_ARRAY)
        elog(ERROR, "unexpected end of flag array");

only continues while the iterator returns WJB_ELEM. A nested array element
makes the iterator return WJB_BEGIN_ARRAY, the loop exits, the token is not
WJB_END_ARRAY, and the "cannot happen" elog fires. So user input reaches an
internal-error path. I did not test a patch. Probably the loop should handle
the nested container the same way as a non-string element (report "flag
array element is not a string" with the hint), or the check after the loop
should raise ERRCODE_INVALID_PARAMETER_VALUE.

Found by a systematic call of every SQL-callable function with boundary and
structured arguments on a cassert build; reproduced 6 of 6 times on a fresh
cluster. Severity: low (wrong SQLSTATE and message for invalid input, no
crash).

Tested on git master (20devel, commit
5ffc82103773ea1ab5aa699edc301d4f03de4ce). Master HEAD 7fc2f0423320
(2026-11-16) has the same code in parse_jsonb_index_flags()
(jsonfuncs.c:5643; read from master, not built).








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

* Re: BUG #19910: json(b)_to_tsvector() nested array filter gives internal error XX000 "unexpected end of flag array"
  2026-11-17 05:03 BUG #19910: json(b)_to_tsvector() nested array filter gives internal error XX000 "unexpected end of flag array" PG Bug reporting form <noreply@postgresql.org>
@ 2026-11-17 15:41 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 3+ messages in thread

From: Tom Lane @ 2026-11-17 15:41 UTC (permalink / raw)
  To: hackerzheng666@gmail.com; +Cc: pgsql-bugs@lists.postgresql.org

PG Bug reporting form <noreply@postgresql.org> writes:
> A filter argument that contains a nested array is rejected with an
> internal-error message (SQLSTATE XX000, raised by elog) instead of the
> invalid_parameter_value error that every other bad filter gets:

Hmm, yeah.  The tests in this code are actually correct, but not
a lot of thought was put into the error messages.  Need something
like the attached.  (I think the last elog() call is indeed
unreachable, but maybe I'm wrong?)

			regards, tom lane

Attachments:

draft-fix-jsonb-flag-parsing.patch (text/x-diff, 3.0K) download diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c index 438f9f23ee0..25f14f92370 100644 --- a/src/backend/utils/adt/jsonfuncs.c +++ b/src/backend/utils/adt/jsonfuncs.c @@ -5604,8 +5604,10 @@ parse_jsonb_index_flags(Jsonb *jb) * chosen the same as jsonb_typeof uses. */ if (type != WJB_BEGIN_ARRAY) - ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("wrong flag type, only arrays and scalars are allowed"))); + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("incorrect flag structure"), + errdetail("Flags argument must be a string or array of strings."))); while ((type = JsonbIteratorNext(&it, &v, false)) == WJB_ELEM) { @@ -5640,12 +5642,15 @@ parse_jsonb_index_flags(Jsonb *jb) /* expect end of array now */ if (type != WJB_END_ARRAY) - elog(ERROR, "unexpected end of flag array"); + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("incorrect flag structure"), + errdetail("Flags argument must be a string or array of strings."))); /* get final WJB_DONE and free iterator */ type = JsonbIteratorNext(&it, &v, false); if (type != WJB_DONE) - elog(ERROR, "unexpected end of flag array"); + elog(ERROR, "unexpected items after flag array"); return flags; } diff --git a/src/test/regress/expected/json.out b/src/test/regress/expected/json.out index 5411003e2e6..461c8781752 100644 --- a/src/test/regress/expected/json.out +++ b/src/test/regress/expected/json.out @@ -2785,7 +2785,8 @@ select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": ERROR: wrong flag in flag array: "" HINT: Possible values are: "string", "numeric", "boolean", "key", and "all". select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '{}'); -ERROR: wrong flag type, only arrays and scalars are allowed +ERROR: incorrect flag structure +DETAIL: Flags argument must be a string or array of strings. select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '[]'); json_to_tsvector ------------------ diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out index efcbcddc1b3..1e8bdd08168 100644 --- a/src/test/regress/expected/jsonb.out +++ b/src/test/regress/expected/jsonb.out @@ -5741,7 +5741,8 @@ select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d" ERROR: wrong flag in flag array: "" HINT: Possible values are: "string", "numeric", "boolean", "key", and "all". select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '{}'); -ERROR: wrong flag type, only arrays and scalars are allowed +ERROR: incorrect flag structure +DETAIL: Flags argument must be a string or array of strings. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '[]'); jsonb_to_tsvector -------------------
^ permalink raw reply [nested|flat] 3+ messages in thread

* Re: BUG #19910: json(b)_to_tsvector() nested array filter gives internal error XX000 "unexpected end of flag array"
  2026-11-17 05:03 BUG #19910: json(b)_to_tsvector() nested array filter gives internal error XX000 "unexpected end of flag array" PG Bug reporting form <noreply@postgresql.org>
@ 2026-11-17 19:00 ` Alexander Lakhin <exclusion@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Alexander Lakhin @ 2026-11-17 19:00 UTC (permalink / raw)
  To: hackerzheng666@gmail.com, Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs@lists.postgresql.org

Hello Zheng and Tom,

17.11.2026 07:03, PG Bug reporting form wrote:
> The following bug has been logged on the website:
>
> Bug reference:      19910
> Logged by:          Zheng Hacker
> ...
> select jsonb_to_tsvector('english'::regconfig, '{"a":"b c"}'::jsonb,
> '[[1]]'::jsonb);
> ERROR:  XX000: unexpected end of flag array
> LOCATION:  parse_jsonb_index_flags, jsonfuncs.c:5643

FWIW, I also reported this before:
https://www.postgresql.org/message-id/f2758799-b7c1-4baf-8593-8899802cf48c%40gmail.com

Maybe other internal errors from that thread worth fixing too...

Best regards,
Alexander

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


end of thread, other threads:[~2026-11-17 19:00 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 05:03 BUG #19910: json(b)_to_tsvector() nested array filter gives internal error XX000 "unexpected end of flag array" PG Bug reporting form <noreply@postgresql.org>
2026-11-17 15:41 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-11-17 19:00 ` Alexander Lakhin <exclusion@gmail.com>

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