From 56427e4f9d24fd2d3934fed1dba79167a573db3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 10 Mar 2023 11:40:02 -0500 Subject: [PATCH] fix: closes #11343, don't crash if tags array is empty --- src/database/mongo/sorted/union.js | 2 +- src/database/postgres/sorted/union.js | 3 +++ src/topics/tags.js | 2 +- test/database/sorted.js | 5 +++++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/database/mongo/sorted/union.js b/src/database/mongo/sorted/union.js index ea4ad4d8e1..f518fab79f 100644 --- a/src/database/mongo/sorted/union.js +++ b/src/database/mongo/sorted/union.js @@ -26,7 +26,7 @@ module.exports = function (module) { async function getSortedSetUnion(params) { if (!Array.isArray(params.sets) || !params.sets.length) { - return; + return []; } let limit = params.stop - params.start + 1; if (limit <= 0) { diff --git a/src/database/postgres/sorted/union.js b/src/database/postgres/sorted/union.js index a8fcbd0d0d..9d671ceb0f 100644 --- a/src/database/postgres/sorted/union.js +++ b/src/database/postgres/sorted/union.js @@ -32,6 +32,9 @@ SELECT COUNT(DISTINCT z."value") c async function getSortedSetUnion(params) { const { sets } = params; + if (!sets || !sets.length) { + return []; + } const start = params.hasOwnProperty('start') ? params.start : 0; const stop = params.hasOwnProperty('stop') ? params.stop : -1; let weights = params.weights || []; diff --git a/src/topics/tags.js b/src/topics/tags.js index 655c18345f..314b5cfd84 100644 --- a/src/topics/tags.js +++ b/src/topics/tags.js @@ -287,7 +287,7 @@ module.exports = function (Topics) { } Topics.getTagData = async function (tags) { - if (!tags.length) { + if (!tags || !tags.length) { return []; } tags.forEach((tag) => { diff --git a/test/database/sorted.js b/test/database/sorted.js index 842b91b146..a5262a5394 100644 --- a/test/database/sorted.js +++ b/test/database/sorted.js @@ -996,6 +996,11 @@ describe('Sorted Set methods', () => { done(); }); }); + + it('should return empty array if sets is empty', async () => { + const result = await db.getSortedSetRevUnion({ sets: [], start: 0, stop: -1 }); + assert.deepStrictEqual(result, []); + }); }); describe('sortedSetIncrBy()', () => {