Quellcode durchsuchen

No version of Solidity supports octal numbers (#1604)

Signed-off-by: Sean Young <sean@mess.org>
Sean Young vor 2 Jahren
Ursprung
Commit
8fdb7284f3

+ 18 - 0
src/sema/expression/literals.rs

@@ -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() {
@@ -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('-');

+ 18 - 0
tests/contract_testcases/evm/octal.sol

@@ -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

+ 2 - 2
tests/contract_testcases/solana/rational_comparison.sol

@@ -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;
@@ -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

+ 1 - 1
tests/evm.rs

@@ -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>) {