From e89e69195786331f391e66af19b5da5a4b8d7853 Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 14 Oct 2014 15:27:47 -0700 Subject: [PATCH] Fix bug in groupId generation Previously, if calling createNewGroup with an undefined groupId, no groupId was generated. This occurred because no entry for "group" + undefined exists in localStorage, which caused this code to think undefined was a valid group id. Fixed by adding `|| groupId == undefined` to the while clause. Also decoupled the groupId collision check for clarity. --- js/helpers.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/js/helpers.js b/js/helpers.js index 49d6120944..25ab38959c 100644 --- a/js/helpers.js +++ b/js/helpers.js @@ -418,11 +418,13 @@ window.textsecure.storage = function() { } self.createNewGroup = function(numbers, groupId) { - if (groupId === undefined) { - while (textsecure.storage.getEncrypted("group" + groupId) !== undefined) - groupId = new Uint32Array(textsecure.crypto.getRandomBytes(4))[0]; - } else if (textsecure.storage.getEncrypted("group" + groupId) !== undefined) + if (groupId !== undefined && textsecure.storage.getEncrypted("group" + groupId) !== undefined) { throw new Error("Tried to recreate group"); + } + + while (groupId === undefined || textsecure.storage.getEncrypted("group" + groupId) !== undefined) { + groupId = new Uint32Array(textsecure.crypto.getRandomBytes(4))[0]; + } var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]; var haveMe = false;