// can happen when expecting to read a 0xff 0x00 byte-stuffed byte.
func (d *decoder) unreadByteStuffedByte() {
if d.bytes.nUnreadable == 0 {
- panic("jpeg: unreadByteStuffedByte call cannot be fulfilled")
+ return
}
d.bytes.i -= d.bytes.nUnreadable
d.bytes.nUnreadable = 0
// stuffing.
func (d *decoder) readFull(p []byte) error {
// Unread the overshot bytes, if any.
- if d.bytes.nUnreadable != 0 {
- if d.bits.n >= 8 {
- d.unreadByteStuffedByte()
- }
- d.bytes.nUnreadable = 0
- }
+ d.unreadByteStuffedByte()
for {
n := copy(p, d.bytes.buf[d.bytes.i:d.bytes.j])
// ignore ignores the next n bytes.
func (d *decoder) ignore(n int) error {
// Unread the overshot bytes, if any.
- if d.bytes.nUnreadable != 0 {
- if d.bits.n >= 8 {
- d.unreadByteStuffedByte()
- }
- d.bytes.nUnreadable = 0
- }
+ d.unreadByteStuffedByte()
for {
m := d.bytes.j - d.bytes.i
return s.String()
}
+func TestTruncatedSOSDataDoesntPanic(t *testing.T) {
+ b, err := ioutil.ReadFile("../testdata/video-005.gray.q50.jpeg")
+ if err != nil {
+ t.Fatal(err)
+ }
+ sosMarker := []byte{0xff, 0xda}
+ i := bytes.Index(b, sosMarker)
+ if i < 0 {
+ t.Fatal("SOS marker not found")
+ }
+ i += len(sosMarker)
+ j := i + 10
+ if j > len(b) {
+ j = len(b)
+ }
+ for ; i < j; i++ {
+ Decode(bytes.NewReader(b[:i]))
+ }
+}
+
func TestExtraneousData(t *testing.T) {
// Encode a 1x1 red image.
src := image.NewRGBA(image.Rect(0, 0, 1, 1))