hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit be017f187e7c9beac6baabdba32bdb5753f9b4d4
parent be3b8a132ba72aa3a797101f942e555610553f72
Author: bep <bjorn.erik.pedersen@gmail.com>
Date:   Thu, 30 Apr 2015 11:26:34 +0200

tpl: check slice bounds in slicestr

Fixes #1090

Diffstat:
Mtpl/template_funcs.go | 7+++++++
Mtpl/template_funcs_test.go | 5+++++
Mtpl/template_test.go | 6+++++-
3 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/tpl/template_funcs.go b/tpl/template_funcs.go
@@ -139,7 +139,14 @@ func Slicestr(a interface{}, startEnd ...int) (string, error) {
 		return "", errors.New("too many arguments")
 	}
 
+	if len(startEnd) > 0 && (startEnd[0] < 0 || startEnd[0] >= len(aStr)) {
+		return "", errors.New("slice bounds out of range")
+	}
+
 	if len(startEnd) == 2 {
+		if startEnd[1] < 0 || startEnd[1] > len(aStr) {
+			return "", errors.New("slice bounds out of range")
+		}
 		return aStr[startEnd[0]:startEnd[1]], nil
 	} else if len(startEnd) == 1 {
 		return aStr[startEnd[0]:], nil
diff --git a/tpl/template_funcs_test.go b/tpl/template_funcs_test.go
@@ -291,6 +291,11 @@ func TestSlicestr(t *testing.T) {
 		{"abcdef", []int{2}, "cdef"},
 		{123, []int{1, 3}, "23"},
 		{123, []int{1, 2, 3}, false},
+		{"abcdef", []int{6}, false},
+		{"abcdef", []int{4, 7}, false},
+		{"abcdef", []int{-1}, false},
+		{"abcdef", []int{-1, 7}, false},
+		{"abcdef", []int{1, -1}, false},
 		{tstNoStringer{}, []int{0, 1}, false},
 	} {
 		result, err := Slicestr(this.v1, this.v2...)
diff --git a/tpl/template_test.go b/tpl/template_test.go
@@ -11,7 +11,11 @@ func TestTplGoFuzzReports(t *testing.T) {
 	for i, this := range []struct {
 		data      string
 		expectErr int
-	}{{"{{apply .C \"first\" }}", 2}} {
+	}{
+		// Issue #1089
+		{"{{apply .C \"first\" }}", 2},
+		// Issue #1090
+		{"{{ slicestr \"000000\" 10}}", 2}} {
 		templ := New()
 
 		d := &Data{