Skip to content

fix(utils): handle allocation failure in RingBuffer::create - #2119

Open
chennaji9 wants to merge 1 commit into
OpenAtom-Linyaps:masterfrom
chennaji9:fix/ring-buffer-alloc-failure
Open

chennaji9 wants to merge 1 commit into
OpenAtom-Linyaps:masterfrom
chennaji9:fix/ring-buffer-alloc-failure

Conversation

@chennaji9

@chennaji9 chennaji9 commented Sep 30, 2026 •

Copy link
Copy Markdown

What does this PR do?

RingBuffer::create() is declared noexcept, but it allocates its backing storage with std::vector<std::byte> data(cap), which can throw:

  • std::length_error when cap > vector::max_size(),
  • std::bad_alloc on out-of-memory.

An exception escaping a noexcept function calls std::terminate() — the process dies instead of reporting failure through the empty std::optional the API already defines for that purpose (e.g. cap == 0 today).

Fix:

  • reject capacities above std::vector<std::byte>().max_size() up front (deterministic, no exception), and
  • wrap the allocation in try/catch (...) returning std::nullopt, keeping the noexcept interface honest.

Validation

  • New unit test RingBuffer.CreateRejectsOversizedCapacity requests 1 << (sizeof(std::size_t) * CHAR_BIT - 1) (a power of two above max_size()); it asserts the empty optional is returned. Verified against roundUpPowerOf2 that this input reaches the guard without wrapping to 0.
  • All existing RingBuffer tests still pass:
    ctest -R RingBuffer → 7/7 Passed (CreateRejectsZeroCapacity, RoundsUpToPowerOfTwo, EmptyAndFullStates, WriteAndReadViaIovecs, WrapsAround, ClearResetsState, CreateRejectsOversizedCapacity).
  • Full ll-tests target builds cleanly with the change.

@deepin-ci-robot

Copy link
Copy Markdown
Collaborator

Hi @chennaji9. Thanks for your PR.

I'm waiting for a OpenAtom-Linyaps member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
libs/utils/src/linglong/utils/io/ring_buffer.h 66.66% 1 Missing and 1 partial ⚠️
Files with missing lines Coverage Δ
libs/utils/src/linglong/utils/io/ring_buffer.h 86.20% <66.66%> (-2.69%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

create() is noexcept but its vector allocation can throw
length_error/bad_alloc, terminating the process; reject capacities above
vector::max_size() up front and convert remaining allocation errors into
the empty optional the API already uses for failure; add a unit test.

Signed-off-by: 陈那几 <2078634206@qq.com>
@chennaji9
chennaji9 force-pushed the fix/ring-buffer-alloc-failure branch from b217596 to 303a877 Compare October 13, 2026 05:46
@chennaji9

Copy link
Copy Markdown
Author

@reddevillg 您好,打扰一下~CI 这边仍在等 org 成员的 /ok-to-test 放行(commitlint 问题 10-15 已修复跟进)。方便的话能否帮忙触发一下 CI?绿了之后也恳请帮忙 review、合入,辛苦了,感谢!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants