From 99f179f55a66f35dc7861fa411b42ed61bd0df31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20M=C3=B6hrmann?= Date: Mon, 20 Jul 2020 07:57:06 +0200 Subject: [PATCH] fmt: avoid badverb formatting for %q when used with integers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of returning a bad verb error format for runes above utf8.Maxrune return a quoted utf8.RuneError rune (\ufffd). This makes the behaviour consistent with the "c" verb and aligns behaviour to not return bad verb error format when a verb is applied to the correct argument type. Fixes #14569 Change-Id: I679485f6bb90ebe408423ab68af16cce38816cd0 Reviewed-on: https://go-review.googlesource.com/c/go/+/248759 Run-TryBot: Martin Möhrmann TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- src/fmt/fmt_test.go | 8 ++++---- src/fmt/print.go | 6 +----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/fmt/fmt_test.go b/src/fmt/fmt_test.go index 6004061020577..87fb32380946e 100644 --- a/src/fmt/fmt_test.go +++ b/src/fmt/fmt_test.go @@ -290,11 +290,11 @@ var fmtTests = []struct { {"%q", '\U00000e00', `'\u0e00'`}, {"%q", '\U0010ffff', `'\U0010ffff'`}, // Runes that are not valid. - {"%q", int32(-1), "%!q(int32=-1)"}, + {"%q", int32(-1), `'�'`}, {"%q", 0xDC80, `'�'`}, - {"%q", rune(0x110000), "%!q(int32=1114112)"}, - {"%q", int64(0xFFFFFFFFF), "%!q(int64=68719476735)"}, - {"%q", uint64(0xFFFFFFFFF), "%!q(uint64=68719476735)"}, + {"%q", rune(0x110000), `'�'`}, + {"%q", int64(0xFFFFFFFFF), `'�'`}, + {"%q", uint64(0xFFFFFFFFF), `'�'`}, // width {"%5s", "abc", " abc"}, diff --git a/src/fmt/print.go b/src/fmt/print.go index 595869140a1f9..778b5b0938f9b 100644 --- a/src/fmt/print.go +++ b/src/fmt/print.go @@ -388,11 +388,7 @@ func (p *pp) fmtInteger(v uint64, isSigned bool, verb rune) { case 'c': p.fmt.fmtC(v) case 'q': - if v <= utf8.MaxRune { - p.fmt.fmtQc(v) - } else { - p.badVerb(verb) - } + p.fmt.fmtQc(v) case 'U': p.fmt.fmtUnicode(v) default: