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

Skip to content

Commit 9d8d953

Browse files
committed
JS: perform widening when adding operands of very different magnitude
1 parent 6c53ad8 commit 9d8d953

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

javascript/ql/src/semmle/javascript/RangeAnalysis.qll

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,26 @@ module RangeAnalysis {
404404
seedEdge(cfg, b, -bsign, a, -asign, c)
405405
}
406406

407+
/**
408+
* Adds a negative and positive integer, but only if they are within in the same
409+
* order of magnitude.
410+
*/
411+
bindingset[x, y]
412+
private int wideningAddition(int x, int y) {
413+
x < 0 and
414+
y >= 0 and
415+
(
416+
x = 0
417+
or
418+
y = 0
419+
or
420+
// If non-zero, check that the values are within a factor 16 of each other
421+
x.abs().bitShiftRight(4) < y.abs() and
422+
y.abs().bitShiftRight(4) < x.abs()
423+
) and
424+
result = x + y
425+
}
426+
407427
/**
408428
* Applies a restricted transitive rule to the edge set.
409429
*
@@ -435,9 +455,7 @@ module RangeAnalysis {
435455
exists (DataFlow::Node mid, int midx, ControlFlowNode cfg1, int c1, ControlFlowNode cfg2, int c2 |
436456
extendedEdge(cfg1, a, asign, mid, midx, c1) and
437457
extendedEdge(cfg2, mid, midx, b, bsign, c2) and
438-
c1 < 0 and
439-
c2 >= 0 and
440-
c = c1 + c2 and
458+
c = wideningAddition(c1, c2) and
441459
// One of the two CFG nodes must dominate the other, and `cfg` must be bound to the dominated one.
442460
(
443461
// They are in the same basic block

0 commit comments

Comments
 (0)