From 42cbabac6995ca7b74f54d8626c116dfb51b6727 Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Thu, 2 Jan 2020 15:01:55 +0100 Subject: [PATCH 1/3] check ast_simplified address literals to be well formatted --- src/passes/3-self_ast_simplified/dune | 1 + src/passes/3-self_ast_simplified/literals.ml | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/src/passes/3-self_ast_simplified/dune b/src/passes/3-self_ast_simplified/dune index 69dc6b507..aef575249 100644 --- a/src/passes/3-self_ast_simplified/dune +++ b/src/passes/3-self_ast_simplified/dune @@ -4,6 +4,7 @@ (libraries simple-utils ast_simplified + proto-alpha-utils ) (preprocess (pps ppx_let bisect_ppx --conditional) diff --git a/src/passes/3-self_ast_simplified/literals.ml b/src/passes/3-self_ast_simplified/literals.ml index 38116590b..7950aeae2 100644 --- a/src/passes/3-self_ast_simplified/literals.ml +++ b/src/passes/3-self_ast_simplified/literals.ml @@ -1,9 +1,17 @@ open Ast_simplified open Trace +open Proto_alpha_utils let peephole_expression : expression -> expression result = fun e -> let return expression = ok { e with expression } in match e.expression with + | E_literal (Literal_address s) as e -> ( + let open Memory_proto_alpha in + let%bind (_contract:Protocol.Alpha_context.Contract.t) = + Trace.trace_alpha_tzresult (simple_error ("address \""^s^"\" is not a valid address")) @@ + Protocol.Alpha_context.Contract.of_b58check s in + return e + ) | E_constant (C_BIG_MAP_LITERAL , lst) -> ( let%bind elt = trace_option (simple_error "big_map literal expects a single parameter") @@ From 8584bacf83eb35e9eb4ec0bd2d7cbc4644852be2 Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Thu, 2 Jan 2020 15:02:02 +0100 Subject: [PATCH 2/3] test & dune promote --- src/bin/expect_tests/contract_tests.ml | 6 +++++- src/test/contracts/bad_address_format.religo | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 src/test/contracts/bad_address_format.religo diff --git a/src/bin/expect_tests/contract_tests.ml b/src/bin/expect_tests/contract_tests.ml index 4d2d6b5a9..0ce94f98d 100644 --- a/src/bin/expect_tests/contract_tests.ml +++ b/src/bin/expect_tests/contract_tests.ml @@ -931,4 +931,8 @@ let%expect_test _ = let%expect_test _ = run_ligo_bad [ "run-function" ; contract "failwith.ligo" ; "failer" ; "1" ] ; - [%expect {| ligo: Execution failed: {"value":"some_string","type":"string"} |}] \ No newline at end of file + [%expect {| ligo: Execution failed: {"value":"some_string","type":"string"} |}] + +let%expect_test _ = + run_ligo_bad [ "compile-contract" ; contract "bad_address_format.religo" ; "main" ] ; + [%expect {| ligo: in file "bad_address_format.religo", line 2, characters 25-47. Badly formatted address "KT1badaddr": {"location":"in file \"bad_address_format.religo\", line 2, characters 25-47"} |}] diff --git a/src/test/contracts/bad_address_format.religo b/src/test/contracts/bad_address_format.religo new file mode 100644 index 000000000..c8668bc45 --- /dev/null +++ b/src/test/contracts/bad_address_format.religo @@ -0,0 +1,3 @@ +let main = (parameter: int, storage: address) => { + ([]:list(operation), "KT1badaddr" : address); +}; From 46027de719930b227b90332f373a119f334cdf45 Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Thu, 2 Jan 2020 15:11:02 +0100 Subject: [PATCH 3/3] Proper error with location --- src/passes/3-self_ast_simplified/literals.ml | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/passes/3-self_ast_simplified/literals.ml b/src/passes/3-self_ast_simplified/literals.ml index 7950aeae2..dfe3cac4a 100644 --- a/src/passes/3-self_ast_simplified/literals.ml +++ b/src/passes/3-self_ast_simplified/literals.ml @@ -2,15 +2,26 @@ open Ast_simplified open Trace open Proto_alpha_utils +module Errors = struct + let bad_literal_address s_addr loc () = + let title = (thunk ("Badly formatted address \""^s_addr^"\"")) in + let message () = "" in + let data = [ + ("location" , fun () -> Format.asprintf "%a" Location.pp loc) + ] in + error ~data title message () +end +open Errors + let peephole_expression : expression -> expression result = fun e -> let return expression = ok { e with expression } in match e.expression with - | E_literal (Literal_address s) as e -> ( + | E_literal (Literal_address s) as l -> ( let open Memory_proto_alpha in let%bind (_contract:Protocol.Alpha_context.Contract.t) = - Trace.trace_alpha_tzresult (simple_error ("address \""^s^"\" is not a valid address")) @@ + Trace.trace_alpha_tzresult (bad_literal_address s e.location) @@ Protocol.Alpha_context.Contract.of_b58check s in - return e + return l ) | E_constant (C_BIG_MAP_LITERAL , lst) -> ( let%bind elt =