hal-common/atomic_ring_buffer: fix crashes when len=0
This commit is contained in:
parent
ebc735008f
commit
feaeb533fb
@ -82,10 +82,11 @@ impl RingBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_full(&self) -> bool {
|
pub fn is_full(&self) -> bool {
|
||||||
|
let len = self.len.load(Ordering::Relaxed);
|
||||||
let start = self.start.load(Ordering::Relaxed);
|
let start = self.start.load(Ordering::Relaxed);
|
||||||
let end = self.end.load(Ordering::Relaxed);
|
let end = self.end.load(Ordering::Relaxed);
|
||||||
|
|
||||||
self.wrap(end + 1) == start
|
len == 0 || self.wrap(end + 1) == start
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
@ -154,7 +155,7 @@ impl<'a> Writer<'a> {
|
|||||||
let end = self.0.end.load(Ordering::Relaxed);
|
let end = self.0.end.load(Ordering::Relaxed);
|
||||||
|
|
||||||
let n = if start <= end {
|
let n = if start <= end {
|
||||||
len - end - (start == 0) as usize
|
len - end - (start == 0 && len != 0) as usize
|
||||||
} else {
|
} else {
|
||||||
start - end - 1
|
start - end - 1
|
||||||
};
|
};
|
||||||
@ -328,4 +329,23 @@ mod tests {
|
|||||||
assert_eq!(rb.is_full(), true);
|
assert_eq!(rb.is_full(), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn zero_len() {
|
||||||
|
let rb = RingBuffer::new();
|
||||||
|
unsafe {
|
||||||
|
assert_eq!(rb.is_empty(), true);
|
||||||
|
assert_eq!(rb.is_full(), true);
|
||||||
|
|
||||||
|
rb.writer().push(|buf| {
|
||||||
|
assert_eq!(0, buf.len());
|
||||||
|
0
|
||||||
|
});
|
||||||
|
|
||||||
|
rb.reader().pop(|buf| {
|
||||||
|
assert_eq!(0, buf.len());
|
||||||
|
0
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user