cpp-httplib icon indicating copy to clipboard operation
cpp-httplib copied to clipboard

ASAN: heap-use-after-free in parse_url (line 717)

Open caomengxuan666 opened this issue 6 months ago • 1 comments

Bug Report: heap-use-after-free in httplib::detail::parse_url

ASAN Output

AddressSanitizer: heap-use-after-free on address 0x51600004a2e8
SUMMARY: AddressSanitizer: heap-use-after-free httplib.h:717:18 in httplib::detail::parse_url(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, httplib::detail::UrlComponents&)

Details

  • Commit: b1cc8095a8feca1b99e296ef78eddd312e80d2c6 (short: b1cc809)
  • File: httplib.h, line 717
  • Function: httplib::detail::parse_url
  • Detection method: AddressSanitizer during CI release-gates testing

Context

This bug was detected during CI testing of the cxxmcp project, which uses httplib as a third-party dependency for HTTP transport. The heap-use-after-free occurs in parse_url, suggesting a lifetime issue with string or buffer references during URL parsing.

caomengxuan666 avatar May 29 '26 13:05 caomengxuan666

Closing this issue — the root cause is in cxxmcp's lifetime management, not httplib.

ASAN analysis shows:

  • HttpTransport::Impl is freed on thread T0 via ~Client() → ~HttpTransport() → ~Impl()
  • An executor worker thread (T21) is still running send() → make_client() → parse_url(), accessing the already-freed Impl member origin (a std::string)

The call chain:

freed:  ~Client() → ~HttpTransport() → ~Impl() (operator delete)
used:   Executor::worker_loop() → call_tool_async → send() → make_client() → parse_url(impl->origin) ← use-after-free

The Client is destroyed while an async request spawned by call_tool_async is still in-flight. The fix needs to ensure the transport outlives all in-flight async operations (e.g., drain the executor or join worker threads before destroying the transport).

Apologies for the false alarm — httplib is working correctly here.

caomengxuan666 avatar May 29 '26 14:05 caomengxuan666