diff options
author | Dinh Cong Toan <toan.dinhcong@vn.bosch.com> | 2021-06-18 13:10:49 +0700 |
---|---|---|
committer | Saya Sugiura <39760799+ssugiura@users.noreply.github.com> | 2021-06-30 10:54:59 +0900 |
commit | 05e4ad2339a24bcf38663eca37a5292a4038d423 (patch) | |
tree | 64fda022db9a54d900ad5bba658a0640c8da0d74 /src | |
parent | 8770cbb37583b2c4654e582874d604ee50d8f68d (diff) | |
download | DLT-daemon-05e4ad2339a24bcf38663eca37a5292a4038d423.tar.gz |
dlt_common: correct read/write position
In dlt_buffer_read_block() and dlt_buffer_write_block(), read/write position
should be updated in case these positions are equal to the size of ringbuffer.
Adding boundary check in gtest_dlt_common.cpp
Signed-off-by: Dinh Cong Toan <toan.dinhcong@vn.bosch.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/dlt_common.c | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/shared/dlt_common.c b/src/shared/dlt_common.c index 8757296..c597468 100644 --- a/src/shared/dlt_common.c +++ b/src/shared/dlt_common.c @@ -2431,7 +2431,10 @@ void dlt_buffer_write_block(DltBuffer *buf, int *write, const unsigned char *dat *write += (int) size; } else { - if(buf->size > (unsigned int) (*write)) { + /* when (*write) = buf->size, write only the second block + * and update write position correspondingly. + */ + if((unsigned int) (*write) <= buf->size) { /* write two blocks */ memcpy(buf->mem + *write, data, buf->size - (unsigned int) (*write)); memcpy(buf->mem, data + buf->size - *write, size - buf->size + (unsigned int) (*write)); @@ -2458,11 +2461,14 @@ void dlt_buffer_read_block(DltBuffer *buf, int *read, unsigned char *data, unsig *read += (int)size; } else { - if (buf->size > (unsigned int)(*read)) { + /* when (*read) = buf->size, read only the second block + * and update read position correspondingly. + */ + if ((unsigned int)(*read) <= buf->size) { /* read two blocks */ memcpy(data, buf->mem + *read, buf->size - (unsigned int)(*read)); memcpy(data + buf->size - *read, buf->mem, size - buf->size + (unsigned int)(*read)); - *read += (int)(size - buf->size); + *read += (int) (size - buf->size); } } } |