Skip to content

Fix chained ternary conditionals instrumentation #830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});