From 8712f36158b199cb2ffa778304ec181306a5f10d Mon Sep 17 00:00:00 2001 From: Martin Martin Date: Fri, 11 Nov 2016 16:08:46 -0800 Subject: [PATCH] When you read from a default-constructed MPMCQueue, assert instead of SIGFPE. Summary: I accidentally forgot to specify the capacity for my MPMCQueue. When I then did a blockingRead(), I got a SIGFPE. Thanks to a custom signal handler that doesn't print stack traces, and a few more comedy of errors, I lost a day to this. With this patch, I would have gotten an assertion failure instead. Reviewed By: yfeldblum Differential Revision: D4169033 fbshipit-source-id: fab97ea0d5afc3c06885758b31a5e8c91ae75a45 --- folly/MPMCQueue.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/folly/MPMCQueue.h b/folly/MPMCQueue.h index 43e9eb21..e706a1f4 100644 --- a/folly/MPMCQueue.h +++ b/folly/MPMCQueue.h @@ -888,6 +888,7 @@ class MPMCQueueBase> : boost::noncopyable { /// Same as blockingRead() but also records the ticket nunmer void blockingReadWithTicket(uint64_t& ticket, T& elem) noexcept { + assert(capacity_ != 0); ticket = popTicket_++; dequeueWithTicketBase(ticket, slots_, capacity_, stride_, elem); } @@ -1065,6 +1066,7 @@ class MPMCQueueBase> : boost::noncopyable { /// Maps an enqueue or dequeue ticket to the turn should be used at the /// corresponding SingleElementQueue uint32_t turn(uint64_t ticket, size_t cap) noexcept { + assert(cap != 0); return ticket / cap; } @@ -1270,6 +1272,7 @@ class MPMCQueueBase> : boost::noncopyable { void dequeueWithTicketBase( uint64_t ticket, Slot* slots, size_t cap, int stride, T& elem ) noexcept { + assert(cap != 0); slots[idx(ticket, cap, stride)] .dequeue(turn(ticket, cap), popSpinCutoff_, -- 2.34.1