Skip to content

Commit

Permalink
Fix chained ternary conditionals instrumentation (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgewecke committed Jan 26, 2024
1 parent 9b64cba commit a6674c6
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ parse.Conditional = function(contract, expression, skipStatementRegistry) {
parse[expression.condition.type] &&
parse[expression.condition.type](contract, expression.condition, true);

if (expression.trueExpression && expression.trueExpression.type === 'Conditional'){
parse[expression.trueExpression.type](contract, expression.trueExpression, true);
}

if (expression.falseExpression && expression.falseExpression.type === 'Conditional'){
parse[expression.falseExpression.type](contract, expression.falseExpression, true);
}

register.conditional(contract, expression);
};

Expand Down
14 changes: 14 additions & 0 deletions test/sources/solidity/contracts/conditional/chained-multiline.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pragma solidity ^0.7.0;

contract Test {
function a() public {
int min = 1;
int max = 2;
int value = 3;
value < min
? min
: value > max
? max
: value;
}
}
10 changes: 10 additions & 0 deletions test/sources/solidity/contracts/conditional/chained-singleline.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pragma solidity ^0.7.0;

contract Test {
function a() public {
int min = 1;
int max = 2;
int value = 3;
value < min ? min : value > max ? max : value;
}
}
14 changes: 14 additions & 0 deletions test/sources/solidity/contracts/conditional/chained-true.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pragma solidity ^0.7.0;

contract Test {
function a() public {
int min = 1;
int max = 2;
int value = 3;
value < min
? value > max
? max
: value
: min;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pragma solidity ^0.7.0;

contract Test {
function a() public {
int min = 1;
int max = 2;
int value = 3;
(value < min) ? min : (value > max) ? max : value;
}
}
20 changes: 20 additions & 0 deletions test/units/conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,24 @@ describe('ternary conditionals', () => {
const info = util.instrumentAndCompile('conditional/ternary-with-unbracketed-else');
util.report(info.solcOutput.errors);
});

it('should compile after instrumenting a chain of ternary conditionals (single line)', () => {
const info = util.instrumentAndCompile('conditional/chained-singleline');
util.report(info.solcOutput.errors);
});

it('should compile after instrumenting a chain of ternary conditionals (multi-line)', () => {
const info = util.instrumentAndCompile('conditional/chained-multiline');
util.report(info.solcOutput.errors);
});

it('should compile after instrumenting a chain of ternary conditionals (parens)', () => {
const info = util.instrumentAndCompile('conditional/chained-with-parens');
util.report(info.solcOutput.errors);
});

it('should compile after instrumenting a chain of ternary conditionals (true expr is tern)', () => {
const info = util.instrumentAndCompile('conditional/chained-true');
util.report(info.solcOutput.errors);
});
});

0 comments on commit a6674c6

Please sign in to comment.