diff --git a/go/compiler.go b/go/compiler.go index fc0cafcb5..852215b8f 100644 --- a/go/compiler.go +++ b/go/compiler.go @@ -75,10 +75,10 @@ func RelaxedReSyntax(yes bool) CompileOption { // ErrorOnSlowPattern is an option for [NewCompiler] and [Compile] that // tells the compiler to treat slow patterns as errors instead of warnings. func ErrorOnSlowPattern(yes bool) CompileOption { - return func(c *Compiler) error { - c.errorOnSlowPattern = yes - return nil - } + return func(c *Compiler) error { + c.errorOnSlowPattern = yes + return nil + } } // A structure that contains the options passed to [Compiler.AddSource]. @@ -97,64 +97,63 @@ type SourceOption func(opt *sourceOptions) error // source's origin. This origin appears in error reports, for instance, if // if origin is "some_file.yar", error reports will look like: // -// error: syntax error -// --> some_file.yar:4:17 -// | -// 4 | ... more details -// +// error: syntax error +// --> some_file.yar:4:17 +// | +// 4 | ... more details // // Example: // -// c := NewCompiler() -// c.AddSource("rule some_rule { condition: true }", WithOrigin("some_file.yar")) +// c := NewCompiler() +// c.AddSource("rule some_rule { condition: true }", WithOrigin("some_file.yar")) func WithOrigin(origin string) SourceOption { - return func(opts *sourceOptions) error { - opts.origin = origin - return nil - } + return func(opts *sourceOptions) error { + opts.origin = origin + return nil + } } // CompileError represents each of the errors returned by [Compiler.Errors]. type CompileError struct { // Error code (e.g: "E001"). - Code string + Code string `json:"code"` // Error title (e.g: "unknown identifier `foo`"). - Title string + Title string `json:"title"` // Each of the labels in the error report. - Labels []Label + Labels []Label `json:"labels"` // The error's full report, as shown by the command-line tool. - Text string + Text string `json:"text"` } // Warning represents each of the warnings returned by [Compiler.Warnings]. type Warning struct { // Error code (e.g: "slow_pattern"). - Code string + Code string `json:"code"` // Error title (e.g: "slow pattern"). - Title string + Title string `json:"title"` // Each of the labels in the error report. - Labels []Label + Labels []Label `json:"labels"` // The error's full report, as shown by the command-line tool. - Text string + Text string `json:"text"` } // Label represents a label in a [CompileError]. type Label struct { // Label's level (e.g: "error", "warning", "info", "note", "help"). - Level string + Level string `json:"level"` // Origin of the code where the error occurred. - CodeOrigin string + CodeOrigin string `json:"code_origin"` // The code span highlighted by this label. - Span Span + Span Span `json:"span"` // Text associated to the label. - Text string + Text string `json:"text"` } // Span represents the starting and ending point of some piece of source // code. type Span struct { - Start int - End int + Start int `json:"start"` + End int `json:"end"` } // Error returns the error's full report. @@ -164,18 +163,18 @@ func (c CompileError) Error() string { // Compiler represent a YARA compiler. type Compiler struct { - cCompiler *C.YRX_COMPILER - relaxedReSyntax bool + cCompiler *C.YRX_COMPILER + relaxedReSyntax bool errorOnSlowPattern bool - ignoredModules map[string]bool - vars map[string]interface{} + ignoredModules map[string]bool + vars map[string]interface{} } // NewCompiler creates a new compiler. -func NewCompiler(opts... CompileOption) (*Compiler, error) { +func NewCompiler(opts ...CompileOption) (*Compiler, error) { c := &Compiler{ ignoredModules: make(map[string]bool), - vars: make(map[string]interface{}), + vars: make(map[string]interface{}), } for _, opt := range opts { @@ -235,11 +234,11 @@ func (c *Compiler) initialize() error { // // Examples: // -// c := NewCompiler() -// c.AddSource("rule foo { condition: true }") -// c.AddSource("rule bar { condition: true }") -// c.AddSource("rule baz { condition: true }", WithOrigin("baz.yar")) -func (c *Compiler) AddSource(src string, opts... SourceOption) error { +// c := NewCompiler() +// c.AddSource("rule foo { condition: true }") +// c.AddSource("rule bar { condition: true }") +// c.AddSource("rule baz { condition: true }", WithOrigin("baz.yar")) +func (c *Compiler) AddSource(src string, opts ...SourceOption) error { options := &sourceOptions{} for _, opt := range opts { opt(options) @@ -249,10 +248,10 @@ func (c *Compiler) AddSource(src string, opts... SourceOption) error { defer C.free(unsafe.Pointer(cSrc)) var cOrigin *C.char - if options.origin != "" { - cOrigin = C.CString(options.origin) - defer C.free(unsafe.Pointer(cOrigin)) - } + if options.origin != "" { + cOrigin = C.CString(options.origin) + defer C.free(unsafe.Pointer(cOrigin)) + } // The call to runtime.LockOSThread() is necessary to make sure that // yrx_compiler_add_source and yrx_last_error are called from the same OS @@ -295,16 +294,16 @@ func (c *Compiler) ignoreModule(module string) { // // Examples: // -// c := NewCompiler() -// // Add some rule named "foo" under the default namespace -// c.AddSource("rule foo { condition: true }") +// c := NewCompiler() +// // Add some rule named "foo" under the default namespace +// c.AddSource("rule foo { condition: true }") // -// // Create a new namespace named "bar" -// c.NewNamespace("bar") +// // Create a new namespace named "bar" +// c.NewNamespace("bar") // -// // It's ok to add another rule named "foo", as it is in a different -// // namespace than the previous one. -// c.AddSource("rule foo { condition: true }") +// // It's ok to add another rule named "foo", as it is in a different +// // namespace than the previous one. +// c.AddSource("rule foo { condition: true }") func (c *Compiler) NewNamespace(namespace string) { cNamespace := C.CString(namespace) defer C.free(unsafe.Pointer(cNamespace)) diff --git a/go/compiler_test.go b/go/compiler_test.go index 2ebe482eb..97ee92d99 100644 --- a/go/compiler_test.go +++ b/go/compiler_test.go @@ -39,7 +39,6 @@ func TestRelaxedReSyntax(t *testing.T) { assert.Len(t, scanResults.MatchingRules(), 1) } - func TestErrorOnSlowPattern(t *testing.T) { _, err := Compile(` rule test { strings: $a = /a.*/ condition: $a }`, @@ -107,7 +106,7 @@ func TestVariables(t *testing.T) { func TestError(t *testing.T) { _, err := Compile("rule test { condition: foo }") - expected := `error[E009]: unknown identifier `+"`foo`"+` + expected := `error[E009]: unknown identifier ` + "`foo`" + ` --> line:1:24 | 1 | rule test { condition: foo } @@ -116,7 +115,6 @@ func TestError(t *testing.T) { assert.EqualError(t, err, expected) } - func TestErrors(t *testing.T) { c, err := NewCompiler() assert.NoError(t, err) @@ -127,17 +125,17 @@ func TestErrors(t *testing.T) { c.AddSource("rule test_2 { condition: foo }", WithOrigin("test.yar")) assert.Equal(t, []CompileError{ { - Code: "E009", + Code: "E009", Title: "unknown identifier `foo`", Labels: []Label{ { - Level: "error", + Level: "error", CodeOrigin: "", - Span: Span { Start: 25, End: 28 }, - Text: "this identifier has not been declared", + Span: Span{Start: 25, End: 28}, + Text: "this identifier has not been declared", }, }, - Text: `error[E009]: unknown identifier `+"`foo`"+` + Text: `error[E009]: unknown identifier ` + "`foo`" + ` --> test.yar:1:26 | 1 | rule test_2 { condition: foo } @@ -147,7 +145,6 @@ func TestErrors(t *testing.T) { }, c.Errors()) } - func TestWarnings(t *testing.T) { c, err := NewCompiler() assert.NoError(t, err) @@ -156,40 +153,40 @@ func TestWarnings(t *testing.T) { assert.Equal(t, []Warning{ { - Code: "consecutive_jumps", + Code: "consecutive_jumps", Title: "consecutive jumps in hex pattern `$a`", Labels: []Label{ { - Level: "warning", + Level: "warning", CodeOrigin: "", - Span: Span { Start: 30, End: 40 }, - Text: "these consecutive jumps will be treated as [0-2]", + Span: Span{Start: 30, End: 40}, + Text: "these consecutive jumps will be treated as [0-2]", }, }, - Text: `warning[consecutive_jumps]: consecutive jumps in hex pattern `+"`$a`"+` + Text: `warning[consecutive_jumps]: consecutive jumps in hex pattern ` + "`$a`" + ` --> line:1:31 | 1 | rule test { strings: $a = {01 [0-1][0-1] 02 } condition: $a } | ---------- these consecutive jumps will be treated as [0-2] |`, }, - { - Code: "slow_pattern", - Title: "slow pattern", - Labels: []Label{ - { - Level: "warning", - CodeOrigin: "", - Span: Span { Start: 21, End: 43 }, - Text: "this pattern may slow down the scan", - }, - }, - Text: `warning[slow_pattern]: slow pattern + { + Code: "slow_pattern", + Title: "slow pattern", + Labels: []Label{ + { + Level: "warning", + CodeOrigin: "", + Span: Span{Start: 21, End: 43}, + Text: "this pattern may slow down the scan", + }, + }, + Text: `warning[slow_pattern]: slow pattern --> line:1:22 | 1 | rule test { strings: $a = {01 [0-1][0-1] 02 } condition: $a } | ---------------------- this pattern may slow down the scan |`, - }, + }, }, c.Warnings()) -} \ No newline at end of file +} diff --git a/go/scanner.go b/go/scanner.go index d688a053f..e3ccc0710 100644 --- a/go/scanner.go +++ b/go/scanner.go @@ -45,9 +45,8 @@ type Scanner struct { matchingRules []*Rule } - // ScanResults contains the results of a call to [Scanner.Scan] or [Rules.Scan]. -type ScanResults struct{ +type ScanResults struct { matchingRules []*Rule } @@ -223,7 +222,7 @@ func (s *Scanner) Scan(buf []byte) (*ScanResults, error) { err = errors.New(C.GoString(C.yrx_last_error())) } - scanResults := &ScanResults{ s.matchingRules } + scanResults := &ScanResults{s.matchingRules} s.matchingRules = nil return scanResults, err diff --git a/go/scanner_test.go b/go/scanner_test.go index b3ebef7e6..e79cf0a83 100644 --- a/go/scanner_test.go +++ b/go/scanner_test.go @@ -51,7 +51,7 @@ func TestScanner3(t *testing.T) { s.SetGlobal("var_bool", false) scanResults, _ = s.Scan([]byte{}) - assert.Len(t, scanResults.MatchingRules(), 0) + assert.Len(t, scanResults.MatchingRules(), 0) } func TestScanner4(t *testing.T) { @@ -109,5 +109,5 @@ func TestScannerMetadata(t *testing.T) { assert.Equal(t, "some_string", matchingRules[0].Metadata()[3].Identifier) assert.Equal(t, "hello", matchingRules[0].Metadata()[3].Value) assert.Equal(t, "some_bytes", matchingRules[0].Metadata()[4].Identifier) - assert.Equal(t, []byte{0, 1, 2}, matchingRules[0].Metadata()[4].Value) + assert.Equal(t, []byte{0, 1, 2}, matchingRules[0].Metadata()[4].Value) }