nfnt / resize

Pure golang image resizing
ISC License
3.02k stars 321 forks source link

Don't resize inputs with zero width or height #55

Closed athomason closed 6 years ago

athomason commented 7 years ago

Inputs having zero as both width and height were caught, but either one alone caused a panic. This adds a test showing the old failing behavior and adds a check for non-positive width/height. Since Resize doesn't have an error return value, the behavior in this case is to return the original image.

Fixes #52

This is the panic, which is uncatchable by client code since it happens in a new goroutine:

=== RUN   Test_HalfZeroImg
--- FAIL: Test_HalfZeroImg (0.00s)
panic: runtime error: makeslice: len out of range [recovered]
    panic: runtime error: makeslice: len out of range

goroutine 19 [running]:
panic(0x5122e0, 0xc420088150)
    /home/adam/go/gos/go1.7.3/src/runtime/panic.go:500 +0x1a1
testing.tRunner.func1(0xc42008e240)
    /home/adam/go/gos/go1.7.3/src/testing/testing.go:579 +0x25d
panic(0x5122e0, 0xc420088150)
    /home/adam/go/gos/go1.7.3/src/runtime/panic.go:458 +0x243
github.com/nfnt/resize.createWeightsNearest(0x8000000000000000, 0x2, 0x3ff0000000000000, 0x0, 0x0, 0x0, 0xc42000e760, 0xc420012680, 0xc4200127c0, 0x3f9eb851eb851eb8, ...)
    /home/adam/go/src/github.com/nfnt/resize/filters.go:127 +0x114
github.com/nfnt/resize.resizeNearest(0x8000000000000000, 0x1, 0x0, 0x0, 0x5b8280, 0xc420084180, 0x0, 0xffffffffffffffee, 0x5b6780)
    /home/adam/go/src/github.com/nfnt/resize/resize.go:535 +0x26a5
github.com/nfnt/resize.Resize(0x0, 0x1, 0x5b8280, 0xc420084180, 0x0, 0x0, 0x100000000)
    /home/adam/go/src/github.com/nfnt/resize/resize.go:101 +0x4963
github.com/nfnt/resize.Test_HalfZeroImg(0xc42008e240)
    /home/adam/go/src/github.com/nfnt/resize/resize_test.go:43 +0x112
testing.tRunner(0xc42008e240, 0x542f58)
    /home/adam/go/gos/go1.7.3/src/testing/testing.go:610 +0x81
created by testing.(*T).Run
    /home/adam/go/gos/go1.7.3/src/testing/testing.go:646 +0x2ec
exit status 2
FAIL    github.com/nfnt/resize  0.008s

This happens because the indexing arithmetic in resizeGray and friends attempts to access index 0 of a slice with no pixel data.

emildd commented 7 years ago

Hi. this code :

`func merging(c *gin.Context) {

    profiles := c.PostForm("profiles")
images := c.PostForm("image")
filename := c.PostForm("filename")
pro, err := os.Open(profiles)
im, err := os.Open(images)
if err != nil {
    fmt.Println(err)
}
img1, _, err := image.Decode(pro)
img2, _, err := image.Decode(im)
if err != nil {
    fmt.Println(err)
}
//resize
imgs1 := resize.Resize(192, 192, img1, resize.Lanczos3)
imgs2 := resize.Resize(25, 25, img2, resize.Lanczos3)`

This error is : runtime error: invalid memory address or nil pointer dereference /usr/local/go/src/runtime/panic.go:458 (0x31773) gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz)) /usr/local/go/src/runtime/panic.go:62 (0x302cd) panicmem: panic(memoryError) /usr/local/go/src/runtime/sigpanic_unix.go:24 (0x461a4) sigpanic: panicmem() /Users/emildmaulana/go/src/github.com/nfnt/resize/resize.go:82 (0x109d2a) Resize: scaleX, scaleY := calcFactors(width, height, float64(img.Bounds().Dx()), float64(img.Bounds().Dy())) /Users/emildmaulana/go/gocrud/main.go:442 (0x53e0) merging: imgs1 := resize.Resize(192, 192, img1, resize.Lanczos3)