tpl: Fix panic in sort of nil sequence
authorCameron Moore <moorereason@gmail.com>
Tue, 29 Mar 2016 23:54:46 +0000 (18:54 -0500)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 2 Apr 2016 21:06:48 +0000 (23:06 +0200)
Properly handle a nil sequence in the sortSeq function.  Test included.
Discovered while using `range sort .Site.Data.source.Undefined "foo"`.

tpl/template_funcs.go
tpl/template_funcs_test.go

index ad5b8db1e35bd0d0e82d87eab589a4f79ad3925e..cd7fff26cb91e0f846072ec671b1ccea3136acf8 100644 (file)
@@ -957,6 +957,10 @@ func delimit(seq, delimiter interface{}, last ...interface{}) (template.HTML, er
 
 // sortSeq returns a sorted sequence.
 func sortSeq(seq interface{}, args ...interface{}) (interface{}, error) {
+       if seq == nil {
+               return nil, errors.New("sequence must be provided")
+       }
+
        seqv := reflect.ValueOf(seq)
        seqv, isNil := indirect(seqv)
        if isNil {
index 4b49da41b04445a2f5b84943622d5972c8e87714..eb5464c0233880d29b91a38d6fadf15f695f45a9 100644 (file)
@@ -1565,6 +1565,7 @@ func TestSort(t *testing.T) {
                        "asc",
                        false,
                },
+               {nil, nil, "asc", false},
        } {
                var result interface{}
                var err error