shssoichiro / oxipng

Multithreaded PNG optimizer written in Rust
MIT License
2.95k stars 124 forks source link

Optimize color counting #598

Closed kornelski closed 8 months ago

kornelski commented 8 months ago

I've seen a panic in most_popular_edge_color, so I've made it more resilient.

AlexTMjugador commented 8 months ago

Thank you, these changes look sensible. Do you have any clue on why len can be outside of the expected range in the affected code?

kornelski commented 8 months ago

I only have a core dump without the image, so I don't know exactly.

A common tricky case that may be related is pixels having higher values than palette length.

It also could have been 1x1 interlaced image.

andrews05 commented 8 months ago

Thanks for this @kornelski. I'll take a closer look later and try to figure out what would actually cause this, but I'm surprised it can happen since:

  1. The image should have already been cleaned from any palette issues at this point.
  2. Interlaced images are explicitly excluded from the co_occurrence_matrix sort methods.
andrews05 commented 8 months ago

I managed to reproduce a crash at that point. @kornelski was right - it can happen when palette entries are missing. The reduced_palette is supposed to fix any such issues, but it won't if the palette was one entry too short. I'll make up a separate PR with a test case to fix this.