rlogiacco / CircularBuffer

Arduino circular buffer library
GNU Lesser General Public License v3.0
312 stars 85 forks source link

bug:Memory trample #65

Closed xiaofyao closed 2 years ago

xiaofyao commented 2 years ago

When the capacity is set to 0, memory will stampede when pushing data:

CircularBuffer<MsgInfo,0> m_ring_msg_priority_low; m_ring_msg_priority_low.push(msg);

rlogiacco commented 2 years ago

And what would be reason for setting the capacity to 0? In your shoes I would be happy the program didn't crash on startup...

Do you have a suggestion on how to properly handle this condition?

xiaofyao commented 2 years ago

I think there should be a limit that denies user operations if the capacity is 0. Whatever the user does should not result in a high risk of memory stampede.

Br, xiaofyao

rlogiacco commented 2 years ago

I'll try to clarify: creating a circularbuffer of 0 capacity is meaningless, it's like creating an array with size 0... I'm not going to deny user operations if the user creates a 0 size buffer because to do that I will have to constantly check, before each operation, if the user was crazy enough to create a 0 capacity buffer, having all the users pay the price for the dumbest ones.

I would be happy to deny the creation of a 0 capacity buffer, but in a context whitout exceptions I don't know how to do that....

Regarding your last statement, C/C++ is full of ways for a user to burn out memory, I don't feel the burden to be the one solving the issue :-D