Fix possible range violation in BatchBuffer.popFront(N).

This commit is contained in:
Sönke Ludwig 2017-01-30 17:27:17 +01:00
parent 2dccd43277
commit 02e00944f6
No known key found for this signature in database
GPG key ID: D95E8DB493EE314C

View file

@ -570,12 +570,17 @@ struct BatchBuffer(T, size_t N = 0) {
@property void capacity(size_t n) { assert(n >= m_fill); m_buffer.length = n; } @property void capacity(size_t n) { assert(n >= m_fill); m_buffer.length = n; }
} }
@property bool empty() const { return m_first >= m_fill; } @property bool empty() const { assert(m_first < m_fill || m_fill == 0 && m_first == 0); return m_first >= m_fill; }
@property size_t capacity() const { return m_buffer.length; } @property size_t capacity() const { return m_buffer.length; }
@property size_t length() const { return m_fill - m_first; } @property size_t length() const { return m_fill - m_first; }
@property ref inout(T) front() inout { assert(!empty); return m_buffer[m_first]; } @property ref inout(T) front() inout { assert(!empty); return m_buffer[m_first]; }
void popFront() { assert(!empty); m_first++; } void popFront() { popFrontN(1); }
void popFrontN(size_t n) { assert(n <= length); m_first += n; } void popFrontN(size_t n) {
assert(n <= length);
m_first += n;
if (m_first == m_fill)
m_first = m_fill = 0;
}
inout(T)[] peek() inout { return m_buffer[m_first .. m_fill]; } inout(T)[] peek() inout { return m_buffer[m_first .. m_fill]; }
T[] peekDst() { assert(empty); return m_buffer; } T[] peekDst() { assert(empty); return m_buffer; }
void putN(size_t n) { assert(empty && n <= m_buffer.length); m_fill = n; } void putN(size_t n) { assert(empty && n <= m_buffer.length); m_fill = n; }
@ -583,9 +588,7 @@ struct BatchBuffer(T, size_t N = 0) {
void read(T[] dst) { void read(T[] dst) {
assert(length() >= dst.length); assert(length() >= dst.length);
dst[] = m_buffer[m_first .. m_first + dst.length]; dst[] = m_buffer[m_first .. m_first + dst.length];
m_first += dst.length; popFrontN(dst.length);
assert(m_first <= m_fill);
if (m_first == m_fill) m_first = m_fill = 0;
} }
} }