BUG #19910: json(b)_to_tsvector() nested array filter gives internal error XX000 "unexpected end of flag array"

Started by PG Bug reporting form5 days ago3 messagesbugs
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

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).

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: PG Bug reporting form (#1)
Re: 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> 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:

t259279_2
draft-fix-jsonb-flag-parsing.patchtext/x-diff; charset=us-ascii; name=draft-fix-jsonb-flag-parsing.patchDownload+13-6
#3Alexander Lakhin
exclusion@gmail.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19910: json(b)_to_tsvector() nested array filter gives internal error XX000 "unexpected end of flag array"

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:
/messages/by-id/f2758799-b7c1-4baf-8593-8899802cf48c@gmail.com

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

Best regards,
Alexander