pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: hackerzheng666@gmail.com
Subject: BUG #19910: json(b)_to_tsvector() nested array filter gives internal error XX000 "unexpected end of flag array"
Date: Tue, 17 Nov 2026 05:03:22 +0000
Message-ID: <19910-24199f8af1474753@postgresql.org> (raw)

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








view thread (3+ messages)  latest in thread

Message-ID: <19910-24199f8af1474753@postgresql.org>
Permalink:  ../19910-24199f8af1474753@postgresql.org/
Also on:    postgresql.org/message-id/19910-24199f8af1474753@postgresql.org

 · 

reply

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-bugs@postgresql.org
  Cc: noreply@postgresql.org, hackerzheng666@gmail.com
  Subject: Re: BUG #19910: json(b)_to_tsvector() nested array filter gives internal error XX000 "unexpected end of flag array"
  In-Reply-To: <19910-24199f8af1474753@postgresql.org>
  References: <19910-24199f8af1474753@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