Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Push then push-pop sequence bug #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mo-atef-dev
Copy link

@mo-atef-dev mo-atef-dev commented May 19, 2024

The current library has a bug that causes the header and tail indicators to overreach into the next buffer. This happens when a nonempty buffer repeatedly receives a sequence of push-pop operations. Each push operation increases the tail and each pop operation increases the head, both without limit.

Here is an exact code to reproduce the problem (assuming a buffer capacity of 4 bytes):

// Assume buffer size is 4 bytes

circularBuffer_t* b = circular_buffer_init(sizeof(uint8_t));
uint8_t dataIn = 0xAA;
uint8_t dataOut = 0;

circular_buffer_push(b, &dataIn);

circular_buffer_push(b, &dataIn);
circular_buffer_pop(b, &dataOut);

circular_buffer_push(b, &dataIn);
circular_buffer_pop(b, &dataOut);

circular_buffer_push(b, &dataIn);
circular_buffer_pop(b, &dataOut);

circular_buffer_push(b, &dataIn);
circular_buffer_pop(b, &dataOut);

circular_buffer_push(b, &dataIn);
circular_buffer_pop(b, &dataOut);

After the above operations (or any number of more push-pop pair of operations), a debugger can be used to inspect that the tail and head indicators have surpassed the maximum index (in this case 3 since the size is 4) of the data array in the circular buffer structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant