From 4db8e325477717544dd22e553a1968d4e486a1e0 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Wed, 26 Aug 2026 15:08:33 +0200 Subject: [PATCH] Compare record fields by ordering, not by subtraction RecordObjectCompare computed its result as (int)(a - b) on uintmax_t and intmax_t fields. That truncates: a difference which is an exact multiple of 2^32 yields 0, so two records already known to differ compared equal and diff dropped the change without any output. $ fsnap diff old.fsnap new.fsnap # size 0 vs 4294967296 $ echo $? 0 The subtraction on intmax_t was also undefined on overflow. The explicit casts are what kept -Wconversion quiet about all of it. Replace them with cmp_intmax/cmp_uintmax helpers returning -1/0/1. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx --- src/types.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/types.c b/src/types.c index 4c4a9f7..edc38fb 100644 --- a/src/types.c +++ b/src/types.c @@ -177,6 +177,16 @@ static int parse_intmax_nonnegative(const char *s, intmax_t *out) { return 0; } +/* Comparing by subtracting truncates once the difference leaves the range of + int, which can make two different values look equal. Compare instead. */ +static int cmp_uintmax(uintmax_t a, uintmax_t b) { + return (a < b) ? -1 : (a > b) ? 1 : 0; +} + +static int cmp_intmax(intmax_t a, intmax_t b) { + return (a < b) ? -1 : (a > b) ? 1 : 0; +} + static char file_type(mode_t mode) { if (S_ISREG(mode)) return 'F'; @@ -345,31 +355,31 @@ int RecordObjectCompare(const struct RecordObject *self, int exit_code = 0; if (self->file_type != other->file_type) { - exit_code = ((int)(self->file_type - other->file_type)); + exit_code = cmp_intmax(self->file_type, other->file_type); if (err) *err = FILE_TYPE; } else if (self->perm != other->perm) { - exit_code = ((int)(self->perm - other->perm)); + exit_code = cmp_uintmax(self->perm, other->perm); if (err) *err = PERMISSIONS; } else if (self->uid != other->uid) { - exit_code = ((int)(self->uid - other->uid)); + exit_code = cmp_uintmax(self->uid, other->uid); if (err) *err = UID; } else if (self->gid != other->gid) { - exit_code = ((int)(self->gid - other->gid)); + exit_code = cmp_uintmax(self->gid, other->gid); if (err) *err = GID; } else if (self->size != other->size) { - exit_code = ((int)(self->size - other->size)); + exit_code = cmp_intmax(self->size, other->size); if (err) *err = SIZE; } else if (self->time != other->time) { - exit_code = ((int)(self->time - other->time)); + exit_code = cmp_intmax(self->time, other->time); if (err) *err = TIME; } else if (self->fingerprint != other->fingerprint) { - exit_code = ((int)(self->fingerprint - other->fingerprint)); + exit_code = cmp_uintmax(self->fingerprint, other->fingerprint); if (err) *err = FINGERPRINT; } else {