ARMmbed / DAPLink

https://daplink.io
Apache License 2.0
2.32k stars 979 forks source link

Circular buffer write race condition #841

Open flit opened 3 years ago

flit commented 3 years ago

It looks like there is a problem in circ_buf_write() in how it doesn't wrap the whole call in interrupt masking and instead relies on circ_buf_push() protecting each byte insert, with only one check for MIN() and no for loop exit on overflow. In practise this may be mitigated by having everything serialised on the main thread.

circ_buf_write() is also very inefficient by pushing a single byte at a time.

iosabi commented 3 years ago

I think if there aren't any concurrent circ_buf_write() calls this is not a problem. Mixing a circ_buf_write() call with concurrent reads or pops looks ok to me. Having two concurrent circ_buf_write() calls is probably a bad idea anyway unless you truly are merging two streams into one.