Skip to content

reset parse_into handlers at the start of each document - #1226

Open
GeekQAQ wants to merge 1 commit into
boostorg:developfrom
GeekQAQ:fix-into-handler
Open

GeekQAQ wants to merge 1 commit into
boostorg:developfrom
GeekQAQ:fix-into-handler

Conversation

@GeekQAQ

@GeekQAQ GeekQAQ commented Nov 20, 2026

Copy link
Copy Markdown

A parser_for<T> cannot be used for another document after reset():

  • after a complete document, every later document fails with error::extra_data;
  • after a document that stopped in the middle of a value (incomplete input written with more = true, or an error), the next document continues from where the previous one stopped: a std::string target gets the new string appended to the old partial one, and a sequence fails with a type error.

basic_parser::reset() is documented to discard the current parsing state "to prepare for parsing a new document", and a new parser_for parses the same documents fine.

Repro (develop e93b106, clang 21, libc++):

#include <boost/json.hpp>
#include <boost/json/basic_parser_impl.hpp>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

namespace json = boost::json;

// parse `first` (or only its first `n` characters, with more = true),
// reset(), then parse `second` with the same parser_for;
// with no `first`, parse `second` with a new parser_for
template<class T>
void run(std::string const& first, std::size_t n, char const* second)
{
    T v{};
    json::parser_for<T> p(json::parse_options(), &v);
    boost::system::error_code ec;
    if(!first.empty())
    {
        p.write_some(n < first.size(), first.data(), n, ec);
        p.reset();
        ec = {};
    }
    p.write_some(false, second, std::strlen(second), ec);
    std::cout << ec.message() << ", done=" << p.done()
              << ", value=" << json::serialize(json::value_from(v)) << "\n";
}

int main()
{
    std::string a = R"(["a","b"])";
    std::string s = R"("first-document")";

    // previous document complete
    run<std::vector<std::string>>(a, a.size(), R"(["c"])");
    run<std::string>(s, s.size(), R"("second")");
    // previous document stopped in the middle of a string
    run<std::vector<std::string>>(a, 4, R"(["c"])");
    run<std::string>(s, 6, R"("second")");
    // new parser_for, for comparison
    run<std::vector<std::string>>("", 0, R"(["c"])");
    run<std::string>("", 0, R"("second")");
}

Output on develop:

extra data, done=0, value=["a","b"]
extra data, done=0, value="first-document"
value is not a string, done=0, value=["a"]
Success, done=1, value="firstsecond"
Success, done=1, value=["c"]
Success, done=1, value="second"

With this change the first four lines are the same as the last two.

Cause: basic_parser::reset() resets the parser but not the handler, and into_handler::on_document_begin does nothing. into_handler::on_document_end sets inner_active_ to false, which then rejects the first event of the next document. The nested handlers keep the progress of the value they are in: the string handler's cleared_ is set by the first on_string_part and cleared again by on_string, and a sequence handler's inner_active_ stays true until its array ends, so a document that stops inside a value leaves them in the middle of it.

Fix: in into_handler::on_document_begin, re-create the nested handler (destroy it and construct it again with the same value and parent) and set inner_active_ back to true. Re-creating the handler covers every handler kind (strings, sequences, maps, tuples, variants, optionals, described structs) without adding a reset to each of them.

Test: testParseIntoValue in test/parse_into/common.hpp now reuses its parser_for after reset(), once after the complete document and once after a document that stops one character short of the end, and compares the result with value_to. On develop this fails in all seven parse_into test files; with the change they all pass (built directly with clang 21 and libc++ under ASan and UBSan, no reports).

basic_parser::reset() discards the parser state but leaves the handler
alone, and into_handler never restored its own state either:

* on_document_end sets inner_active_ to false, so once a parser_for has
  finished a document, every document after reset() fails with
  error::extra_data;
* the nested handlers keep the progress of the value they are in (the
  string handler's cleared_, the sequence handler's inner_active_, ...),
  so after a document that stopped in the middle of a value, the next
  document continues from there: a string is appended to the previous
  partial string, and a sequence fails with a type error.

Re-create the nested handler and reactivate it in on_document_begin.
Copilot AI balanced review requested due to automatic review settings November 20, 2026 14:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@cppalliance-bot

Copy link
Copy Markdown

An automated preview of the documentation is available at https://1226.json.prtest2.cppalliance.org/libs/json/doc/html/index.html

If more commits are pushed to the pull request, the docs will rebuild at the same URL.

2026-11-20 14:47:26 UTC

@cppalliance-bot

Copy link
Copy Markdown

GCOVR code coverage report https://1226.json.prtest2.cppalliance.org/gcovr/index.html
LCOV code coverage report https://1226.json.prtest2.cppalliance.org/genhtml/index.html
Coverage Diff Report https://1226.json.prtest2.cppalliance.org/diff-report/index.html

Build time: 2026-11-20 15:16:43 UTC

@cppalliance-bot

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants