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

Skip to content

Commit e35fde3

Browse files
author
Max Schaefer
committed
JavaScript: Teach ShiftOutOfRange about BigInt.
1 parent 99c32f0 commit e35fde3

4 files changed

Lines changed: 13 additions & 6 deletions

File tree

change-notes/1.22/analysis-javascript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
| **Query** | **Expected impact** | **Change** |
2323
|--------------------------------|------------------------------|---------------------------------------------------------------------------|
24-
24+
| Shift out of range | Fewer false positive results | This rule now correctly handles BigInt shift operands. |
2525

2626
## Changes to QL libraries
2727

javascript/ql/src/Expressions/ShiftOutOfRange.qhelp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ greater than 31, the left operand is actually only shifted by that value modulo
1414

1515
<p>
1616
Use standard library functions such as <code>Math.pow</code> to perform the required
17-
shifting.
17+
shifting. Alternatively, you can use the
18+
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt">BigInt</a>
19+
type if it is available on your platform.
1820
</p>
1921

2022
</recommendation>

javascript/ql/src/Expressions/ShiftOutOfRange.ql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @name Shift out of range
3-
* @description The shift operators '<<', '>>' and '>>>' only take the five least significant bits of their
4-
* right operand into account. Thus, it is not possible to shift by more than 31 bits.
3+
* @description The integer shift operators '<<', '>>' and '>>>' only take the five least significant bits of their
4+
* right operand into account. Thus, it is not possible to shift an integer by more than 31 bits.
55
* @kind problem
66
* @problem.severity error
77
* @id js/shift-out-of-range
@@ -14,5 +14,7 @@
1414
import javascript
1515

1616
from ShiftExpr shift
17-
where shift.getRightOperand().getIntValue() > 31
17+
where
18+
shift.getRightOperand().getIntValue() > 31 and
19+
not shift.getRightOperand().stripParens() instanceof BigIntLiteral
1820
select shift, "Shift out of range."
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
var n = 1<<40;
1+
var n = 1<<40; // NOT OK
2+
var n2 = BigInt(1) << 40n; // OK
3+
4+
// semmle-extractor-options: --experimental

0 commit comments

Comments
 (0)