Hi folks. I need a review of a function I wrote that is suspected to perhaps have a bug in it - off by one is the most likely culprit, but I can't find it.
It is part of a
const_buffer_stream
class I use to read data off an array. For reasons I cannot use iostream classes.
Basically, it seems to be seeking to the wrong position when reading single bytes out of it. (calling
read()
with a size of 1. I'm not sure if it happens with larger values.
There could very well be no problem with this code. I just need more eyes to rule it out because as per my previous question
(Indexing into a bitmap problem)[
^] it was determined that the bug was deeper in my code.
What I have tried:
This function reads a portion of memory out of a const byte array. It's part of a larger
const_buffer_stream
class. It holds a buffer, a cursor position, and the size.
It returns the number of bytes read (which appears to work)
m_current
the current offset into the buffer, as a pointer.
m_size
the size of the buffer
m_begin
the beginning of the buffer, as a pointer
size_t const_buffer_stream::read(uint8_t* destination,size_t size) {
if(nullptr==m_current || nullptr==destination || size==0)
return 0;
size_t offs=m_current-m_begin;
if(offs>=m_size)
return 0;
if(size+offs>m_size)
size = (m_size-offs);
memcpy(destination,m_current,size);
m_current+=size;
return size;
}