Skip to content

RTT buffer size computation incorrect for empty buffer #1277

@WasabiFan

Description

@WasabiFan

I see this getSize implementation has gone through a few iterations based on commit history. It seems to me there's still a bug.

bool isEmpty() const { return (head == tail); }
uint32_t getSize() const
{
const uint32_t rhead{head};
const uint32_t rtail{tail};
return ((rhead > rtail) ? 0 : size) + rhead - rtail;
}

On construction:

  • head = 0
  • tail = 0
  • getSize() returns MAX + 0 - 0 = MAX

After writing one byte:

  • head = 1
  • tail = 0
  • getSize() returns 0 + 1 - 0 = 1

My best interpretation for the intent of this method is to return the number of occupied bytes. If so, the behavior for an empty buffer is incorrect.

I think the implementation in the submitted version of #610 is correct, with one caveat. If head == tail, it is ambiguous whether the buffer is full or empty. I suspect baa88d2 was attempting to work around this limitation. I think the change moves the bug but doesn't fix it.

I am not sure what the RTT format expects for boundary conditions. One entry must be reserved to disambiguate unless there's additional state (e.g. entry count). The original implementation works so long as one entry is reserved. If that's the expected behavior, isEmpty() and write() would need to be updated to match.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions