Skip to content

Commit 332413b

Browse files
committed
Fixed if using splice in loop should always decrement index or might out of array range
1 parent 3683a6d commit 332413b

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/realtime.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,10 @@ function finishConnection(socket, note, user) {
264264

265265
//clear finished socket in queue
266266
for (var i = 0; i < connectionSocketQueue.length; i++) {
267-
if (!connectionSocketQueue[i] || connectionSocketQueue[i].id == socket.id)
267+
if (!connectionSocketQueue[i] || connectionSocketQueue[i].id == socket.id) {
268268
connectionSocketQueue.splice(i, 1);
269+
i--;
270+
}
269271
}
270272
//seek for next socket
271273
isConnectionBusy = false;
@@ -302,8 +304,10 @@ function startConnection(socket) {
302304
socket.disconnect(true);
303305
//clear err socket in queue
304306
for (var i = 0; i < connectionSocketQueue.length; i++) {
305-
if (!connectionSocketQueue[i] || connectionSocketQueue[i].id == socket.id)
307+
if (!connectionSocketQueue[i] || connectionSocketQueue[i].id == socket.id) {
306308
connectionSocketQueue.splice(i, 1);
309+
i--;
310+
}
307311
}
308312
isConnectionBusy = false;
309313
return logger.error(err);
@@ -390,8 +394,10 @@ function disconnect(socket) {
390394

391395
//clear finished socket in queue
392396
for (var i = 0; i < disconnectSocketQueue.length; i++) {
393-
if (!disconnectSocketQueue[i] || disconnectSocketQueue[i].id == socket.id)
397+
if (!disconnectSocketQueue[i] || disconnectSocketQueue[i].id == socket.id) {
394398
disconnectSocketQueue.splice(i, 1);
399+
i--;
400+
}
395401
}
396402
//seek for next socket
397403
isDisconnectBusy = false;

public/js/history.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ function addHistory(id, text, time, tags, notehistory) {
123123

124124
function removeHistory(id, notehistory) {
125125
for (var i = 0; i < notehistory.length; i++) {
126-
if (notehistory[i].id == id)
126+
if (notehistory[i].id == id) {
127127
notehistory.splice(i, 1);
128+
i--;
129+
}
128130
}
129131
return notehistory;
130132
}

0 commit comments

Comments
 (0)