Skip to content

fix: avoid panic on corrupt cache file - #915

Closed
Bolt4243 wants to merge 1 commit into
gocolly:masterfrom
Bolt4243:fix/cachedir-corrupt-file-panic
Closed

Bolt4243 wants to merge 1 commit into
gocolly:masterfrom
Bolt4243:fix/cachedir-corrupt-file-panic

Conversation

@Bolt4243

Copy link
Copy Markdown

Fixes #914.

Problem

When CacheDir is enabled, a corrupt or truncated cache file crashes the process with a nil-pointer dereference (SIGSEGV). In httpBackend.Cache the gob decode error is ignored, and on a partial/corrupt file the decoded Response.Headers is left nil; the following *resp.Headers deref then panics. There is no recover(), so the process (or an async worker goroutine) crashes.

This can be triggered by an interrupted write, a disk-full condition, a crash mid-write, or external tampering.

Fix

Check the gob decode error and nil Headers before dereferencing the cached Response. On any decode error, remove the corrupt file and fall through to a live fetch. Behavior for valid cache files is unchanged.

Test

Adds TestCorruptCacheFileFallsBackToFetch (http_backend_test.go): pre-writes a truncated gob at the correct cache path for the normalized URL, then visits and asserts no panic, a live fetch occurs with the correct body, and the corrupt file is replaced with a valid cache entry. Without the fix this test panics with SIGSEGV; with the fix the full suite passes.

Check the gob decode error and nil Headers before dereferencing the
cached Response. A truncated or corrupt CacheDir file (interrupted
write, disk full, external tampering) left resp.Headers nil and the
subsequent *resp.Headers deref crashed the process with a SIGSEGV.
On any decode error, remove the corrupt file and fall back to a live
fetch.
@WGH- WGH- closed this Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Corrupt/truncated CacheDir file causes nil-pointer panic (SIGSEGV)

2 participants