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

add method for zero alloc byte slice to str #2141

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 8 additions & 0 deletions pkg/common/byteconv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package common

import "unsafe"

// BytesToString converts a byte slice to a string without allocating.
func BytesToString(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}
77 changes: 77 additions & 0 deletions pkg/common/byteconv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package common

import (
"math/rand"
"reflect"
"testing"
"testing/quick"
"time"

"github.com/stretchr/testify/assert"
)

func TestBytesToString_IdentityProperty(t *testing.T) {
identityProperty := func(b []byte) bool {
return BytesToString(b) == string(b)
}

genRandBytes := func(size int) []byte {
b := make([]byte, size)

r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < size; i++ {
b[i] = byte(r.Intn(256))
}

return b
}

cfg := &quick.Config{
Values: func(values []reflect.Value, rand *rand.Rand) {
// 2% chance to return a nil slice.
if rand.Intn(2) == 0 {
values[0] = reflect.Zero(reflect.TypeOf([]byte{}))
return
}
values[0] = reflect.ValueOf(genRandBytes(rand.Intn(100_000)))
},
}

assert.NoError(t, quick.Check(identityProperty, cfg))
}

func TestBytesToString_LengthProperty(t *testing.T) {
lengthProperty := func(b []byte) bool {
return len(BytesToString(b)) == len(b)
}

assert.NoError(t, quick.Check(lengthProperty, nil))
}

// TestBytesToString_SharedDataConsistency checks if mutating the input byte slice
// after conversion affects the string output from BytesToString, testing that the
// underlying data sharing between the slice and the string is handled correctly.
func TestBytesToString_SharedDataConsistency(t *testing.T) {
immutabilityProperty := func(b []byte) bool {
if len(b) == 0 {
return BytesToString(b) == string(b)
}
s := BytesToString(b)
b[0] = 0 // modify byte slice
return s == BytesToString(b)
}

assert.NoError(t, quick.Check(immutabilityProperty, nil))
}

// TestBytesToString_ConsistencyProperty checks if BytesToString returns consistent
// results for the same byte slice input, ensuring deterministic behavior.
func TestBytesToString_ConsistencyProperty(t *testing.T) {
consistencyProperty := func(b []byte) bool {
s1 := BytesToString(b)
s2 := BytesToString(b)
return s1 == s2
}

assert.NoError(t, quick.Check(consistencyProperty, nil))
}
5 changes: 3 additions & 2 deletions pkg/custom_detectors/custom_detectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"regexp"
"strings"

"golang.org/x/sync/errgroup"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/custom_detectorspb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
"golang.org/x/sync/errgroup"
)

// The maximum number of matches from one chunk. This const is used when
Expand Down Expand Up @@ -58,7 +59,7 @@ func NewWebhookCustomRegex(pb *custom_detectorspb.CustomRegex) (*CustomRegexWebh
var httpClient = common.SaneHttpClient()

func (c *CustomRegexWebhook) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)
regexMatches := make(map[string][][]string, len(c.GetRegex()))

// Find all submatches for each regex.
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/abbysale/abbysale.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify Abbysale secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)

Expand Down
1 change: 1 addition & 0 deletions pkg/detectors/abbysale/abbysale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/kylelemons/godebug/pretty"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/abstract/abstract.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify Abstract secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)

Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/abuseipdb/abuseipdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify AbuseIPDB secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)

Expand Down
1 change: 1 addition & 0 deletions pkg/detectors/abuseipdb/abuseipdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/kylelemons/godebug/pretty"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/accuweather/accuweather.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify Accuweather secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)

Expand Down
1 change: 1 addition & 0 deletions pkg/detectors/accuweather/accuweather_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/kylelemons/godebug/pretty"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/adafruitio/adafruitio.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify AdafruitIO secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)

Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/adobeio/adobeio.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify AdobeIO secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)
idMatches := idPat.FindAllStringSubmatch(dataStr, -1)
Expand Down
1 change: 1 addition & 0 deletions pkg/detectors/adobeio/adobeio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/kylelemons/godebug/pretty"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/adzuna/adzuna.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify Adzuna secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)
matches := keyPat.FindAllStringSubmatch(dataStr, -1)
idMatches := idPat.FindAllStringSubmatch(dataStr, -1)

Expand Down
1 change: 1 addition & 0 deletions pkg/detectors/adzuna/adzuna_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/kylelemons/godebug/pretty"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/aeroworkflow/aeroworkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify Aeroworkflow secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)
idmatches := idPat.FindAllStringSubmatch(dataStr, -1)
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/agora/agora.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify Agora secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)
secretMatches := secretPat.FindAllStringSubmatch(dataStr, -1)
Expand Down
1 change: 1 addition & 0 deletions pkg/detectors/agora/agora_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/kylelemons/godebug/pretty"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/aha/aha.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify Aha secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)

Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/airbrakeprojectkey/airbrakeprojectkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify AirbrakeProjectKey secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)
idMatches := idPat.FindAllStringSubmatch(dataStr, -1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/kylelemons/godebug/pretty"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/airbrakeuserkey/airbrakeuserkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify AirbrakeUserKey secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)

Expand Down
1 change: 1 addition & 0 deletions pkg/detectors/airbrakeuserkey/airbrakeuserkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/kylelemons/godebug/pretty"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/airship/airship.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify Airship secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)

Expand Down
1 change: 1 addition & 0 deletions pkg/detectors/airship/airship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/kylelemons/godebug/pretty"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/airtableapikey/airtableapikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type response struct {

// FromData will find and optionally verify AirtableApiKey secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

appMatches := appPat.FindAllStringSubmatch(dataStr, -1)
keyMatches := keyPat.FindAllStringSubmatch(dataStr, -1)
Expand Down
1 change: 1 addition & 0 deletions pkg/detectors/airtableapikey/airtableapikey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/airvisual/airvisual.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify AirVisual secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)

Expand Down
1 change: 1 addition & 0 deletions pkg/detectors/airvisual/airvisual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/kylelemons/godebug/pretty"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/aiven/aiven.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify Aiven secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)

Expand Down
1 change: 1 addition & 0 deletions pkg/detectors/aiven/aiven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/kylelemons/godebug/pretty"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/alchemy/alchemy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify Alchemy secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)

Expand Down
5 changes: 3 additions & 2 deletions pkg/detectors/alchemy/alchemy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ package alchemy
import (
"context"
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/alconost/alconost.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify Alconost secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)

Expand Down
1 change: 1 addition & 0 deletions pkg/detectors/alconost/alconost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/kylelemons/godebug/pretty"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/alegra/alegra.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s Scanner) Keywords() []string {

// FromData will find and optionally verify Alegra secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
dataStr := common.BytesToString(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)
idMatches := idPat.FindAllStringSubmatch(dataStr, -1)
Expand Down
Loading