1// run 2 3// Copyright 2010 The Go Authors. All rights reserved. 4// Use of this source code is governed by a BSD-style 5// license that can be found in the LICENSE file. 6 7// Issue 1011. Removing either #1 or #3 avoided the crash at #2. 8 9package main 10 11import ( 12 "io" 13 "strings" 14) 15 16func readU16BE(b []byte) uint16 { 17 b[0] = 0 18 b[1] = 1 19 return uint16(b[0])<<8 + uint16(b[1]) // #1 20 n := uint16(b[0])<<8 + uint16(b[1]) 21 return n 22} 23 24func readStr(r io.Reader, b []byte) string { 25 n := readU16BE(b) 26 if int(n) > len(b) { 27 return "err: n>b" 28 } 29 io.ReadFull(r, b[0:n]) // #2 30 return string(b[0:n]) // #3 31 return "ok" 32} 33 34func main() { 35 br := strings.NewReader("abcd") 36 readStr(br, make([]byte, 256)) 37} 38