]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.regabi] cmd/compile: use '%q' for printing rune values less than 128
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Tue, 19 Jan 2021 15:57:45 +0000 (22:57 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Tue, 19 Jan 2021 18:27:50 +0000 (18:27 +0000)
Fixes #43762

Change-Id: I51734c9b4ee2366a5dae53b2d27b363f4d5fe6c1
Reviewed-on: https://go-review.googlesource.com/c/go/+/284592
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/ir/fmt.go
test/fixedbugs/issue43762.go [new file with mode: 0644]

index ee6a62625afdabf537be533cd5edba25fb4aadba..0ebfb842867b93e410dec6383a9610306d81f9a5 100644 (file)
@@ -589,20 +589,20 @@ func exprFmt(n Node, s fmt.State, prec int) {
                }
 
                if n.Type() == types.UntypedRune {
-                       switch x, ok := constant.Int64Val(n.Val()); {
+                       switch x, ok := constant.Uint64Val(n.Val()); {
                        case !ok:
                                fallthrough
                        default:
                                fmt.Fprintf(s, "('\\x00' + %v)", n.Val())
 
-                       case ' ' <= x && x < utf8.RuneSelf && x != '\\' && x != '\'':
-                               fmt.Fprintf(s, "'%c'", int(x))
+                       case x < utf8.RuneSelf:
+                               fmt.Fprintf(s, "%q", x)
 
-                       case 0 <= x && x < 1<<16:
-                               fmt.Fprintf(s, "'\\u%04x'", uint(int(x)))
+                       case x < 1<<16:
+                               fmt.Fprintf(s, "'\\u%04x'", x)
 
-                       case 0 <= x && x <= utf8.MaxRune:
-                               fmt.Fprintf(s, "'\\U%08x'", uint64(x))
+                       case x <= utf8.MaxRune:
+                               fmt.Fprintf(s, "'\\U%08x'", x)
                        }
                } else {
                        fmt.Fprint(s, types.FmtConst(n.Val(), s.Flag('#')))
diff --git a/test/fixedbugs/issue43762.go b/test/fixedbugs/issue43762.go
new file mode 100644 (file)
index 0000000..4544b6e
--- /dev/null
@@ -0,0 +1,11 @@
+// errorcheck
+
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+var _ = true == '\\' // ERROR "invalid operation: true == '\\\\'"
+var _ = true == '\'' // ERROR "invalid operation: true == '\\''"
+var _ = true == '\n' // ERROR "invalid operation: true == '\\n'"