Skip to content

Commit

Permalink
fuzz all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
boyter committed May 20, 2020
1 parent 1cca04c commit ac5e57e
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion check.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

GREEN='\033[1;32m'
RED='\033[0;31m'
Expand Down
2 changes: 2 additions & 0 deletions processor/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ func drawResults(app *tview.Application, results []*fileJob, textView *tview.Tex
// unlike in others where we throw away the results afterwards
t := []int{s[0] - v3.StartPos, s[1] - v3.StartPos}
l = append(l, t)
} else {
debugLogger(fmt.Sprintf("%d", s[0] - v3.StartPos))
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions string/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
string-fuzz.zip
corpus
crashes
supressions
5 changes: 5 additions & 0 deletions string/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fuzz test run for almost an hour

```
020/05/20 14:23:13 workers: 12, corpus: 1473 (52m38s ago), crashers: 0, restarts: 1/9996, execs: 171550918 (54048/sec), cover: 299, uptime: 52m54s
```
21 changes: 21 additions & 0 deletions string/fuzz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package string

import (
"crypto/md5"
"encoding/hex"
)

// For fuzz testing...
// https://github.com/dvyukov/go-fuzz
// install both go-fuzz-build and go-fuzz
// go-fuzz-build && go-fuzz
func Fuzz(data []byte) int {

md5_d := md5.New()
find := hex.EncodeToString(md5_d.Sum(data))

IndexAll(string(data), find[:8], -1)
l := IndexAllIgnoreCaseUnicode(string(data), find[:8], -1)
HighlightString(string(data), l, "__IN__", "__OUT__")
return 1
}
2 changes: 2 additions & 0 deletions string/fuzz.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
go-fuzz-build && go-fuzz
12 changes: 11 additions & 1 deletion string/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import (
// Note that this method is explicitly case sensitive in its matching.
// A return value of nil indicates no match.
func IndexAll(haystack string, needle string, limit int) [][]int {
// The below needed to avoid timeout crash found using go-fuzz
if len(haystack) == 0 || len(needle) == 0 {
return nil
}

// Return contains a slice of slices where index 0 is the location of the match in bytes
// and index 1 contains the end location in bytes of the match
locs := [][]int{}
Expand Down Expand Up @@ -88,6 +93,11 @@ var __permuteCacheLock = sync.Mutex{}
// for S will search for S s and ſ which a simple strings.ToLower over the haystack
// and the needle will not.
func IndexAllIgnoreCaseUnicode(haystack string, needle string, limit int) [][]int {
// The below needed to avoid timeout crash found using go-fuzz
if len(haystack) == 0 || len(needle) == 0 {
return nil
}

// One of the problems with finding locations ignoring case is that
// the different case representations can have different byte counts
// which means the locations using strings or bytes Index can be off
Expand All @@ -109,8 +119,8 @@ func IndexAllIgnoreCaseUnicode(haystack string, needle string, limit int) [][]in
// you the need to validate a potential match after you have found one.
// The confirmation match is done in a loop because for some literals regular expression
// is still to slow, although for most its a valid option.

locs := [][]int{}

// Char limit is the cut-off where we switch from all case permutations
// to just the first 3 and then check for an actual match
// in my tests 3 speeds things up the most against test data
Expand Down
8 changes: 8 additions & 0 deletions string/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ func TestDropInReplacementMultiple(t *testing.T) {
}
}

func TestIndexAllIgnoreCaseUnicodeEmpty(t *testing.T) {
matches := IndexAllIgnoreCaseUnicode("", "2", -1)

if matches != nil {
t.Error("Expected no matches")
}
}

func TestIndexAllIgnoreCaseUnicodeLongNeedleNoMatch(t *testing.T) {
matches := IndexAllIgnoreCaseUnicode("aaaaabbbbb", "aaaaaa", -1)

Expand Down

0 comments on commit ac5e57e

Please sign in to comment.