Skip to content

image/jpeg: panic: runtime error: index out of range #10154

Closed
@needkane

Description

@needkane

Image:
http://s3-us-west-2.amazonaws.com/qiniu-bs/panicImage

package main

import (
        "fmt"
        "image"
        "os"

        _ "image/jpeg"
)

func main(){
        f , _ := os.Open("panicImage")
        defer f.Close()
        m, _, err := image.Decode(f)
        if err != nil {
                fmt.Println("abnormal jpeg")
        }else{
                fmt.Println(m.ColorModel)
        }   
}
kane:qbox mac$ go run getPanic.go
Result:
goroutine 1 [running]:
image/jpeg.(*decoder).processSOS(0x102b6000, 0xa, 0x0, 0x0)
    /usr/local/go/src/image/jpeg/scan.go:143 +0x1e85
image/jpeg.(*decoder).decode(0x102b6000, 0x3027bad8, 0x102ac0c0, 0x102ac000, 0x0, 0x0, 0x0, 0x0)
    /usr/local/go/src/image/jpeg/reader.go:474 +0x53c
image/jpeg.Decode(0x3027bad8, 0x102ac0c0, 0x0, 0x0, 0x0, 0x0)
    /usr/local/go/src/image/jpeg/reader.go:498 +0x63
image.Decode(0x3027ba80, 0x102721b8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
    /usr/local/go/src/image/format.go:84 +0x106
main.main()
    /Users/mac/qbox/getPanic.go:14 +0x78

In my opinion:
No matter the image is normal or not ,panic was unexpected.
If the image is normal ,golang need give a normal result.
If the image is abnormal,golang should throw error not panic.
I modified the golang source code image/jpeg/scan.go

for i := 0; i < nComp; i++ {
                compIndex := scan[i].compIndex
+               if d.comp[compIndex].tq > maxTq {
+                   return FormatError("index is greater than maxTq")
+               }
                qt := &d.quant[d.comp[compIndex].tq]
                for j := 0; j < d.comp[compIndex].h*d.comp[compIndex].v; j++ {

I use golang contribute ,but i don't know if it works success or not.
jpeg panic

Change-Id: I45d8c88b84c47ac21adc814c25a426fa4aa1d209

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch needkane
# Your branch is ahead of 'origin/master' by 1 commit.
#   (use "git push" to publish your local commits)
#
# Changes to be committed:
#       modified:   src/image/jpeg/scan.go
#

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions