Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Int conversion checks #248

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions pkg/storage/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"encoding/json"
"fmt"
"log"
"math"
"strings"

elastic "github.com/olivere/elastic/v7"
Expand Down Expand Up @@ -197,9 +198,25 @@
}
}

// Check if filter.Limit conversion is safe
var filterLimit int
if filter.Limit > uint32(math.MaxInt32) {

Check failure on line 203 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / Checks

invalid operation: filter.Limit > uint32(math.MaxInt32) (mismatched types uint and uint32)

Check failure on line 203 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / Checks

invalid operation: filter.Limit > uint32(math.MaxInt32) (mismatched types uint and uint32)

Check failure on line 203 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / CodeQL

invalid operation: filter.Limit > uint32(math.MaxInt32) (mismatched types uint and uint32)

Check failure on line 203 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / Build

invalid operation: filter.Limit > uint32(math.MaxInt32) (mismatched types uint and uint32)
filterLimit = math.MaxInt32
} else {
filterLimit = int(filter.Limit)
}

// Check if filter.Limit conversion is safe
var filterOffset int
if filter.Offset > uint32(math.MaxInt32) {

Check failure on line 211 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / Checks

invalid operation: filter.Offset > uint32(math.MaxInt32) (mismatched types uint and uint32)

Check failure on line 211 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / Checks

invalid operation: filter.Offset > uint32(math.MaxInt32) (mismatched types uint and uint32)

Check failure on line 211 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / CodeQL

invalid operation: filter.Offset > uint32(math.MaxInt32) (mismatched types uint and uint32)

Check failure on line 211 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / Build

invalid operation: filter.Offset > uint32(math.MaxInt32) (mismatched types uint and uint32)
filterOffset = math.MaxInt32
} else {
filterOffset = int(filter.Offset)
}

esSearch = esSearch.
Sort(esFieldMapping["time"], false).
From(int(filter.Offset)).Size(int(filter.Limit))
From(filterOffset).Size(filterLimit)

searchResult, err := esSearch.Do(context.Background()) // execute
// errcheck already within an errchecek, this is for additional detail.
Expand Down Expand Up @@ -272,9 +289,17 @@
}
logg.Debug("Mapped Queryname: %s --> %s", filter.QueryName, esName)

queryAgg := elastic.NewTermsAggregation().Size(int(filter.Limit)).Field(esName)
// Check if filter.Limit conversion is safe
var filterLimit int
if filter.Limit > uint32(math.MaxInt32) {

Check failure on line 294 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / Checks

invalid operation: filter.Limit > uint32(math.MaxInt32) (mismatched types uint and uint32)

Check failure on line 294 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / Checks

invalid operation: filter.Limit > uint32(math.MaxInt32) (mismatched types uint and uint32)

Check failure on line 294 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / CodeQL

invalid operation: filter.Limit > uint32(math.MaxInt32) (mismatched types uint and uint32)

Check failure on line 294 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / Build

invalid operation: filter.Limit > uint32(math.MaxInt32) (mismatched types uint and uint32)
filterLimit = math.MaxInt32
} else {
filterLimit = int(filter.Limit)
}

queryAgg := elastic.NewTermsAggregation().Size(filterLimit).Field(esName)

esSearch := es.client().Search().Index(index).Size(int(filter.Limit)).Aggregation("attributes", queryAgg)
esSearch := es.client().Search().Index(index).Size(filterLimit).Aggregation("attributes", queryAgg)
searchResult, err := esSearch.Do(context.Background())
// errcheck already within an errcheck, this is for additional detail.
if err != nil {
Expand Down Expand Up @@ -304,24 +329,38 @@
logg.Debug("Number of Buckets: %d", len(termsAggRes.Buckets))

var unique []string

for _, bucket := range termsAggRes.Buckets {
logg.Debug("key: %s count: %d", bucket.Key, bucket.DocCount)
attribute := bucket.Key.(string) //nolint:errcheck

attribute, ok := bucket.Key.(string)
if !ok {
logg.Debug("Failed to convert bucket key to string")
continue
}

// Hierarchical Depth Handling
var att string
if filter.MaxDepth != 0 && strings.Contains(attribute, "/") {
s := strings.Split(attribute, "/")
l := len(s)
for i := 0; i < int(filter.MaxDepth) && i < l; i++ {

// Check if MaxDepth conversion is safe
var maxDepth int
if filter.MaxDepth > uint32(math.MaxInt32) {

Check failure on line 349 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / Checks

invalid operation: filter.MaxDepth > uint32(math.MaxInt32) (mismatched types uint and uint32)) (typecheck)

Check failure on line 349 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / Checks

invalid operation: filter.MaxDepth > uint32(math.MaxInt32) (mismatched types uint and uint32)) (typecheck)

Check failure on line 349 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / CodeQL

invalid operation: filter.MaxDepth > uint32(math.MaxInt32) (mismatched types uint and uint32)

Check failure on line 349 in pkg/storage/elasticsearch.go

View workflow job for this annotation

GitHub Actions / Build

invalid operation: filter.MaxDepth > uint32(math.MaxInt32) (mismatched types uint and uint32)
maxDepth = math.MaxInt32
} else {
maxDepth = int(filter.MaxDepth)
}

for i := 0; i < maxDepth && i < l; i++ {
if i != 0 {
att += "/"
}
att += s[i]
}
attribute = att
}

unique = append(unique, attribute)
}

Expand Down
Loading