From cee7899d94c2906b4641d4fdfec281705bd5b6b0 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 9 May 2024 16:57:31 +0200 Subject: [PATCH 1/2] chore(linter): forbid usage of `regexp.Match(String)?` --- .github/golangci.yml | 5 ++++ gnovm/pkg/gnolang/alloc.go | 4 ++- tm2/pkg/amino/genproto/bindings.go | 48 ++++++++++++++---------------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/.github/golangci.yml b/.github/golangci.yml index 8b38691ec75..e78d09a582e 100644 --- a/.github/golangci.yml +++ b/.github/golangci.yml @@ -38,6 +38,7 @@ linters: - gofumpt # Stricter gofmt - unused # Checks Go code for unused constants, variables, functions and types - gomodguard # Enforces an allow and block list for direct Go module dependencies + - forbidigo # Forbids some custom-set identifiers, like regexp.MatchString linters-settings: gofmt: @@ -60,6 +61,10 @@ linters-settings: - opinionated - performance - style + forbidigo: + forbid: + - p: '^regexp\.(Match|MatchString)$' + msg: it will re-compile the regexp for each execution; compile the regexp with regexp.Compile and store it as a singleton issues: whole-files: true diff --git a/gnovm/pkg/gnolang/alloc.go b/gnovm/pkg/gnolang/alloc.go index 495be0d2dc2..e85928b98dd 100644 --- a/gnovm/pkg/gnolang/alloc.go +++ b/gnovm/pkg/gnolang/alloc.go @@ -1,6 +1,8 @@ package gnolang -import "reflect" +import ( + "reflect" +) // Keeps track of in-memory allocations. // In the future, allocations within realm boundaries will be diff --git a/tm2/pkg/amino/genproto/bindings.go b/tm2/pkg/amino/genproto/bindings.go index 5d3b46c59fc..c1de79caa59 100644 --- a/tm2/pkg/amino/genproto/bindings.go +++ b/tm2/pkg/amino/genproto/bindings.go @@ -1100,6 +1100,27 @@ func _fields(args ...interface{}) *ast.FieldList { } } +const ( + reDGTS = `(?:[0-9]+)` + reHExX = `(?:0[xX][0-9a-fA-F]+)` + rePSCI = `(?:[eE]+?[0-9]+)` + reNSCI = `(?:[eE]-[1-9][0-9]+)` + reASCI = `(?:[eE][-+]?[0-9]+)` +) + +var ( + reIsInt = regexp.MustCompile( + `^-?(?:` + + reDGTS + `|` + + reHExX + `)` + rePSCI + `?$`, + ) + reIsFloat = regexp.MustCompile( + `^-?(?:` + + reDGTS + `\.` + reDGTS + reASCI + `?|` + + reDGTS + reNSCI + `)$`, + ) +) + // Parses simple expressions (but not all). // Useful for parsing strings to ast nodes, like foo.bar["qwe"](), // new(bytes.Buffer), *bytes.Buffer, package.MyStruct{FieldA:1}, numeric @@ -1282,22 +1303,7 @@ func _x(expr string, args ...interface{}) ast.Expr { } } // Numeric int? We do these before dots, because dots are legal in numbers. - const ( - DGTS = `(?:[0-9]+)` - HExX = `(?:0[xX][0-9a-fA-F]+)` - PSCI = `(?:[eE]+?[0-9]+)` - NSCI = `(?:[eE]-[1-9][0-9]+)` - ASCI = `(?:[eE][-+]?[0-9]+)` - ) - isInt, err := regexp.Match( - `^-?(?:`+ - DGTS+`|`+ - HExX+`)`+PSCI+`?$`, - []byte(expr), - ) - if err != nil { - panic("should not happen") - } + isInt := reIsInt.MatchString(expr) if isInt { return &ast.BasicLit{ Kind: token.INT, @@ -1305,15 +1311,7 @@ func _x(expr string, args ...interface{}) ast.Expr { } } // Numeric float? We do these before dots, because dots are legal in floats. - isFloat, err := regexp.Match( - `^-?(?:`+ - DGTS+`\.`+DGTS+ASCI+`?|`+ - DGTS+NSCI+`)$`, - []byte(expr), - ) - if err != nil { - panic("should not happen") - } + isFloat := reIsFloat.MatchString(expr) if isFloat { return &ast.BasicLit{ Kind: token.FLOAT, From e18a8e684884501c7a3f3e5a9e4ba21374a6191a Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 9 May 2024 16:58:08 +0200 Subject: [PATCH 2/2] revert changes on alloc --- gnovm/pkg/gnolang/alloc.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/gnovm/pkg/gnolang/alloc.go b/gnovm/pkg/gnolang/alloc.go index e85928b98dd..495be0d2dc2 100644 --- a/gnovm/pkg/gnolang/alloc.go +++ b/gnovm/pkg/gnolang/alloc.go @@ -1,8 +1,6 @@ package gnolang -import ( - "reflect" -) +import "reflect" // Keeps track of in-memory allocations. // In the future, allocations within realm boundaries will be