]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: don't unread eof scanning %x
authorRob Pike <r@golang.org>
Mon, 10 Aug 2015 03:21:28 +0000 (13:21 +1000)
committerRob Pike <r@golang.org>
Mon, 10 Aug 2015 03:34:48 +0000 (03:34 +0000)
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 #12090.

Change-Id: Iad1ce8e7db8ec26615c5271310f4b0228cca7d78
Reviewed-on: https://go-review.googlesource.com/13461
Reviewed-by: Andrew Gerrand <adg@golang.org>
src/fmt/scan.go
src/fmt/scan_test.go

index 21ed091d80f6068b0d5df93f8d815e27574eddf8..5b9b516353b94c18ff3bf7538f61aac7b7acf897 100644 (file)
@@ -888,7 +888,6 @@ func hexDigit(d rune) (int, bool) {
 func (s *ss) hexByte() (b byte, ok bool) {
        rune1 := s.getRune()
        if rune1 == eof {
-               s.UnreadRune()
                return
        }
        value1, ok := hexDigit(rune1)
index 694f93e1aeb0a544a7f8fbc3351ce1b6eb36cb53..a3784364e630329d9b9214dd8af80dd426e92321 100644 (file)
@@ -1128,3 +1128,31 @@ func TestScanfNewlineMatchFormat(t *testing.T) {
                }
        }
 }
+
+// Test for issue 12090: Was unreading at EOF, double-scanning a byte.
+
+type hexBytes [2]byte
+
+func (h *hexBytes) Scan(ss ScanState, verb rune) error {
+       var b []byte
+       _, err := Fscanf(ss, "%4x", &b)
+       if err != nil {
+               panic(err) // Really shouldn't happen.
+       }
+       copy((*h)[:], b)
+       return err
+}
+
+func TestHexByte(t *testing.T) {
+       var h hexBytes
+       n, err := Sscanln("0123\n", &h)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if n != 1 {
+               t.Fatalf("expected 1 item; scanned %d", n)
+       }
+       if h[0] != 0x01 || h[1] != 0x23 {
+               t.Fatalf("expected 0123 got %x", h)
+       }
+}