Skip to content

Commit

Permalink
refactor: make Metadata.Identifier and Metadata.Value methods ins…
Browse files Browse the repository at this point in the history
…tead of fields.

Also fixes a nil pointer dereference when a rule doesn't have any metadata.
  • Loading branch information
plusvic committed Sep 12, 2024
1 parent 441f4ed commit 89b4a18
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 28 deletions.
17 changes: 15 additions & 2 deletions go/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,18 @@ func TestRulesIter(t *testing.T) {
c, err := NewCompiler()
assert.NoError(t, err)

c.AddSource("rule test_1 { condition: true }")
c.AddSource(`rule test_1 {
condition:
true
}`)
assert.NoError(t, err)

c.AddSource("rule test_2 { condition: true }")
c.AddSource(`rule test_2 {
meta:
foo = "foo"
condition:
true
}`)
assert.NoError(t, err)

rules := c.Build()
Expand All @@ -162,6 +170,11 @@ func TestRulesIter(t *testing.T) {
assert.Len(t, slice, 2)
assert.Equal(t, "test_1", slice[0].Identifier())
assert.Equal(t, "test_2", slice[1].Identifier())

assert.Len(t, slice[0].Metadata(), 0)
assert.Len(t, slice[1].Metadata(), 1)

assert.Equal(t, "foo", slice[1].Metadata()[0].Identifier())
}

func TestWarnings(t *testing.T) {
Expand Down
33 changes: 25 additions & 8 deletions go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,28 +205,35 @@ func (r *Rule) Metadata() []Metadata {
return r.metadata
}

// if cMetadata is nil the rule doesn't have any metadata, return an
// empty list.
if r.cMetadata == nil {
r.metadata = make([]Metadata, 0)
return r.metadata
}

numMetadata := int(r.cMetadata.num_entries)
cMetadata := unsafe.Slice(r.cMetadata.entries, numMetadata)
r.metadata = make([]Metadata, numMetadata)

for i, metadata := range cMetadata {
r.metadata[i].Identifier = C.GoString(metadata.identifier)
r.metadata[i].identifier = C.GoString(metadata.identifier)
switch metadata.value_type {
case C.I64:
r.metadata[i].Value = int64(
r.metadata[i].value = int64(
C.meta_i64(unsafe.Pointer(&metadata.value)))
case C.F64:
r.metadata[i].Value = float64(
r.metadata[i].value = float64(
C.meta_f64(unsafe.Pointer(&metadata.value)))
case C.BOOLEAN:
r.metadata[i].Value = bool(
r.metadata[i].value = bool(
C.meta_bool(unsafe.Pointer(&metadata.value)))
case C.STRING:
r.metadata[i].Value = C.GoString(
r.metadata[i].value = C.GoString(
C.meta_str(unsafe.Pointer(&metadata.value)))
case C.BYTES:
bytes := C.meta_bytes(unsafe.Pointer(&metadata.value))
r.metadata[i].Value = C.GoBytes(
r.metadata[i].value = C.GoBytes(
unsafe.Pointer(bytes.data),
C.int(bytes.length),
)
Expand All @@ -238,8 +245,18 @@ func (r *Rule) Metadata() []Metadata {

// Metadata represents a metadata in a Rule.
type Metadata struct {
Identifier string
Value interface{}
identifier string
value interface{}
}

// Identifier associated to the metadata.
func (m *Metadata) Identifier() string {
return m.identifier
}

// Value associated to the metadata.
func (m *Metadata) Value() interface{} {
return m.value
}

// Patterns returns the patterns defined by this rule.
Expand Down
36 changes: 18 additions & 18 deletions go/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,29 @@ func TestScannerTimeout(t *testing.T) {
}

func TestScannerMetadata(t *testing.T) {
r, _ := Compile(`rule t {
meta:
some_int = 1
some_float = 2.3034
some_bool = true
some_string = "hello"
r, _ := Compile(`rule t {
meta:
some_int = 1
some_float = 2.3034
some_bool = true
some_string = "hello"
some_bytes = "\x00\x01\x02"
condition:
true
condition:
true
}`)
s := NewScanner(r)
scanResults, _ := s.Scan([]byte{})
matchingRules := scanResults.MatchingRules()

assert.Len(t, matchingRules, 1)
assert.Equal(t, "some_int", matchingRules[0].Metadata()[0].Identifier)
assert.Equal(t, int64(1), matchingRules[0].Metadata()[0].Value)
assert.Equal(t, "some_float", matchingRules[0].Metadata()[1].Identifier)
assert.Equal(t, float64(2.3034), matchingRules[0].Metadata()[1].Value)
assert.Equal(t, "some_bool", matchingRules[0].Metadata()[2].Identifier)
assert.Equal(t, true, matchingRules[0].Metadata()[2].Value)
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, "some_int", matchingRules[0].Metadata()[0].Identifier())
assert.Equal(t, int64(1), matchingRules[0].Metadata()[0].Value())
assert.Equal(t, "some_float", matchingRules[0].Metadata()[1].Identifier())
assert.Equal(t, float64(2.3034), matchingRules[0].Metadata()[1].Value())
assert.Equal(t, "some_bool", matchingRules[0].Metadata()[2].Identifier())
assert.Equal(t, true, matchingRules[0].Metadata()[2].Value())
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())
}

0 comments on commit 89b4a18

Please sign in to comment.