Skip to content

Commit fe3d8d4

Browse files
committed
fmt: don't unread eof scanning %x
When scanning a hex byte at EOF, the code was ungetting the eof, which backed up the input and caused double-scanning of a byte. Delete the call to UnreadRune. This line appeared in 1.5 for some reason; it was not in 1.4 and should be removed again for 1.5 Fixes golang#12090. Change-Id: Iad1ce8e7db8ec26615c5271310f4b0228cca7d78 Reviewed-on: https://go-review.googlesource.com/13461 Reviewed-by: Andrew Gerrand <adg@golang.org>
1 parent e7f4df7 commit fe3d8d4

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/fmt/scan.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,6 @@ func hexDigit(d rune) (int, bool) {
888888
func (s *ss) hexByte() (b byte, ok bool) {
889889
rune1 := s.getRune()
890890
if rune1 == eof {
891-
s.UnreadRune()
892891
return
893892
}
894893
value1, ok := hexDigit(rune1)

src/fmt/scan_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,3 +1128,31 @@ func TestScanfNewlineMatchFormat(t *testing.T) {
11281128
}
11291129
}
11301130
}
1131+
1132+
// Test for issue 12090: Was unreading at EOF, double-scanning a byte.
1133+
1134+
type hexBytes [2]byte
1135+
1136+
func (h *hexBytes) Scan(ss ScanState, verb rune) error {
1137+
var b []byte
1138+
_, err := Fscanf(ss, "%4x", &b)
1139+
if err != nil {
1140+
panic(err) // Really shouldn't happen.
1141+
}
1142+
copy((*h)[:], b)
1143+
return err
1144+
}
1145+
1146+
func TestHexByte(t *testing.T) {
1147+
var h hexBytes
1148+
n, err := Sscanln("0123\n", &h)
1149+
if err != nil {
1150+
t.Fatal(err)
1151+
}
1152+
if n != 1 {
1153+
t.Fatalf("expected 1 item; scanned %d", n)
1154+
}
1155+
if h[0] != 0x01 || h[1] != 0x23 {
1156+
t.Fatalf("expected 0123 got %x", h)
1157+
}
1158+
}

0 commit comments

Comments
 (0)