discord / lilliput

Resize images and animated GIFs in Go
https://discord.com/blog/how-discord-resizes-150-million-images-every-day-with-go-and-c
Other
1.94k stars 124 forks source link

Fix handling of source images that lack ICC profile data. #154

Closed skidder closed 2 months ago

skidder commented 2 months ago

Fixes a regression introduced in #143 that broke handling of source images that do not contain ICC color profile data: https://github.com/discord/lilliput/commit/3e9617a036fd8bc0b999e5667cf633d30bf5660f#diff-79cd9917335eff2b35914b1036a3bc233c3b0cb7750c306021e93a2752f42e5dR114

An attempt to read the first byte of a zero-length array led to a panic.

panic: runtime error: index out of range [0] with length 0
goroutine 122 [running]:
github.com/discord/lilliput.newWebpEncoder.func1(0xc000d08c20, 0xc000d08c20)
#011/root/go/pkg/mod/github.com/discord/lilliput@v0.0.0-20240426221002-a7dee7f9fa87/webp.go:114 +0x10d
github.com/discord/lilliput.newWebpEncoder({0x21a2d30, 0xc0ad253ce0}, {0xc0ad700000, 0x1, 0x6400000})
#011/root/go/pkg/mod/github.com/discord/lilliput@v0.0.0-20240426221002-a7dee7f9fa87/webp.go:114 +0x6b
github.com/discord/lilliput.NewEncoder({0x18eaa3b, 0x5}, {0x21a2d30, 0xc0ad253ce0}, {0xc0ad700000, 0x0, 0x6400000})
#011/root/go/pkg/mod/github.com/discord/lilliput@v0.0.0-20240426221002-a7dee7f9fa87/lilliput.go:129 +0xd2

This PR addresses that issue, along with adding safe-guards & unit tests for ICC profile preservation and WebP processing in general.