]> Cypherpunks repositories - gostls13.git/commitdiff
text/template: 0xef is an integer, not a floating-point value.
authorRob Pike <r@golang.org>
Wed, 3 Sep 2014 22:57:03 +0000 (15:57 -0700)
committerRob Pike <r@golang.org>
Wed, 3 Sep 2014 22:57:03 +0000 (15:57 -0700)
The discriminator in the execution engine was stupid.
Add a test to the parse package too. The problem wasn't there
but the particular case ('e' in a hex integer) was not covered.

Fixes #8622.

LGTM=ruiu
R=golang-codereviews, ruiu
CC=golang-codereviews
https://golang.org/cl/133530043

src/pkg/text/template/exec.go
src/pkg/text/template/exec_test.go
src/pkg/text/template/parse/parse_test.go

index 2f323126453fdc1ad6e1b8f03135128b83584e99..8e155d478ecd17c91a5eb94213b46c68e8a12ed1 100644 (file)
@@ -393,7 +393,7 @@ func (s *state) idealConstant(constant *parse.NumberNode) reflect.Value {
        switch {
        case constant.IsComplex:
                return reflect.ValueOf(constant.Complex128) // incontrovertible.
-       case constant.IsFloat && strings.IndexAny(constant.Text, ".eE") >= 0:
+       case constant.IsFloat && !isHexConstant(constant.Text) && strings.IndexAny(constant.Text, ".eE") >= 0:
                return reflect.ValueOf(constant.Float64)
        case constant.IsInt:
                n := int(constant.Int64)
@@ -407,6 +407,10 @@ func (s *state) idealConstant(constant *parse.NumberNode) reflect.Value {
        return zero
 }
 
+func isHexConstant(s string) bool {
+       return len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')
+}
+
 func (s *state) evalFieldNode(dot reflect.Value, field *parse.FieldNode, args []parse.Node, final reflect.Value) reflect.Value {
        s.at(field)
        return s.evalFieldChain(dot, dot, field, field.Ident, args, final)
index 868f2cb94c392d649c6ab907e3d9c28cad8e646c..663aaf3af8c515e05d5782a39f2f9a4f379090cd 100644 (file)
@@ -514,6 +514,11 @@ var execTests = []execTest{
        {"bug10", "{{mapOfThree.three}}-{{(mapOfThree).three}}", "3-3", 0, true},
        // Dereferencing nil pointer while evaluating function arguments should not panic. Issue 7333.
        {"bug11", "{{valueString .PS}}", "", T{}, false},
+       // 0xef gave constant type float64. Issue 8622.
+       {"bug12xe", "{{printf `%T` 0xef}}", "int", T{}, true},
+       {"bug12xE", "{{printf `%T` 0xEE}}", "int", T{}, true},
+       {"bug12Xe", "{{printf `%T` 0Xef}}", "int", T{}, true},
+       {"bug12XE", "{{printf `%T` 0XEE}}", "int", T{}, true},
 }
 
 func zeroArgs() string {
index fa6790bef9c24f8b03ace9aadeac0e5a4f45b1cd..4a504fa7c839ccbd33d84274cda671fd74b8423e 100644 (file)
@@ -69,6 +69,8 @@ var numberTests = []numberTest{
        {text: "1+2."},
        {text: "'x"},
        {text: "'xx'"},
+       // Issue 8622 - 0xe parsed as floating point. Very embarrassing.
+       {"0xef", true, true, true, false, 0xef, 0xef, 0xef, 0},
 }
 
 func TestNumberParse(t *testing.T) {