Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libs/linglong/tests/ll-tests/src/linglong/utils/io_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
#include "linglong/utils/io/ring_buffer.h"
#include "linglong/utils/signal/signal_blocker.h"
#include "linglong/utils/terminal/terminal_guard.h"
#include "linglong/utils/unique_fd.h"

Check warning on line 15 in libs/linglong/tests/ll-tests/src/linglong/utils/io_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/unique_fd.h" not found.

#include <array>

Check warning on line 17 in libs/linglong/tests/ll-tests/src/linglong/utils/io_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <array> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <climits>

Check warning on line 18 in libs/linglong/tests/ll-tests/src/linglong/utils/io_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <cstring> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstddef>

Check warning on line 19 in libs/linglong/tests/ll-tests/src/linglong/utils/io_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <string> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstring>
#include <string>

Check warning on line 21 in libs/linglong/tests/ll-tests/src/linglong/utils/io_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <fcntl.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 22 in libs/linglong/tests/ll-tests/src/linglong/utils/io_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <pthread.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
Expand Down Expand Up @@ -259,6 +261,14 @@
EXPECT_EQ(buf->size(), 0);
}

TEST(RingBuffer, CreateRejectsOversizedCapacity)
{
// A power-of-two capacity beyond vector::max_size() must be reported via
// the empty optional instead of escaping the noexcept boundary.
auto oversized = std::size_t{ 1 } << (sizeof(std::size_t) * CHAR_BIT - 1);
EXPECT_FALSE(RingBuffer::create(oversized).has_value());
}

TEST(EventLoop, CreateAndWaitTimeout)
{
auto loop = EventLoop::create();
Expand Down
12 changes: 10 additions & 2 deletions libs/utils/src/linglong/utils/io/ring_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ class alignas(hardware_constructive_interference_size) RingBuffer
return std::nullopt;
}

std::vector<std::byte> data(cap);
return RingBuffer{ std::move(data), cap - 1 };
if (cap > std::vector<std::byte>().max_size()) {
return std::nullopt;
}

try {
std::vector<std::byte> data(cap);
return RingBuffer{ std::move(data), cap - 1 };
} catch (...) {
return std::nullopt;
}
}

RingBuffer(const RingBuffer &) = delete;
Expand Down
Loading