From 981c7014009eee456bdc70adf4d9d5c89211cafe Mon Sep 17 00:00:00 2001 From: Jason Zhang Date: Mon, 2 Sep 2024 11:12:44 +0930 Subject: [PATCH] lib: ensure no holey array in fixed_queue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jake Yuesong Li PR-URL: https://github.com/nodejs/node/pull/54537 Fixes: https://github.com/nodejs/node/issues/54472 Reviewed-By: James M Snell Reviewed-By: Michaƫl Zasso Reviewed-By: Benjamin Gruenbaum Reviewed-By: Jake Yuesong Li --- lib/internal/fixed_queue.js | 29 +++++++++++++++-------------- test/parallel/test-fixed-queue.js | 12 ++++++++++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/internal/fixed_queue.js b/lib/internal/fixed_queue.js index 4019d6e6dc4..a6e00c716c5 100644 --- a/lib/internal/fixed_queue.js +++ b/lib/internal/fixed_queue.js @@ -2,6 +2,7 @@ const { Array, + ArrayPrototypeFill, } = primordials; // 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 | // +-----------+ +-----------+ +-----------+ -// | item | <-- bottom | item | <-- bottom | [empty] | -// | item | | item | | [empty] | -// | item | | item | | [empty] | -// | item | | item | | [empty] | +// | item | <-- bottom | item | <-- bottom | undefined | +// | item | | item | | undefined | +// | item | | item | | undefined | +// | item | | item | | undefined | // | item | | item | bottom --> | item | // | item | | item | | item | // | ... | | ... | | ... | // | item | | item | | item | // | item | | item | | item | -// | [empty] | <-- top | item | | item | -// | [empty] | | item | | item | -// | [empty] | | [empty] | <-- top top --> | [empty] | +// | undefined | <-- top | item | | item | +// | undefined | | item | | item | +// | undefined | | undefined | <-- top top --> | undefined | // +-----------+ +-----------+ +-----------+ // // Or, if there is only one circular buffer, it looks something @@ -40,12 +41,12 @@ const kMask = kSize - 1; // +-----------+ +-----------+ // | [null] | | [null] | // +-----------+ +-----------+ -// | [empty] | | item | -// | [empty] | | item | -// | item | <-- bottom top --> | [empty] | -// | item | | [empty] | -// | [empty] | <-- top bottom --> | item | -// | [empty] | | item | +// | undefined | | item | +// | undefined | | item | +// | item | <-- bottom top --> | undefined | +// | item | | undefined | +// | undefined | <-- top bottom --> | item | +// | undefined | | item | // +-----------+ +-----------+ // // Adding a value means moving `top` forward by one, removing means @@ -60,7 +61,7 @@ class FixedCircularBuffer { constructor() { this.bottom = 0; this.top = 0; - this.list = new Array(kSize); + this.list = ArrayPrototypeFill(new Array(kSize), undefined); this.next = null; } diff --git a/test/parallel/test-fixed-queue.js b/test/parallel/test-fixed-queue.js index a50be1309a5..4f9b513aba8 100644 --- a/test/parallel/test-fixed-queue.js +++ b/test/parallel/test-fixed-queue.js @@ -32,3 +32,15 @@ const FixedQueue = require('internal/fixed_queue'); assert.strictEqual(queue.shift(), 'a'); 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); + } +}