Browse Source

fix(RTC): protect from counter overflow

dev1
paweldomas 7 years ago
parent
commit
5c674f923e
2 changed files with 29 additions and 4 deletions
  1. 10
    4
      modules/RTC/RTC.js
  2. 19
    0
      modules/util/MathUtil.js

+ 10
- 4
modules/RTC/RTC.js View File

@@ -9,6 +9,7 @@ import JitsiLocalTrack from './JitsiLocalTrack';
9 9
 import JitsiTrackError from '../../JitsiTrackError';
10 10
 import * as JitsiTrackErrors from '../../JitsiTrackErrors';
11 11
 import Listenable from '../util/Listenable';
12
+import { safeCounterIncrement } from '../util/MathUtil';
12 13
 import * as MediaType from '../../service/RTC/MediaType';
13 14
 import browser from '../browser';
14 15
 import RTCEvents from '../../service/RTC/RTCEvents';
@@ -23,8 +24,13 @@ const logger = getLogger(__filename);
23 24
  * The counter used to generated id numbers assigned to peer connections
24 25
  * @type {number}
25 26
  */
26
-let peerConnectionIdCounter = 1;
27
+let peerConnectionIdCounter = 0;
27 28
 
29
+/**
30
+ * The counter used to generate id number for the local
31
+ * <code>MediaStreamTrack</code>s.
32
+ * @type {number}
33
+ */
28 34
 let rtcTrackIdCounter = 0;
29 35
 
30 36
 /**
@@ -42,7 +48,7 @@ function createLocalTracks(tracksInfo, options) {
42 48
         } else if (trackInfo.videoType === VideoType.CAMERA) {
43 49
             deviceId = options.cameraDeviceId;
44 50
         }
45
-        rtcTrackIdCounter += 1;
51
+        rtcTrackIdCounter = safeCounterIncrement(rtcTrackIdCounter);
46 52
         const localTrack = new JitsiLocalTrack({
47 53
             ...trackInfo,
48 54
             deviceId,
@@ -85,7 +91,7 @@ function _newCreateLocalTracks(mediaStreamMetaData = []) {
85 91
         // FIXME Move rtcTrackIdCounter to a static method in JitsiLocalTrack
86 92
         // so RTC does not need to handle ID management. This move would be
87 93
         // safer to do once the old createLocalTracks is removed.
88
-        rtcTrackIdCounter += 1;
94
+        rtcTrackIdCounter = safeCounterIncrement(rtcTrackIdCounter);
89 95
 
90 96
         return new JitsiLocalTrack({
91 97
             deviceId,
@@ -441,6 +447,7 @@ export default class RTC extends Listenable {
441 447
                 { abtestSuspendVideo: options.abtestSuspendVideo });
442 448
         }
443 449
 
450
+        peerConnectionIdCounter = safeCounterIncrement(peerConnectionIdCounter);
444 451
         const newConnection
445 452
             = new TraceablePeerConnection(
446 453
                 this,
@@ -450,7 +457,6 @@ export default class RTC extends Listenable {
450 457
                 isP2P, options);
451 458
 
452 459
         this.peerConnections.set(newConnection.id, newConnection);
453
-        peerConnectionIdCounter += 1;
454 460
 
455 461
         return newConnection;
456 462
     }

+ 19
- 0
modules/util/MathUtil.js View File

@@ -0,0 +1,19 @@
1
+
2
+
3
+/**
4
+ * The method will increase the given number by 1. If the given counter is equal
5
+ * or greater to {@link Number.MAX_SAFE_INTEGER} then it will be rolled back to
6
+ * 1.
7
+ * @param {number} number - An integer counter value to be incremented.
8
+ * @return {number} the next counter value increased by 1 (see the description
9
+ * above for exception).
10
+ */
11
+export function safeCounterIncrement(number) {
12
+    let nextValue = number;
13
+
14
+    if (number >= Number.MAX_SAFE_INTEGER) {
15
+        nextValue = 0;
16
+    }
17
+
18
+    return nextValue + 1;
19
+}

Loading…
Cancel
Save