Skip to content

Commit

Permalink
assembler: Error if extra args are present in pragmas (#5400)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpaulos authored May 19, 2023
1 parent b376efd commit 5e1f8a2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
6 changes: 6 additions & 0 deletions data/transactions/logic/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions data/transactions/logic/assembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions data/txntest/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions ledger/simulation/simulation_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion netdeploy/remote/deployedNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e-go/restAPI/restClient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 5e1f8a2

Please sign in to comment.