Skip to content

Fix chunk_extensions state consistency and empty input handling - #3137

Merged
ashtum merged 2 commits into
boostorg:developfrom
mzfr:fix/chunk-extensions-state
Nov 15, 2026
Merged

ashtum merged 2 commits into
boostorg:developfrom
mzfr:fix/chunk-extensions-state

Conversation

@mzfr

@mzfr mzfr commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

All three issues were reproduced against the latest Boost.Beast develop branch before applying this patch:

  • Boost.Beast version: 362
  • Upstream commit: 200ebae2d8021d3bdf1a713d4ffbaf1521c03dc7

Bugs

  • Out-of-bounds read after a chunk-extension parse error

    do_parse() constructs range_ incrementally. A malformed quoted value such as:

    ;x="unterminated
    

    stores the extension name before detecting that its value is invalid.

    parse() previously rebuilt the serialized representation only after a successful parse:

    do_parse(s.data(), s.data() + s.size(), ec);
    if(! ec)
    {
        s_.clear();
        for(auto const& v : *this)
            do_insert(v.first, v.second);
    }

    On failure, the partial range_ was retained. Iteration assumes that every entry contains both a name and a value:

    auto n = varint_read(it);
    it += n;
    n = varint_read(it);

    The second varint_read() therefore reads past the end of the allocation. ASan reports a heap-buffer-overflow in varint_read().

    A failed parse now leaves the object empty, matching the documented behavior that parsing clears previous extensions.

  • Stale extensions remain iterable after clear()

    clear() previously emptied only the serialized representation:

    void
    clear()
    {
        s_.clear();
    }

    Consequently:

    chunk_extensions extensions;
    extensions.insert("name", "value");
    extensions.clear();

    produced an empty str(), but iteration still returned name=value from the unchanged range_.

    This also preserved any partial iteration entry left by a previous parse failure. Both internal representations are now cleared together.

  • Undefined behavior when inserting empty string_view values

    The insert overloads copied names and values unconditionally:

    std::memcpy(dest, name.data(), name.size());
    
    ...
    
    std::memcpy(dest, value.data(), value.size());

    A default-constructed empty string_view has a null data pointer and a size of zero. Passing that pointer to memcpy(), even with a zero length, is reported by UBSan as an invalid null-pointer argument.

    The same concern applies to calls such as:

    s_.append(name.data(), name.size());

    The copy operations are now skipped when the corresponding view is empty. Existing serialized and iteration behavior for empty inputs is preserved.

Testing

Before the patch, the focused cases on upstream commit 200ebae2 produced:

  • an ASan heap-buffer-overflow when iterating after an unterminated quoted extension;
  • an empty str() that still iterated the extension cleared by clear();
  • a UBSan null-pointer diagnostic when inserting a default-constructed empty string_view.

After the patch:

  • all three focused reproducer cases pass under ASan/UBSan;
  • the beast.http.chunk_encode unit suite passes:
    • 1 suite
    • 1 case
    • 614,314 checks
    • 0 failures
  • the unit suite passes in both C++11 and sanitizer builds;
  • release builds and the existing valid chunk-extension cases continue to pass.

Comment on lines +579 to +580
if(! name.empty())
s_.append(name.data(), name.size());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The if (!name.empty()) condition is unnecessary because s_.append(name.data(), name.size()); is well-defined even when name is empty. The same check appears in lines 598 and 610.

@ashtum
ashtum force-pushed the fix/chunk-extensions-state branch from 579b470 to 76da8e5 Compare November 15, 2026 06:42
@cppalliance-bot

Copy link
Copy Markdown

An automated preview of the documentation is available at https://3137.beast.prtest.cppalliance.org/libs/beast/doc/html/index.html

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

2026-11-15 06:55:58 UTC

@ashtum
ashtum merged commit 94fc939 into boostorg:develop Nov 15, 2026
23 of 72 checks passed
@codecov

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.92%. Comparing base (200ebae) to head (76da8e5).
⚠️ Report is 2 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3137      +/-   ##
===========================================
+ Coverage    93.88%   93.92%   +0.03%     
===========================================
  Files          176      176              
  Lines        13977    13982       +5     
===========================================
+ Hits         13123    13132       +9     
+ Misses         854      850       -4     
Files with missing lines Coverage Δ
include/boost/beast/http/chunk_encode.hpp 100.00% <100.00%> (ø)
include/boost/beast/http/impl/chunk_encode.hpp 98.74% <100.00%> (+0.01%) ⬆️

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 200ebae...76da8e5. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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