Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Kirill Smelkov
go
Commits
214a1ca3
Commit
214a1ca3
authored
13 years ago
by
Rob Pike
Browse files
Options
Download
Email Patches
Plain Diff
html/template: fix nil pointer bug
Fixes #3272. R=golang-dev, rsc CC=golang-dev
https://golang.org/cl/5819046
parent
9eeb9094
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
3 deletions
+18
-3
src/pkg/html/template/escape_test.go
src/pkg/html/template/escape_test.go
+9
-0
src/pkg/html/template/template.go
src/pkg/html/template/template.go
+9
-3
No files found.
src/pkg/html/template/escape_test.go
View file @
214a1ca3
...
...
@@ -8,6 +8,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"text/template"
...
...
@@ -1637,6 +1638,14 @@ func TestIndirectPrint(t *testing.T) {
}
}
// This is a test for issue 3272.
func
TestEmptyTemplate
(
t
*
testing
.
T
)
{
page
:=
Must
(
New
(
"page"
)
.
ParseFiles
(
os
.
DevNull
))
if
err
:=
page
.
ExecuteTemplate
(
os
.
Stdout
,
"page"
,
"nothing"
);
err
==
nil
{
t
.
Fatal
(
"expected error"
)
}
}
func
BenchmarkEscapedExecute
(
b
*
testing
.
B
)
{
tmpl
:=
Must
(
New
(
"t"
)
.
Parse
(
`<a onclick="alert('{{.}}')">{{.}}</a>`
))
var
buf
bytes
.
Buffer
...
...
This diff is collapsed.
Click to expand it.
src/pkg/html/template/template.go
View file @
214a1ca3
...
...
@@ -64,7 +64,13 @@ func (t *Template) lookupAndEscapeTemplate(name string) (tmpl *Template, err err
t
.
nameSpace
.
mu
.
Lock
()
defer
t
.
nameSpace
.
mu
.
Unlock
()
tmpl
=
t
.
set
[
name
]
if
(
tmpl
==
nil
)
!=
(
t
.
text
.
Lookup
(
name
)
==
nil
)
{
if
tmpl
==
nil
{
return
nil
,
fmt
.
Errorf
(
"html/template: %q is undefined"
,
name
)
}
if
tmpl
.
text
.
Tree
==
nil
||
tmpl
.
text
.
Root
==
nil
{
return
nil
,
fmt
.
Errorf
(
"html/template: %q is an incomplete template"
,
name
)
}
if
t
.
text
.
Lookup
(
name
)
==
nil
{
panic
(
"html/template internal error: template escaping out of sync"
)
}
if
tmpl
!=
nil
&&
!
tmpl
.
escaped
{
...
...
@@ -276,7 +282,7 @@ func (t *Template) ParseFiles(filenames ...string) (*Template, error) {
func
parseFiles
(
t
*
Template
,
filenames
...
string
)
(
*
Template
,
error
)
{
if
len
(
filenames
)
==
0
{
// Not really a problem, but be consistent.
return
nil
,
fmt
.
Errorf
(
"template: no files named in call to ParseFiles"
)
return
nil
,
fmt
.
Errorf
(
"
html/
template: no files named in call to ParseFiles"
)
}
for
_
,
filename
:=
range
filenames
{
b
,
err
:=
ioutil
.
ReadFile
(
filename
)
...
...
@@ -333,7 +339,7 @@ func parseGlob(t *Template, pattern string) (*Template, error) {
return
nil
,
err
}
if
len
(
filenames
)
==
0
{
return
nil
,
fmt
.
Errorf
(
"template: pattern matches no files: %#q"
,
pattern
)
return
nil
,
fmt
.
Errorf
(
"
html/
template: pattern matches no files: %#q"
,
pattern
)
}
return
parseFiles
(
t
,
filenames
...
)
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment