Skip to content

Commit

Permalink
Fix a bug that led to a divide-by-zero exception
Browse files Browse the repository at this point in the history
Change-Id: Ia5ba4a2e24108d2017ea3ed2a8a2a56cdd9c110e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/98042
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
  • Loading branch information
bwilkerson committed Mar 27, 2019
1 parent f226f4f commit 3364df5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/analyzer/lib/src/dart/constant/value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2413,9 +2413,15 @@ class IntState extends NumState {
if (rightValue >= 0) {
// TODO(brianwilkerson) After the analyzer package has a minimum SDK
// constraint that includes support for the real operator, consider
// changing this to the following line:
// changing the line below to
// return new IntState(value >>> rightValue);
return new IntState(value ~/ (1 << rightValue));
int divisor = 1 << rightValue;
if (divisor == 0) {
// The `rightValue` is large enough to cause all of the non-zero bits
// in the left operand to be shifted out of the value.
return new IntState(0);
}
return new IntState(value ~/ divisor);
}
} else if (rightOperand is DynamicState || rightOperand is NumState) {
return UNKNOWN_VALUE;
Expand Down
18 changes: 18 additions & 0 deletions pkg/analyzer/test/src/dart/constant/evaluation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,15 @@ const c = 0xFFFFFFFF >>> 33;
expect(result.toIntValue(), 0);
}

test_visitBinaryExpression_gtGtGt_negative_moreThan64Bits() async {
await _resolveTestCode('''
const c = 0xFFFFFFFF >>> 65;
''');
DartObjectImpl result = _evaluateConstant('c');
expect(result.type, typeProvider.intType);
expect(result.toIntValue(), 0);
}

test_visitBinaryExpression_gtGtGt_negative_negativeBits() async {
await _resolveTestCode('''
const c = 0xFFFFFFFF >>> -2;
Expand Down Expand Up @@ -402,6 +411,15 @@ const c = 0xFF >>> 9;
expect(result.toIntValue(), 0);
}

test_visitBinaryExpression_gtGtGt_positive_moreThan64Bits() async {
await _resolveTestCode('''
const c = 0xFF >>> 65;
''');
DartObjectImpl result = _evaluateConstant('c');
expect(result.type, typeProvider.intType);
expect(result.toIntValue(), 0);
}

test_visitBinaryExpression_gtGtGt_positive_negativeBits() async {
await _resolveTestCode('''
const c = 0xFF >>> -2;
Expand Down

0 comments on commit 3364df5

Please sign in to comment.