jsonapi: add lexer option to keep token ownership

Commit 0785d1b8b adds support for libpq as a JSON client, but
allocations for string tokens can still be leaked during parsing
failures. This is tricky to fix for the object_field semantic callbacks:
the field name must remain valid until the end of the object, but if a
parsing error is encountered partway through, object_field_end() won't
be invoked and the client won't get a chance to free the field name.

This patch adds a flag to switch the ownership of parsed tokens to the
lexer. When this is enabled, the client must make a copy of any tokens
it wants to persist past the callback lifetime, but the lexer will
handle necessary cleanup on failure.

Backend uses of the JSON parser don't need to use this flag, since the
parser's allocations will occur in a short lived memory context.

A -o option has been added to test_json_parser_incremental to exercise
the new setJsonLexContextOwnsTokens() API, and the test_json_parser TAP
tests make use of it. (The test program now cleans up allocated memory,
so that tests can be usefully run under leak sanitizers.)

Author: Jacob Champion

Discussion: 
https://postgr.es/m/caoymi+kb38eciwybqof9peapkgwrahqa7pgzbkvounw5brf...@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/5c32c21afe6449a19b6dfafa17f29b71c9595e03

Modified Files
--------------
src/common/jsonapi.c                               | 102 +++++++++++++++++++--
src/include/common/jsonapi.h                       |  28 +++++-
.../t/001_test_json_parser_incremental.pl          |  13 ++-
src/test/modules/test_json_parser/t/002_inline.pl  |  15 +--
.../test_json_parser/t/003_test_semantic.pl        |  11 ++-
.../test_json_parser_incremental.c                 |  37 ++++++--
6 files changed, 173 insertions(+), 33 deletions(-)

Reply via email to