From 5e1f8a2077bc6ca2f181001f00a9c64903a609aa Mon Sep 17 00:00:00 2001 From: Jason Paulos Date: Fri, 19 May 2023 08:03:02 -0700 Subject: [PATCH] assembler: Error if extra args are present in pragmas (#5400) --- data/transactions/logic/assembler.go | 6 ++++++ data/transactions/logic/assembler_test.go | 20 ++++++++++++++++++++ data/txntest/txn.go | 3 +-- ledger/simulation/simulation_eval_test.go | 6 +++--- netdeploy/remote/deployedNetwork.go | 2 +- test/e2e-go/restAPI/restClient_test.go | 2 +- 6 files changed, 32 insertions(+), 7 deletions(-) diff --git a/data/transactions/logic/assembler.go b/data/transactions/logic/assembler.go index 7aa1ac48f8..65f9f1dfb4 100644 --- a/data/transactions/logic/assembler.go +++ b/data/transactions/logic/assembler.go @@ -2193,6 +2193,9 @@ func pragma(ops *OpStream, tokens []string) error { if len(tokens) < 3 { return ops.error("no version value") } + if len(tokens) > 3 { + return ops.errorf("unexpected extra tokens: %s", strings.Join(tokens[3:], " ")) + } value := tokens[2] var ver uint64 if ops.pending.Len() > 0 { @@ -2222,6 +2225,9 @@ func pragma(ops *OpStream, tokens []string) error { if len(tokens) < 3 { return ops.error("no typetrack value") } + if len(tokens) > 3 { + return ops.errorf("unexpected extra tokens: %s", strings.Join(tokens[3:], " ")) + } value := tokens[2] on, err := strconv.ParseBool(value) if err != nil { diff --git a/data/transactions/logic/assembler_test.go b/data/transactions/logic/assembler_test.go index dbc665e26e..dc970ef64f 100644 --- a/data/transactions/logic/assembler_test.go +++ b/data/transactions/logic/assembler_test.go @@ -2325,6 +2325,26 @@ func TestPragmas(t *testing.T) { ops = testProg(t, " #pragma version 5 ", assemblerNoVersion) require.Equal(t, uint64(5), ops.Version) + + testProg(t, "#pragma version 5 blah", assemblerNoVersion, + Expect{1, "unexpected extra tokens: blah"}) + + testProg(t, "#pragma typetrack", assemblerNoVersion, + Expect{1, "no typetrack value"}) + + testProg(t, "#pragma typetrack blah", assemblerNoVersion, + Expect{1, `bad #pragma typetrack: "blah"`}) + + testProg(t, "#pragma typetrack false blah", assemblerNoVersion, + Expect{1, "unexpected extra tokens: blah"}) + + // Currently pragmas don't treat semicolons as newlines. It would probably + // be nice to fix this. + testProg(t, "#pragma version 5; int 1", assemblerNoVersion, + Expect{1, "unexpected extra tokens: ; int 1"}) + + testProg(t, "#pragma typetrack false; int 1", assemblerNoVersion, + Expect{1, "unexpected extra tokens: ; int 1"}) } func TestAssemblePragmaVersion(t *testing.T) { diff --git a/data/txntest/txn.go b/data/txntest/txn.go index 2ad4bf27f3..5815aacc60 100644 --- a/data/txntest/txn.go +++ b/data/txntest/txn.go @@ -182,8 +182,7 @@ func assemble(source interface{}) []byte { } ops, err := logic.AssembleString(program) if err != nil { - fmt.Printf("Bad program %v", ops.Errors) - panic(ops.Errors) + panic(fmt.Sprintf("Bad program %v", ops.Errors)) } return ops.Program case []byte: diff --git a/ledger/simulation/simulation_eval_test.go b/ledger/simulation/simulation_eval_test.go index 115f52d5d6..ee5c800f39 100644 --- a/ledger/simulation/simulation_eval_test.go +++ b/ledger/simulation/simulation_eval_test.go @@ -867,7 +867,7 @@ func TestAppCallWithExtraBudget(t *testing.T) { Sender: sender.Addr, ApplicationID: 0, ApprovalProgram: expensiveAppSource, - ClearStateProgram: `#pragma version 6; int 0`, + ClearStateProgram: "#pragma version 6\nint 0", }) // Expensive 700 repetition of int 1 and pop total cost 1404 expensiveTxn := env.TxnInfo.NewTxn(txntest.Txn{ @@ -938,7 +938,7 @@ func TestAppCallWithExtraBudgetOverBudget(t *testing.T) { Sender: sender.Addr, ApplicationID: 0, ApprovalProgram: expensiveAppSource, - ClearStateProgram: `#pragma version 6; int 0`, + ClearStateProgram: "#pragma version 6\nint 0", }) // Expensive 700 repetition of int 1 and pop total cost 1404 expensiveTxn := env.TxnInfo.NewTxn(txntest.Txn{ @@ -1015,7 +1015,7 @@ func TestAppCallWithExtraBudgetExceedsInternalLimit(t *testing.T) { Sender: sender.Addr, ApplicationID: 0, ApprovalProgram: expensiveAppSource, - ClearStateProgram: `#pragma version 6; int 0`, + ClearStateProgram: "#pragma version 6\nint 0", }) // Expensive 700 repetition of int 1 and pop total cost 1404 expensiveTxn := env.TxnInfo.NewTxn(txntest.Txn{ diff --git a/netdeploy/remote/deployedNetwork.go b/netdeploy/remote/deployedNetwork.go index a6b7be3f77..4bf3955142 100644 --- a/netdeploy/remote/deployedNetwork.go +++ b/netdeploy/remote/deployedNetwork.go @@ -757,7 +757,7 @@ func createSignedTx(src basics.Address, round basics.Round, params config.Consen return []transactions.SignedTxn{}, err } approval := ops.Program - ops, err = logic.AssembleString("#pragma version 2 int 1") + ops, err = logic.AssembleString("#pragma version 2\nint 1") if err != nil { panic(err) } diff --git a/test/e2e-go/restAPI/restClient_test.go b/test/e2e-go/restAPI/restClient_test.go index 7dbc02e341..2ff0c08e52 100644 --- a/test/e2e-go/restAPI/restClient_test.go +++ b/test/e2e-go/restAPI/restClient_test.go @@ -1907,7 +1907,7 @@ int 1` ops, err := logic.AssembleString(prog) a.NoError(err) approval := ops.Program - ops, err = logic.AssembleString("#pragma version 8; int 1") + ops, err = logic.AssembleString("#pragma version 8\nint 1") a.NoError(err) clearState := ops.Program