mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
lib: ensure no holey array in fixed_queue
Co-authored-by: Jake Yuesong Li <jake.yuesong@gmail.com> PR-URL: https://github.com/nodejs/node/pull/54537 Fixes: https://github.com/nodejs/node/issues/54472 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
This commit is contained in:
parent
3d954dcf81
commit
981c701400
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
Array,
|
Array,
|
||||||
|
ArrayPrototypeFill,
|
||||||
} = primordials;
|
} = primordials;
|
||||||
|
|
||||||
// Currently optimal queue size, tested on V8 6.0 - 6.6. Must be power of two.
|
// Currently optimal queue size, tested on V8 6.0 - 6.6. Must be power of two.
|
||||||
@ -17,18 +18,18 @@ const kMask = kSize - 1;
|
|||||||
// +-----------+ <-----\ +-----------+ <------\ +-----------+
|
// +-----------+ <-----\ +-----------+ <------\ +-----------+
|
||||||
// | [null] | \----- | next | \------- | next |
|
// | [null] | \----- | next | \------- | next |
|
||||||
// +-----------+ +-----------+ +-----------+
|
// +-----------+ +-----------+ +-----------+
|
||||||
// | item | <-- bottom | item | <-- bottom | [empty] |
|
// | item | <-- bottom | item | <-- bottom | undefined |
|
||||||
// | item | | item | | [empty] |
|
// | item | | item | | undefined |
|
||||||
// | item | | item | | [empty] |
|
// | item | | item | | undefined |
|
||||||
// | item | | item | | [empty] |
|
// | item | | item | | undefined |
|
||||||
// | item | | item | bottom --> | item |
|
// | item | | item | bottom --> | item |
|
||||||
// | item | | item | | item |
|
// | item | | item | | item |
|
||||||
// | ... | | ... | | ... |
|
// | ... | | ... | | ... |
|
||||||
// | item | | item | | item |
|
// | item | | item | | item |
|
||||||
// | item | | item | | item |
|
// | item | | item | | item |
|
||||||
// | [empty] | <-- top | item | | item |
|
// | undefined | <-- top | item | | item |
|
||||||
// | [empty] | | item | | item |
|
// | undefined | | item | | item |
|
||||||
// | [empty] | | [empty] | <-- top top --> | [empty] |
|
// | undefined | | undefined | <-- top top --> | undefined |
|
||||||
// +-----------+ +-----------+ +-----------+
|
// +-----------+ +-----------+ +-----------+
|
||||||
//
|
//
|
||||||
// Or, if there is only one circular buffer, it looks something
|
// Or, if there is only one circular buffer, it looks something
|
||||||
@ -40,12 +41,12 @@ const kMask = kSize - 1;
|
|||||||
// +-----------+ +-----------+
|
// +-----------+ +-----------+
|
||||||
// | [null] | | [null] |
|
// | [null] | | [null] |
|
||||||
// +-----------+ +-----------+
|
// +-----------+ +-----------+
|
||||||
// | [empty] | | item |
|
// | undefined | | item |
|
||||||
// | [empty] | | item |
|
// | undefined | | item |
|
||||||
// | item | <-- bottom top --> | [empty] |
|
// | item | <-- bottom top --> | undefined |
|
||||||
// | item | | [empty] |
|
// | item | | undefined |
|
||||||
// | [empty] | <-- top bottom --> | item |
|
// | undefined | <-- top bottom --> | item |
|
||||||
// | [empty] | | item |
|
// | undefined | | item |
|
||||||
// +-----------+ +-----------+
|
// +-----------+ +-----------+
|
||||||
//
|
//
|
||||||
// Adding a value means moving `top` forward by one, removing means
|
// Adding a value means moving `top` forward by one, removing means
|
||||||
@ -60,7 +61,7 @@ class FixedCircularBuffer {
|
|||||||
constructor() {
|
constructor() {
|
||||||
this.bottom = 0;
|
this.bottom = 0;
|
||||||
this.top = 0;
|
this.top = 0;
|
||||||
this.list = new Array(kSize);
|
this.list = ArrayPrototypeFill(new Array(kSize), undefined);
|
||||||
this.next = null;
|
this.next = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,3 +32,15 @@ const FixedQueue = require('internal/fixed_queue');
|
|||||||
assert.strictEqual(queue.shift(), 'a');
|
assert.strictEqual(queue.shift(), 'a');
|
||||||
assert(queue.isEmpty());
|
assert(queue.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// FixedQueue must not be holey array
|
||||||
|
// Refs: https://github.com/nodejs/node/issues/54472
|
||||||
|
const queue = new FixedQueue();
|
||||||
|
for (let i = 0; i < queue.head.list.length; i++) {
|
||||||
|
assert(i in queue.head.list);
|
||||||
|
}
|
||||||
|
for (let i = 0; i < queue.tail.list.length; i++) {
|
||||||
|
assert(i in queue.tail.list);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user