Skip to content

Commit

Permalink
No version of Solidity supports octal numbers (hyperledger#1604)
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Young <sean@mess.org>
  • Loading branch information
seanyoung committed Dec 1, 2023
1 parent f401069 commit 8fdb728
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/sema/expression/literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@ pub(crate) fn number_literal(
diagnostics: &mut Diagnostics,
resolve_to: ResolveTo,
) -> Result<Expression, ()> {
if integer.starts_with('0') && integer.len() > 1 {
diagnostics.push(Diagnostic::error(
*loc,
"leading zeros not permitted, can be confused with octal".into(),
));
return Err(());
}

let integer = BigInt::from_str(integer).unwrap();

let n = if exp.is_empty() {
Expand Down Expand Up @@ -373,6 +381,16 @@ pub(super) fn rational_number_literal(
diagnostics: &mut Diagnostics,
resolve_to: ResolveTo,
) -> Result<Expression, ()> {
if integer.starts_with("-0") && integer.len() > 2
|| integer.starts_with('0') && integer.len() > 1
{
diagnostics.push(Diagnostic::error(
*loc,
"leading zeros not permitted, can be confused with octal".into(),
));
return Err(());
}

let mut integer = integer.to_owned();
let len = fraction.len() as u32;
let exp_negative = exp.starts_with('-');
Expand Down
18 changes: 18 additions & 0 deletions tests/contract_testcases/evm/octal.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
contract C {
int public test1 = 00;
int public test2 = 0;
int public test3 = 01e1;
int public test4 = 09.1 * 10;
int public test5 = -00;
int public test6 = -0;
int public test7 = -01e1;
int public test8 = -09.1 * 10;
}

// ---- Expect: diagnostics ----
// error: 2:21-23: leading zeros not permitted, can be confused with octal
// error: 4:21-25: leading zeros not permitted, can be confused with octal
// error: 5:21-25: leading zeros not permitted, can be confused with octal
// error: 6:21-24: leading zeros not permitted, can be confused with octal
// error: 8:21-26: leading zeros not permitted, can be confused with octal
// error: 9:22-26: leading zeros not permitted, can be confused with octal
4 changes: 2 additions & 2 deletions tests/contract_testcases/solana/rational_comparison.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ contract c {
return (a/b) >= 0.05;
}
function foo2(uint64 a, uint64 b) public returns (bool) {
return 002.2 > a;
return 2.2 > a;
}
function foo3(uint64 a, uint64 b) public returns (bool) {
return 1 == 0.05;
Expand All @@ -22,7 +22,7 @@ contract c {

// ---- Expect: diagnostics ----
// error: 4:10-23: cannot use rational numbers with '>=' operator
// error: 7:10-19: cannot use rational numbers with '>' operator
// error: 7:10-17: cannot use rational numbers with '>' operator
// error: 10:10-19: cannot use rational numbers with '==' operator
// error: 13:10-11: expression not allowed in constant rational number expression
// error: 16:10-26: cannot use rational numbers with '<=' operator
Expand Down
2 changes: 1 addition & 1 deletion tests/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ fn ethereum_solidity_tests() {
})
.sum();

assert_eq!(errors, 936);
assert_eq!(errors, 933);
}

fn set_file_contents(source: &str, path: &Path) -> (FileResolver, Vec<String>) {
Expand Down

0 comments on commit 8fdb728

Please sign in to comment.