Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit 5bebef9

Browse files
committed
ringbuf: check maximum length
1 parent 2a8e855 commit 5bebef9

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

include/libpmemobj++/detail/ringbuf.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,17 @@ struct ringbuf_t {
109109
/* Set by ringbuf_consume, reset by ringbuf_release. */
110110
bool consume_in_progress;
111111

112+
/**
113+
* Creates new ringbuf_t instance.
114+
*
115+
* Length must be < RBUF_OFF_MASK
116+
*/
112117
ringbuf_t(size_t max_workers, size_t length)
113118
: workers(new ringbuf_worker_t[max_workers])
114119
{
120+
if (length >= RBUF_OFF_MASK)
121+
throw std::out_of_range("ringbuf length too big");
122+
115123
written.store(0);
116124
next.store(0);
117125
end.store(0);

tests/mpsc_queue/ringbuf.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,22 @@ test_random(void)
314314
delete r;
315315
}
316316

317+
static void
318+
test_size()
319+
{
320+
try {
321+
auto size = (1ULL << 32) + 1;
322+
auto r = new ringbuf_t(1, size);
323+
(void)r;
324+
325+
ASSERT_UNREACHABLE;
326+
} catch (std::out_of_range &) {
327+
328+
} catch (...) {
329+
ASSERT_UNREACHABLE;
330+
}
331+
}
332+
317333
int
318334
main(void)
319335
{
@@ -326,5 +342,6 @@ main(void)
326342
test_multi();
327343
test_overlap();
328344
test_random();
345+
test_size();
329346
return 0;
330347
}

0 commit comments

Comments
 (0)