Update UTF8Reader to fix slice bounds out of range error

This commit is contained in:
Iwasaki Yudai 2015-08-17 12:32:45 -07:00
parent 5e3796d644
commit bc109b4f5b
5 changed files with 45 additions and 2 deletions

2
Godeps/Godeps.json generated
View File

@ -22,7 +22,7 @@
}, },
{ {
"ImportPath": "github.com/yudai/utf8reader", "ImportPath": "github.com/yudai/utf8reader",
"Rev": "543610cf49fc1279921d6ef1b7f7773ebece14f8" "Rev": "0ccad3e5e2d8dc2493179319c4c8d1172f583ea4"
} }
] ]
} }

View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2015 Iwasaki Yudai
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -63,3 +63,7 @@ Of course, bytes left behind will be used to fill up the buffer on next `Read()`
## Note ## Note
UTF8Reader just checks incomplete bytes at the tail of the buffer. Even if the original byte sequence given to UTF8Reader is broken, UTF8Reader reports no errors and just fills up the buffer. UTF8Reader just checks incomplete bytes at the tail of the buffer. Even if the original byte sequence given to UTF8Reader is broken, UTF8Reader reports no errors and just fills up the buffer.
## License
The MIT License

View File

@ -43,7 +43,7 @@ func (r *UTF8Reader) Read(p []byte) (n int, err error) {
} }
leftOver := 0 leftOver := 0
for ; leftOver < utf8.UTFMax; leftOver++ { for ; leftOver < utf8.UTFMax && size-leftOver > 0; leftOver++ {
rune, _ := utf8.DecodeLastRune(p[:size-leftOver]) rune, _ := utf8.DecodeLastRune(p[:size-leftOver])
if rune != utf8.RuneError { if rune != utf8.RuneError {
break break

View File

@ -77,3 +77,21 @@ func TestReadWithSmallBuffer(t *testing.T) {
t.Errorf("Expected error were not returned") t.Errorf("Expected error were not returned")
} }
} }
func TestReadWithSmallRead(t *testing.T) {
input := []byte("いろは")
or := bytes.NewBuffer(input[0:2]) // small read
r := New(or)
buf := make([]byte, 512)
_, err := r.Read(buf)
if err != nil {
t.Errorf("Unexpected error")
}
or.Write(input[2:6])
_, err = r.Read(buf)
if err != nil {
t.Errorf("Unexpected error")
}
}