Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ce79bc

Browse files
committed
guard against infinite loops
... in scattergl positions cleanup.
1 parent 4aca315 commit 0ce79bc

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

src/traces/scattergl/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,11 @@ function plot(gd, subplot, cdata) {
399399
var srcPos = lineOptions.positions;
400400

401401
var firstptdef = 0;
402-
while(isNaN(srcPos[firstptdef]) || isNaN(srcPos[firstptdef + 1])) {
402+
while(firstptdef < srcPos.length && (isNaN(srcPos[firstptdef]) || isNaN(srcPos[firstptdef + 1]))) {
403403
firstptdef += 2;
404404
}
405405
var lastptdef = srcPos.length - 2;
406-
while(isNaN(srcPos[lastptdef]) || isNaN(srcPos[lastptdef + 1])) {
406+
while(lastptdef > -1 && (isNaN(srcPos[lastptdef]) || isNaN(srcPos[lastptdef + 1]))) {
407407
lastptdef -= 2;
408408
}
409409
pos = pos.concat(srcPos.slice(firstptdef, lastptdef + 2));
@@ -443,12 +443,12 @@ function plot(gd, subplot, cdata) {
443443
var firstptdef, lastptdef;
444444

445445
if(trace.fill === 'tozeroy') {
446-
while(isNaN(srcPos[firstpdef + 1])) {
447446
firstptdef = 0;
447+
while(firstptdef < srcPos.length && isNaN(srcPos[firstptdef + 1])) {
448448
firstptdef += 2;
449449
}
450-
while(isNaN(srcPos[lastpdef + 1])) {
451450
lastptdef = srcPos.length - 2;
451+
while(lastptdef > -1 && isNaN(srcPos[lastptdef + 1])) {
452452
lastptdef -= 2;
453453
}
454454
if(srcPos[firstptdef + 1] !== 0) {
@@ -460,12 +460,12 @@ function plot(gd, subplot, cdata) {
460460
}
461461
}
462462
else if(trace.fill === 'tozerox') {
463-
while(isNaN(srcPos[firstptdef])) {
464463
firstptdef = 0;
464+
while(firstptdef < srcPos.length && isNaN(srcPos[firstptdef])) {
465465
firstptdef += 2;
466466
}
467-
while(isNaN(srcPos[lastptdef])) {
468467
lastptdef = srcPos.length - 2;
468+
while(lastptdef > -1 && isNaN(srcPos[lastptdef])) {
469469
lastptdef -= 2;
470470
}
471471
if(srcPos[firstptdef] !== 0) {

test/jasmine/tests/gl2d_plot_interact_test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,79 @@ describe('Test gl2d plots', function() {
11861186
.catch(failTest)
11871187
.then(done);
11881188
});
1189+
1190+
it('@gl should not cause infinite loops when coordinate arrays start/end with NaN', function(done) {
1191+
function _assertPositions(msg, cont, exp) {
1192+
var pos = gd._fullLayout._plots.xy._scene[cont]
1193+
.map(function(opt) { return opt.positions; });
1194+
expect(pos).toBeCloseTo2DArray(exp, 2, msg);
1195+
}
1196+
1197+
Plotly.plot(gd, [{
1198+
type: 'scattergl',
1199+
mode: 'lines',
1200+
x: [1, 2, 3],
1201+
y: [null, null, null]
1202+
}, {
1203+
type: 'scattergl',
1204+
mode: 'lines',
1205+
x: [1, 2, 3],
1206+
y: [1, 2, null]
1207+
}, {
1208+
type: 'scattergl',
1209+
mode: 'lines',
1210+
x: [null, 2, 3],
1211+
y: [1, 2, 3]
1212+
}, {
1213+
type: 'scattergl',
1214+
mode: 'lines',
1215+
x: [null, null, null],
1216+
y: [1, 2, 3]
1217+
}, {
1218+
}])
1219+
.then(function() {
1220+
_assertPositions('base', 'lineOptions', [
1221+
[],
1222+
[1, 1, 2, 2],
1223+
[2, 2, 3, 3],
1224+
[]
1225+
]);
1226+
1227+
return Plotly.restyle(gd, 'fill', 'tozerox');
1228+
})
1229+
.then(function() {
1230+
_assertPositions('tozerox', 'lineOptions', [
1231+
[],
1232+
[1, 1, 2, 2],
1233+
[2, 2, 3, 3],
1234+
[]
1235+
]);
1236+
_assertPositions('tozerox', 'fillOptions', [
1237+
[0, undefined, 0, undefined],
1238+
[0, 1, 1, 1, 2, 2, 0, 2],
1239+
[0, 2, 2, 2, 3, 3, 0, 3],
1240+
[0, undefined, 0, undefined]
1241+
]);
1242+
1243+
return Plotly.restyle(gd, 'fill', 'tozeroy');
1244+
})
1245+
.then(function() {
1246+
_assertPositions('tozeroy', 'lineOptions', [
1247+
[],
1248+
[1, 1, 2, 2],
1249+
[2, 2, 3, 3],
1250+
[]
1251+
]);
1252+
_assertPositions('tozeroy', 'fillOptions', [
1253+
[undefined, 0, undefined, 0],
1254+
[1, 0, 1, 1, 2, 2, 2, 0],
1255+
[2, 0, 2, 2, 3, 3, 3, 0],
1256+
[undefined, 0, undefined, 0]
1257+
]);
1258+
})
1259+
.catch(failTest)
1260+
.then(done);
1261+
});
11891262
});
11901263

11911264
describe('Test scattergl autorange:', function() {

0 commit comments

Comments
 (0)