Util: Fix circle buffer dump offset forcing an early wrap

This commit is contained in:
Vicki Pfau 2024-04-16 02:54:30 -07:00
parent 9fa825e336
commit 76169c669a
2 changed files with 44 additions and 1 deletions

View File

@ -279,8 +279,18 @@ size_t mCircleBufferDump(const struct mCircleBuffer* buffer, void* output, size_
if (length > buffer->size - offset) {
length = buffer->size - offset;
}
data += offset;
size_t remaining = buffer->capacity - ((uintptr_t) data - (uintptr_t) buffer->data);
if (offset) {
if (remaining >= offset) {
data += offset;
remaining -= offset;
} else {
offset -= remaining;
data = buffer->data;
data += offset;
}
}
if (length <= remaining) {
memcpy(output, data, length);
} else {

View File

@ -938,6 +938,38 @@ M_TEST_DEFINE(dumpOffset) {
mCircleBufferDeinit(&buffer);
}
M_TEST_DEFINE(dumpOffsetWrap) {
struct mCircleBuffer buffer;
const char* data = " Lorem ipsum dolor sit amet, consectetur adipiscing elit placerat.";
char databuf[64];
mCircleBufferInit(&buffer, 64);
assert_int_equal(mCircleBufferWrite(&buffer, data, 64), 64);
assert_int_equal(mCircleBufferSize(&buffer), 64);
assert_int_equal(mCircleBufferRead(&buffer, databuf, 48), 48);
assert_memory_equal(data, databuf, 48);
assert_int_equal(mCircleBufferSize(&buffer), 16);
assert_int_equal(mCircleBufferWrite(&buffer, data, 16), 16);
assert_int_equal(mCircleBufferSize(&buffer), 32);
assert_int_equal(mCircleBufferDump(&buffer, databuf, 64, 0), 32);
assert_memory_equal(&data[48], databuf, 16);
assert_memory_equal(data, &databuf[16], 16);
assert_int_equal(mCircleBufferDump(&buffer, databuf, 64, 8), 24);
assert_memory_equal(&data[56], databuf, 8);
assert_memory_equal(data, &databuf[8], 16);
assert_int_equal(mCircleBufferDump(&buffer, databuf, 64, 16), 16);
assert_memory_equal(data, databuf, 16);
assert_int_equal(mCircleBufferDump(&buffer, databuf, 64, 24), 8);
assert_memory_equal(&data[8], databuf, 8);
mCircleBufferDeinit(&buffer);
}
M_TEST_SUITE_DEFINE(mCircleBuffer,
cmocka_unit_test(basicCircle),
cmocka_unit_test(basicAlignment16),
@ -959,4 +991,5 @@ M_TEST_SUITE_DEFINE(mCircleBuffer,
cmocka_unit_test(writeTruncate),
cmocka_unit_test(dumpBasic),
cmocka_unit_test(dumpOffset),
cmocka_unit_test(dumpOffsetWrap),
)