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

Closed
opened 2026-02-09 00:56:08 +01:00 by clawbot · 0 comments
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 self-assigned this 2026-02-09 00:56:08 +01:00
sneak closed this issue 2026-02-09 01:05:25 +01:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sneak/pixa#5
No description provided.