I think this is a true positive, unless Size == 0, and then you have a problem in getNrReadable().
The result from getNrReadable() is in [0, Size-1], so getNrWritable() is positive.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
where if (writeIndex_ >= Size) can become true due to other parts of the code.
I asked GitHub copilot why Cppcheck provides this error and I received:
The cppcheck warning "Return value 'getNrWritable()>0' is always true" is indicating that the function canWrite() will always return true.
The canWrite() function returns the result of getNrWritable() > 0. The getNrWritable() function returns Size - getNrReadable().
The getNrReadable() function returns either Size or (Size + writeIndex_ - readIndex_) % Size. In both cases, the value returned by getNrReadable() is less than or equal to Size.
Therefore, Size - getNrReadable() is always greater than or equal to 0. This means that getNrWritable() > 0 will always be true unless getNrReadable() returns Size, which would make getNrWritable() return 0.
However, getNrReadable() will only return Size if writeIndex_ >= Size. But in the markWritten() function, if writeIndex_ == readIndex_, writeIndex_ is increased by Size, making it greater than Size. This means that writeIndex_ >= Size can be true, and therefore getNrReadable() can return Size, making getNrWritable() return 0, and canWrite() return false.
So, it seems like there might be a mistake in the cppcheck analysis. It's possible that cppcheck is not correctly understanding the logic of your code.
Last edit: Rob Deckers 2024-05-13
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I seem to be getting a false-positive in this code:
CppCheck states:
But a small adjustment of canWrite resolves the false-positive
Even though nothing has really changed
I think this is a true positive, unless
Size == 0
, and then you have a problem ingetNrReadable()
.The result from
getNrReadable()
is in[0, Size-1]
, sogetNrWritable()
is positive.Still I think there is a problem here.
where if (writeIndex_ >= Size) can become true due to other parts of the code.
I asked GitHub copilot why Cppcheck provides this error and I received:
Last edit: Rob Deckers 2024-05-13
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/12727
Good thing that you marked the AI drivel, so I could skip over that ;)