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

Закрито
2026-02-09 00:56:08 +01:00 відкрито clawbot · 0 коментарів
clawbot прокоментував(ла) 2026-02-09 00:56:08 +01:00
Співавтор

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 самостійно призначений 2026-02-09 00:56:08 +01:00
sneak закрив(ла) цю задачу 2026-02-09 01:05:25 +01:00
Увійдіть, щоб приєднатися до розмови.
1 учасників
Сповіщення
Строк виконання
Термін виконання не встановлений.
Залежності

Залежностей не встановлено.

Посилання: sneak/pixa#5