Skip to content

Commit b8c06dc

Browse files
jazellyjakecastelli
authored andcommitted
lib: ensure no holey array in fixed_queue
Co-authored-by: Jake Yuesong Li <[email protected]> PR-URL: #54537 Fixes: #54472 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Jake Yuesong Li <[email protected]>
1 parent fe3155c commit b8c06dc

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

lib/internal/fixed_queue.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
Array,
5+
ArrayPrototypeFill,
56
} = primordials;
67

78
// Currently optimal queue size, tested on V8 6.0 - 6.6. Must be power of two.
@@ -17,18 +18,18 @@ const kMask = kSize - 1;
1718
// +-----------+ <-----\ +-----------+ <------\ +-----------+
1819
// | [null] | \----- | next | \------- | next |
1920
// +-----------+ +-----------+ +-----------+
20-
// | item | <-- bottom | item | <-- bottom | [empty] |
21-
// | item | | item | | [empty] |
22-
// | item | | item | | [empty] |
23-
// | item | | item | | [empty] |
21+
// | item | <-- bottom | item | <-- bottom | undefined |
22+
// | item | | item | | undefined |
23+
// | item | | item | | undefined |
24+
// | item | | item | | undefined |
2425
// | item | | item | bottom --> | item |
2526
// | item | | item | | item |
2627
// | ... | | ... | | ... |
2728
// | item | | item | | item |
2829
// | item | | item | | item |
29-
// | [empty] | <-- top | item | | item |
30-
// | [empty] | | item | | item |
31-
// | [empty] | | [empty] | <-- top top --> | [empty] |
30+
// | undefined | <-- top | item | | item |
31+
// | undefined | | item | | item |
32+
// | undefined | | undefined | <-- top top --> | undefined |
3233
// +-----------+ +-----------+ +-----------+
3334
//
3435
// Or, if there is only one circular buffer, it looks something
@@ -40,12 +41,12 @@ const kMask = kSize - 1;
4041
// +-----------+ +-----------+
4142
// | [null] | | [null] |
4243
// +-----------+ +-----------+
43-
// | [empty] | | item |
44-
// | [empty] | | item |
45-
// | item | <-- bottom top --> | [empty] |
46-
// | item | | [empty] |
47-
// | [empty] | <-- top bottom --> | item |
48-
// | [empty] | | item |
44+
// | undefined | | item |
45+
// | undefined | | item |
46+
// | item | <-- bottom top --> | undefined |
47+
// | item | | undefined |
48+
// | undefined | <-- top bottom --> | item |
49+
// | undefined | | item |
4950
// +-----------+ +-----------+
5051
//
5152
// Adding a value means moving `top` forward by one, removing means
@@ -60,7 +61,7 @@ class FixedCircularBuffer {
6061
constructor() {
6162
this.bottom = 0;
6263
this.top = 0;
63-
this.list = new Array(kSize);
64+
this.list = ArrayPrototypeFill(new Array(kSize), undefined);
6465
this.next = null;
6566
}
6667

test/parallel/test-fixed-queue.js

+12
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,15 @@ const FixedQueue = require('internal/fixed_queue');
3232
assert.strictEqual(queue.shift(), 'a');
3333
assert(queue.isEmpty());
3434
}
35+
36+
{
37+
// FixedQueue must not be holey array
38+
// Refs: https://github.com/nodejs/node/issues/54472
39+
const queue = new FixedQueue();
40+
for (let i = 0; i < queue.head.list.length; i++) {
41+
assert(i in queue.head.list);
42+
}
43+
for (let i = 0; i < queue.tail.list.length; i++) {
44+
assert(i in queue.tail.list);
45+
}
46+
}

0 commit comments

Comments
 (0)