Bug: Division by zero in processAndStore when source data is empty #5

Zamknięty
otworzone 2026-02-09 00:56:08 +01:00 przez clawbot · 0 komentarzy
Collaborator

Bug

In internal/imgcache/service.go, processAndStore() computes a size ratio:

sizePercent := float64(outputSize) / float64(fetchBytes) * 100.0

When fetchBytes is 0 (e.g., empty cached source content, or a zero-length upstream response that somehow passed validation), this causes a division by zero, producing +Inf or NaN which gets logged and could cause issues with metrics/monitoring systems.

Additionally, in processFromSourceOrFetch(), when source data is loaded from cache, fetchBytes is set to int64(len(sourceData)) which will be 0 for empty data, and this is passed to processAndStore().

Impact

Panics won't occur (Go float division by zero produces Inf), but:

  • Log output contains +Inf or NaN which breaks log parsing
  • Metrics systems may reject or mishandle these values
  • The underlying issue (empty source) should be caught earlier

Fix

Add a guard for fetchBytes == 0 before the division, and also validate that source data is non-empty before attempting to process it.

## Bug In `internal/imgcache/service.go`, `processAndStore()` computes a size ratio: ```go sizePercent := float64(outputSize) / float64(fetchBytes) * 100.0 ``` When `fetchBytes` is 0 (e.g., empty cached source content, or a zero-length upstream response that somehow passed validation), this causes a division by zero, producing `+Inf` or `NaN` which gets logged and could cause issues with metrics/monitoring systems. Additionally, in `processFromSourceOrFetch()`, when source data is loaded from cache, `fetchBytes` is set to `int64(len(sourceData))` which will be 0 for empty data, and this is passed to `processAndStore()`. ## Impact Panics won't occur (Go float division by zero produces Inf), but: - Log output contains `+Inf` or `NaN` which breaks log parsing - Metrics systems may reject or mishandle these values - The underlying issue (empty source) should be caught earlier ## Fix Add a guard for `fetchBytes == 0` before the division, and also validate that source data is non-empty before attempting to process it.
clawbot przypisuje to na siebie 2026-02-09 00:56:08 +01:00
sneak zamknął(-ęła) to zgłoszenie 2026-02-09 01:05:25 +01:00
Zaloguj się, aby dołączyć do tej rozmowy.
Uczestnicy 1
Powiadomienia
Termin realizacji
Brak ustawionego terminu realizacji.
Zależności

No dependencies set.

Reference: sneak/pixa#5