Fix overflowing subtraction (#2164)

This commit is contained in:
Dániel Buga 2024-09-16 12:47:16 +02:00 committed by GitHub
parent b5dd5aed17
commit dfb40c0416
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 3 deletions

View File

@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- Feature `wifi-logs` doesn't break the build anymore (#2117) - Feature `wifi-logs` doesn't break the build anymore (#2117)
- Fixed a panic when overflow-checks are enabled (#2164)
### Removed ### Removed

View File

@ -63,7 +63,7 @@ impl RawQueue {
} }
fn enqueue(&mut self, item: *mut c_void) -> i32 { fn enqueue(&mut self, item: *mut c_void) -> i32 {
if (self.current_write - self.current_read) % self.count < self.count { if self.count() < self.count {
unsafe { unsafe {
let p = self.storage.byte_add(self.item_size * self.current_write); let p = self.storage.byte_add(self.item_size * self.current_write);
p.copy_from(item as *mut u8, self.item_size); p.copy_from(item as *mut u8, self.item_size);
@ -77,7 +77,7 @@ impl RawQueue {
} }
fn try_dequeue(&mut self, item: *mut c_void) -> bool { fn try_dequeue(&mut self, item: *mut c_void) -> bool {
if (self.current_write - self.current_read) % self.count > 0 { if self.count() > 0 {
unsafe { unsafe {
let p = self.storage.byte_add(self.item_size * self.current_read) as *const c_void; let p = self.storage.byte_add(self.item_size * self.current_read) as *const c_void;
item.copy_from(p, self.item_size); item.copy_from(p, self.item_size);
@ -90,7 +90,11 @@ impl RawQueue {
} }
fn count(&self) -> usize { fn count(&self) -> usize {
(self.current_write - self.current_read) % self.count if self.current_write >= self.current_read {
self.current_write - self.current_read
} else {
self.count - self.current_read + self.current_write
}
} }
} }