mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 03:09:51 +00:00
crc32 returned 0 both for a read error and as a genuine checksum, so the
caller had to remember to test ferror separately. That is precisely how
the descriptor leak fixed in 9e65819 got in.
The ambiguity is not hypothetical: an empty file checksums to exactly 0,
and create writes one for every empty file it walks.
Take a uint32_t out parameter and return 0/-1 instead.
Verified byte-identical snapshots against the previous build, and the
values still agree with zlib.crc32 for both an empty and a 100KB file.
A read failure still surfaces: create over a symlink to a directory
reports "Is a directory".
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
14 lines
416 B
C
14 lines
416 B
C
#ifndef HASH_H
|
|
#define HASH_H
|
|
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
|
|
/* Compute the CRC-32 checksum of the stream's remaining contents into *out.
|
|
Returns 0 on success, -1 on read error, leaving *out untouched. The result
|
|
is reported through an out parameter because 0 is a perfectly good checksum
|
|
and would otherwise be indistinguishable from a failure. */
|
|
int crc32(FILE *stream, uint32_t *out);
|
|
|
|
#endif
|