fix: use wrapping arithmetic for position#2
fix: use wrapping arithmetic for position#2HaveFunTrading merged 3 commits intoHaveFunTrading:mainfrom Librazy:fix/wrapping-arithmetic
Conversation
|
Thanks for raising the PR @Librazy I have added few comments. The |
|
For the wrapping arithmetic, it's exactly the same as normal arithmetic if it never wraps around, and the |
fair point; I might need to revise it at some point and split |
Position Arithmetic Overflow
The ring buffer position calculations were using regular arithmetic operations which could overflow. Just like Linux's jiffies, the writer position ran out quickly on i686, and possible to exhaust x86_64's
usizein reasonable time. This has been fixed by using wrapping arithmetic operations (wrapping_addandwrapping_sub) to handle position calculations safely.It's still technically possible that the position wraps all around and make the overrun test to return false negative, but just like the jiffies, we can safely ignore this problem in any production environment.
Improved Writer Overrun Detection
Fixed overrun detection by checking after message construction but before reader position updates. This prevents issues where the frame header might be overwritten by the writer.
Additional Improvements
resetmethod to Reader for recovering from overrun conditionsinto_writer_atandwith_initial_position