diff --git a/scraping.go b/scraping.go index fe9c254..b77d6dd 100644 --- a/scraping.go +++ b/scraping.go @@ -379,9 +379,9 @@ func getFilterResultXPath(filter *Filter) { } default: { - var b bytes.Buffer - html.Render(&b, node) - filter.Results = append(filter.Results, html.UnescapeString(b.String())) + var b bytes.Buffer + html.Render(&b, node) + filter.Results = append(filter.Results, html.UnescapeString(b.String())) break } } @@ -446,9 +446,9 @@ func getFilterResultCSS(filter *Filter) { } default: { - var b bytes.Buffer - html.Render(&b, node) - filter.Results = append(filter.Results, html.UnescapeString(b.String())) + var b bytes.Buffer + html.Render(&b, node) + filter.Results = append(filter.Results, html.UnescapeString(b.String())) break } } @@ -514,6 +514,11 @@ func getFilterResultSubstring(filter *Filter) { } else if from < 0 { from = len(asRunes) + from } + if from < 0 { + filter.log("Out of bounds:", from_to) + continue + } + toStr := from_to[1] var hasTo bool = true if toStr == "" { @@ -527,8 +532,15 @@ func getFilterResultSubstring(filter *Filter) { } else if to < 0 { to = len(asRunes) + to } + if to < 0 { + filter.log("Out of bounds:", from_to) + continue + } if hasFrom && hasTo { - sb.WriteString(string(asRunes[from:to])) + _, err := sb.WriteString(string(asRunes[from:to])) + if err != nil { + filter.log("Could not substring: ", err) + } } else if hasFrom { sb.WriteString(string(asRunes[from:])) } else if hasTo { diff --git a/scraping_test.go b/scraping_test.go index ec3100c..3dc566d 100644 --- a/scraping_test.go +++ b/scraping_test.go @@ -277,6 +277,34 @@ func TestFilterSubstring(t *testing.T) { } } +func TestFilterSubstringOutOfBounds(t *testing.T) { + var tests = []struct { + Input string + Query string + }{ + {"01234", ":-6"}, + {"01234", "-6:"}, + } + + for _, test := range tests { + testname := fmt.Sprintf("%s %s", test.Input, test.Query) + t.Run(testname, func(t *testing.T) { + filter := Filter{ + Parents: []*Filter{ + {Results: []string{test.Input}}, + }, + Var1: test.Query, + } + getFilterResultSubstring( + &filter, + ) + if len(filter.Logs) == 0 { + t.Errorf("No log message, expected one for OoB") + } + }) + } +} + func TestFilterContains(t *testing.T) { var tests = []struct { Input []string diff --git a/todo.md b/todo.md index 2bfb1b9..0d06c67 100644 --- a/todo.md +++ b/todo.md @@ -13,6 +13,4 @@ - ~~inner~~ - ~~attributes~~ - ~~node~~ - - tests for all of it -- substring out of range? - - test it \ No newline at end of file + - tests for all of it \ No newline at end of file