Skip to content

Commit

Permalink
Fixes denoland#2033, shared queue push bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Apr 20, 2019
1 parent c8db224 commit 28721c4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
16 changes: 16 additions & 0 deletions core/shared_queue.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
/*
SharedQueue Binary Layout
+-------------------------------+-------------------------------+
| NUM_RECORDS (32) |
+---------------------------------------------------------------+
| NUM_SHIFTED_OFF (32) |
+---------------------------------------------------------------+
| HEAD (32) |
+---------------------------------------------------------------+
| OFFSETS (32) |
+---------------------------------------------------------------+
| RECORD_ENDS (*MAX_RECORDS) ...
+---------------------------------------------------------------+
| RECORDS (*MAX_RECORDS) ...
+---------------------------------------------------------------+
*/

(window => {
const GLOBAL_NAMESPACE = "Deno";
Expand Down
33 changes: 31 additions & 2 deletions core/shared_queue.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
/*
SharedQueue Binary Layout
+-------------------------------+-------------------------------+
| NUM_RECORDS (32) |
+---------------------------------------------------------------+
| NUM_SHIFTED_OFF (32) |
+---------------------------------------------------------------+
| HEAD (32) |
+---------------------------------------------------------------+
| OFFSETS (32) |
+---------------------------------------------------------------+
| RECORD_ENDS (*MAX_RECORDS) ...
+---------------------------------------------------------------+
| RECORDS (*MAX_RECORDS) ...
+---------------------------------------------------------------+
*/

use crate::libdeno::deno_buf;

const MAX_RECORDS: usize = 100;
Expand Down Expand Up @@ -127,11 +144,11 @@ impl SharedQueue {
pub fn push(&mut self, record: &[u8]) -> bool {
let off = self.head();
let end = off + record.len();
let index = self.num_records();
if end > self.bytes.len() {
if end > self.bytes.len() || self.num_records() >= MAX_RECORDS {
debug!("WARNING the sharedQueue overflowed");
return false;
}
let index = self.num_records();
self.set_end(index, end);
assert_eq!(end - off, record.len());
self.bytes[off..end].copy_from_slice(record);
Expand Down Expand Up @@ -213,4 +230,16 @@ mod tests {
assert_eq!(q.shift().unwrap().len(), 1);
assert_eq!(q.size(), 0);
}

#[test]
fn full_records() {
let mut q = SharedQueue::new(RECOMMENDED_SIZE);
for _ in 0..MAX_RECORDS {
assert!(q.push(&alloc_buf(1)))
}
assert_eq!(q.push(&alloc_buf(1)), false);
// Even if we shift one off, we still cannot push a new record.
assert_eq!(q.shift().unwrap().len(), 1);
assert_eq!(q.push(&alloc_buf(1)), false);
}
}

0 comments on commit 28721c4

Please sign in to comment.