From 74b9312477538922f7ce3194b37ed3e0c5d7ba16 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Wed, 27 Feb 2013 16:48:56 -0500 Subject: [PATCH] Fix: don't flush-final for offset 0 if reader is on sub-buffer Triggers rare sub-buffer corruption when a buffer is completely full and a final flush is then performed. Signed-off-by: Mathieu Desnoyers --- lib/ringbuffer/ring_buffer_frontend.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/ringbuffer/ring_buffer_frontend.c b/lib/ringbuffer/ring_buffer_frontend.c index dc0357f5..2626afa3 100644 --- a/lib/ringbuffer/ring_buffer_frontend.c +++ b/lib/ringbuffer/ring_buffer_frontend.c @@ -1424,6 +1424,19 @@ int lib_ring_buffer_try_switch_slow(enum switch_mode mode, */ if (mode == SWITCH_FLUSH || off > 0) { if (unlikely(off == 0)) { + /* + * A final flush that encounters an empty + * sub-buffer cannot switch buffer if a + * reader is located within this sub-buffer. + * Anyway, the purpose of final flushing of a + * sub-buffer at offset 0 is to handle the case + * of entirely empty stream. + */ + if (unlikely(subbuf_trunc(offsets->begin, chan) + - subbuf_trunc((unsigned long) + atomic_long_read(&buf->consumed), chan) + >= chan->backend.buf_size)) + return -1; /* * The client does not save any header information. * Don't switch empty subbuffer on finalize, because it -- 2.34.1