]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Decimation: Prevent buffer overflow (#9367)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Wed, 7 Jul 2021 21:45:31 +0000 (00:45 +0300)
committerGitHub <noreply@github.com>
Wed, 7 Jul 2021 21:45:31 +0000 (17:45 -0400)
src/plugins/plugin.decimation.js
test/specs/plugin.decimation.tests.js

index 701d673c0614e311265479b798e8c46f511173e5..1deb0efd2e42ce037d320c54fa2f7d1bcf1fc325 100644 (file)
@@ -46,7 +46,7 @@ function lttbDecimation(data, start, count, availableWidth, options) {
 
     // Adding offset
     const rangeOffs = Math.floor(i * bucketWidth) + 1 + start;
-    const rangeTo = Math.floor((i + 1) * bucketWidth) + 1 + start;
+    const rangeTo = Math.min(Math.floor((i + 1) * bucketWidth) + 1, count) + start;
     const {x: pointAx, y: pointAy} = data[a];
 
     // Note that this is changed from the original algorithm which initializes these
index f9efbc29af70d8f9cacf29b35b9ae9241c15002d..9f8320b1e72b697bb527bfce18e5bcad4fd4ff5d 100644 (file)
@@ -179,5 +179,41 @@ describe('Plugin.decimation', function() {
       expect(chart.data.datasets[0].data[3].x).toBe(originalData[5].x);
       expect(chart.data.datasets[0].data[4].x).toBe(originalData[6].x);
     });
+
+    it('should not crash with uneven points', function() {
+      const data = [];
+      for (let i = 0; i < 15552; i++) {
+        data.push({x: i, y: i});
+      }
+
+      function createChart() {
+        return window.acquireChart({
+          type: 'line',
+          data: {
+            datasets: [{
+              data
+            }]
+          },
+          options: {
+            devicePixelRatio: 1.25,
+            parsing: false,
+            scales: {
+              x: {
+                type: 'linear'
+              }
+            },
+            plugins: {
+              decimation: {
+                enabled: true,
+                algorithm: 'lttb'
+              }
+            }
+          }
+        }, {
+          canvas: {width: 511, height: 511},
+        });
+      }
+      expect(createChart).not.toThrow();
+    });
   });
 });