diff --git a/.golangci.yml b/.golangci.yml index 139768e7a..18cc6fd4b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -24,7 +24,7 @@ linters: - revive # a metalinter with more checks - bodyclose # Check HTTP response bodies are closed - goconst # Find repeated strings that could be constants - - gocyclo # Check function complexity +# - gocyclo # Check function complexity - godot # Check comment endings - gocognit # Check cognitive complexity - whitespace # Check trailing whitespace diff --git a/cmd/demo/main.go b/cmd/demo/main.go index 8afbfe650..d54e0a94a 100644 --- a/cmd/demo/main.go +++ b/cmd/demo/main.go @@ -4,53 +4,91 @@ import ( "fmt" "math" "os" + "path/filepath" + "strings" wasmvm "github.com/CosmWasm/wasmvm/v2" ) +// PrintDebug enables debug printing when true const ( - PRINT_DEBUG = true - MEMORY_LIMIT = 32 // MiB - CACHE_SIZE = 100 // MiB + PrintDebug = true + // MemoryLimit defines the memory limit in MiB + MemoryLimit = 32 + // CacheSize defines the cache size in MiB + CacheSize = 100 ) -var SUPPORTED_CAPABILITIES = []string{"staking"} +// SupportedCapabilities defines the list of supported staking capabilities +var SupportedCapabilities = []string{"staking"} -// This is just a demo to ensure we can compile a static go binary. +// exitCode tracks the code that the program will exit with +var exitCode = 0 + +// main is the entry point for the demo application that tests wasmvm functionality func main() { + defer func() { + os.Exit(exitCode) + }() + + if len(os.Args) < 2 { + fmt.Println("Usage: demo ") + exitCode = 1 + return + } + file := os.Args[1] if file == "version" { libwasmvmVersion, err := wasmvm.LibwasmvmVersion() if err != nil { - panic(err) + fmt.Printf("Error getting libwasmvm version: %v\n", err) + exitCode = 1 + return } fmt.Printf("libwasmvm: %s\n", libwasmvmVersion) return } fmt.Printf("Running %s...\n", file) - bz, err := os.ReadFile(file) + + // Validate file path + cleanPath := filepath.Clean(file) + if filepath.IsAbs(cleanPath) || strings.Contains(cleanPath, "..") { + fmt.Println("Error: invalid file path") + exitCode = 1 + return + } + + bz, err := os.ReadFile(cleanPath) if err != nil { - panic(err) + fmt.Printf("Error reading file: %v\n", err) + exitCode = 1 + return } fmt.Println("Loaded!") - err = os.MkdirAll("tmp", 0o755) + err = os.MkdirAll("tmp", 0o750) if err != nil { - panic(err) + fmt.Printf("Error creating tmp directory: %v\n", err) + exitCode = 1 + return } - vm, err := wasmvm.NewVM("tmp", SUPPORTED_CAPABILITIES, MEMORY_LIMIT, PRINT_DEBUG, CACHE_SIZE) + vm, err := wasmvm.NewVM("tmp", SupportedCapabilities, MemoryLimit, PrintDebug, CacheSize) if err != nil { - panic(err) + fmt.Printf("Error creating VM: %v\n", err) + exitCode = 1 + return } + defer vm.Cleanup() checksum, _, err := vm.StoreCode(bz, math.MaxUint64) if err != nil { - panic(err) + fmt.Printf("Error storing code: %v\n", err) + exitCode = 1 + return } fmt.Printf("Stored code with checksum: %X\n", checksum) - vm.Cleanup() fmt.Println("finished") }