Unify error handling: output parameter, goto cleanup, errno preservation

- Replace global field variable with output parameter enum Fields *err
- Adopt single goto cleanup pattern across all functions
- Add errno preservation around cleanup in all modules
- Fix early return bug in create.c cleanup block
- Add ferror check in crc32() for silent read errors
- Document error contracts in all public headers
This commit is contained in:
Fabio Scotto di Santolo
2026-08-26 14:20:00 +02:00
parent 464bb58c8c
commit c397728912
15 changed files with 237 additions and 195 deletions

View File

@@ -1,6 +1,8 @@
#ifndef CREATE_H #ifndef CREATE_H
#define CREATE_H #define CREATE_H
/* Create a snapshot of a directory tree.
Returns 0 on success, -1 on failure. */
int create(const char *directory, const char *output_file); int create(const char *directory, const char *output_file);
#endif #endif

View File

@@ -1,6 +1,8 @@
#ifndef DIFF_H #ifndef DIFF_H
#define DIFF_H #define DIFF_H
/* Compare two snapshot files and print added/deleted/modified entries.
Returns 0 on success, -1 on failure. */
int diff(const char *file1, const char *file2); int diff(const char *file1, const char *file2);
#endif #endif

View File

@@ -4,6 +4,8 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
/* Compute CRC-32 checksum of stream contents.
Returns 0 on read error. */
uint32_t crc32(FILE *stream); uint32_t crc32(FILE *stream);
#endif #endif

View File

@@ -1,6 +1,8 @@
#ifndef LIST_H #ifndef LIST_H
#define LIST_H #define LIST_H
/* List all entries in a snapshot file.
Returns 0 on success, -1 on system/IO error, 128 on parse error. */
int list(const char *snapshot); int list(const char *snapshot);
#endif #endif

View File

@@ -1,6 +1,8 @@
#ifndef SCAN_H #ifndef SCAN_H
#define SCAN_H #define SCAN_H
int scan(const char *); /* Recursively scan a directory, printing all entries.
Returns 0 on success, -1 on failure. */
int scan(const char *directory);
#endif #endif

View File

@@ -17,26 +17,41 @@ enum Fields {
NONE, NONE,
}; };
extern enum Fields field;
const char *RecordObjectFieldName(enum Fields f); const char *RecordObjectFieldName(enum Fields f);
struct RecordObject; struct RecordObject;
/* Methods */ /* Allocate a new RecordObject. Returns NULL on allocation failure. */
struct RecordObject *RecordObjectNew(); struct RecordObject *RecordObjectNew(void);
int RecordObjectUnpack(struct RecordObject *, char *);
/* Unpack a pipe-delimited record string into self.
Returns 0 on success, -1 on failure. If err is non-NULL, sets *err to
the field that failed to parse. */
int RecordObjectUnpack(struct RecordObject *self, char *s, enum Fields *err);
/* Populate self by stat-ing the file at path and writing to out.
Returns the number of bytes written on success, -1 on failure. */
int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out); int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out);
int RecordObjectCompare(const struct RecordObject *, const struct RecordObject *);
char RecordObjectFileType(const struct RecordObject *); /* Compare two RecordObjects field by field.
unsigned int RecordObjectPermissions(const struct RecordObject *); Returns 0 if equal, a non-zero difference otherwise.
uintmax_t RecordObjectUid(const struct RecordObject *); On NULL self returns 128, on NULL other returns 1.
uintmax_t RecordObjectGid(const struct RecordObject *); If err is non-NULL, sets *err to the first differing field. */
intmax_t RecordObjectSize(const struct RecordObject *); int RecordObjectCompare(const struct RecordObject *self,
intmax_t RecordObjectTime(const struct RecordObject *); const struct RecordObject *other, enum Fields *err);
const char *RecordObjectPath(const struct RecordObject *);
const char *RecordObjectTarget(const struct RecordObject *); /* Accessors */
uintmax_t RecordObjectFingerPrint(const struct RecordObject *); char RecordObjectFileType(const struct RecordObject *self);
int RecordObjectRelease(struct RecordObject *); unsigned int RecordObjectPermissions(const struct RecordObject *self);
uintmax_t RecordObjectUid(const struct RecordObject *self);
uintmax_t RecordObjectGid(const struct RecordObject *self);
intmax_t RecordObjectSize(const struct RecordObject *self);
intmax_t RecordObjectTime(const struct RecordObject *self);
const char *RecordObjectPath(const struct RecordObject *self);
const char *RecordObjectTarget(const struct RecordObject *self);
uintmax_t RecordObjectFingerPrint(const struct RecordObject *self);
/* Free self and all owned resources. Returns 0, or -1 if self is NULL. */
int RecordObjectRelease(struct RecordObject *self);
#endif #endif

View File

@@ -1,12 +1,20 @@
#ifndef UTILITY_H #ifndef UTILITY_H
#define UTILITY_H #define UTILITY_H
/* Concatenate src with n additional strings. Returns a newly allocated
string, or NULL on allocation failure. */
char *strconcat(const char *src, int n, ...); char *strconcat(const char *src, int n, ...);
/* Resolve path to an absolute canonical form. Returns a newly allocated
string, or NULL on failure (errno set by realpath). */
char *resolve_path(const char *path); char *resolve_path(const char *path);
/* Copy file contents from src to dst.
Returns 0 on success, -1 on failure. */
int copy(const char *src, const char *dst); int copy(const char *src, const char *dst);
/* Count lines in a file.
Returns the line count on success, -1 on failure. */
int count_lines(const char *file); int count_lines(const char *file);
#endif #endif

View File

@@ -1,8 +1,11 @@
#ifndef WALK_H #ifndef WALK_H
#define WALK_H #define WALK_H
typedef int(*WalkOp)(const char *, void *); typedef int (*WalkOp)(const char *, void *);
/* Recursively walk a directory, calling op for each entry.
Returns 0 on success, -1 on failure. Silently skips directories
where access is denied. */
int walk(const char *directory, WalkOp op, void *context); int walk(const char *directory, WalkOp op, void *context);
#endif #endif

View File

@@ -39,50 +39,44 @@ int create(const char *directory, const char *output_file) {
return -1; return -1;
} }
char *resolved = resolve_path(directory); int rc = -1;
if (resolved == NULL) { char *resolved = NULL;
return -1;
}
int rc = 0;
char template[] = "/tmp/snapshot_XXXXXX"; char template[] = "/tmp/snapshot_XXXXXX";
int fd = mkstemp(template); int fd = -1;
if (fd == -1) { FILE *tmp = NULL;
free(resolved);
return -1;
}
FILE *tmp = fdopen(fd, "w+"); resolved = resolve_path(directory);
if (resolved == NULL)
goto cleanup;
fd = mkstemp(template);
if (fd == -1)
goto cleanup;
tmp = fdopen(fd, "w+");
if (tmp == NULL) { if (tmp == NULL) {
close(fd); close(fd);
rc = -1; goto cleanup;
goto out;
} }
if (walk(resolved, write_record, (void *)tmp) == -1) { if (walk(resolved, write_record, (void *)tmp) == -1)
rc = -1; goto cleanup;
goto out;
}
if (fflush(tmp) == EOF) { if (fflush(tmp) == EOF)
rc = -1; goto cleanup;
goto out;
}
/* FIXME: It doesn't atomic copy, using rename */ /* FIXME: It doesn't atomic copy, using rename */
if (copy(template, output_file) == -1) { if (copy(template, output_file) == -1)
rc = -1; goto cleanup;
goto out;
}
out:; rc = 0;
/* A buffered write can fail for the first time here, so fclose owns errno
when nothing had failed before it. */ cleanup:;
const int saved_errno = errno; const int saved_errno = errno;
free(resolved); free(resolved);
unlink(template); unlink(template);
if (tmp != NULL && fclose(tmp) != 0 && rc == 0) if (tmp != NULL && fclose(tmp) != 0 && rc == 0)
return -1; rc = -1;
if (rc == -1) if (rc == -1)
errno = saved_errno; errno = saved_errno;
return rc; return rc;

View File

@@ -9,23 +9,23 @@
static int load_in_memory(const char *file, struct RecordObject ***records, static int load_in_memory(const char *file, struct RecordObject ***records,
int *count) { int *count) {
int exit_failure = -1; int rc = -1;
FILE *stream = NULL; FILE *stream = NULL;
char *buf = NULL; char *buf = NULL;
if ((stream = fopen(file, "r")) == NULL) if ((stream = fopen(file, "r")) == NULL)
goto failure; goto cleanup;
*count = count_lines(file); *count = count_lines(file);
if (*count == 0) if (*count == 0)
goto success; goto cleanup;
else if (*count < 0) if (*count < 0)
goto failure; goto cleanup;
*records = calloc((size_t)*count, sizeof(**records)); *records = calloc((size_t)*count, sizeof(**records));
if (*records == NULL) { if (*records == NULL) {
errno = ENOMEM; errno = ENOMEM;
goto failure; goto cleanup;
} }
int i = 0; int i = 0;
@@ -34,14 +34,14 @@ static int load_in_memory(const char *file, struct RecordObject ***records,
struct RecordObject *objp = RecordObjectNew(); struct RecordObject *objp = RecordObjectNew();
if (objp == NULL) { if (objp == NULL) {
errno = ENOMEM; errno = ENOMEM;
goto failure; goto cleanup;
} }
int ret = RecordObjectUnpack(objp, buf); enum Fields err;
int ret = RecordObjectUnpack(objp, buf, &err);
if (ret == -1) { if (ret == -1) {
errno = EINVAL;
RecordObjectRelease(objp); RecordObjectRelease(objp);
goto failure; goto cleanup;
} }
(*records)[i] = objp; (*records)[i] = objp;
@@ -49,32 +49,26 @@ static int load_in_memory(const char *file, struct RecordObject ***records,
} }
if (ferror(stream)) if (ferror(stream))
goto failure; goto cleanup;
success: rc = 0;
cleanup:;
const int saved_errno = errno;
if (stream != NULL) if (stream != NULL)
fclose(stream); fclose(stream);
if (buf != NULL) if (buf != NULL)
free(buf); free(buf);
errno = saved_errno;
return 0; return rc;
failure:
if (stream != NULL)
fclose(stream);
if (buf != NULL)
free(buf);
return exit_failure;
} }
static int dealloc(struct RecordObject **records, int size) { static void dealloc(struct RecordObject **records, int size) {
for (int j = 0; j < size; j++) { for (int j = 0; j < size; j++) {
RecordObjectRelease(records[j]); RecordObjectRelease(records[j]);
records[j] = NULL; records[j] = NULL;
} }
free(records); free(records);
return 0;
} }
static int order_by_path(const void *o1, const void *o2) { static int order_by_path(const void *o1, const void *o2) {
@@ -84,21 +78,17 @@ static int order_by_path(const void *o1, const void *o2) {
} }
int diff(const char *file1, const char *file2) { int diff(const char *file1, const char *file2) {
int exit_failure = -1; int rc = -1;
struct RecordObject **records1 = NULL; struct RecordObject **records1 = NULL;
struct RecordObject **records2 = NULL; struct RecordObject **records2 = NULL;
int len1 = 0; int len1 = 0;
int ret1 = load_in_memory(file1, &records1, &len1); if (load_in_memory(file1, &records1, &len1) == -1)
if (ret1 == -1) { goto cleanup;
goto failure;
}
int len2 = 0; int len2 = 0;
int ret2 = load_in_memory(file2, &records2, &len2); if (load_in_memory(file2, &records2, &len2) == -1)
if (ret2 == -1) { goto cleanup;
goto failure;
}
qsort(records1, (size_t)len1, sizeof(records1[0]), order_by_path); qsort(records1, (size_t)len1, sizeof(records1[0]), order_by_path);
qsort(records2, (size_t)len2, sizeof(records2[0]), order_by_path); qsort(records2, (size_t)len2, sizeof(records2[0]), order_by_path);
@@ -119,8 +109,10 @@ int diff(const char *file1, const char *file2) {
code = 'A'; code = 'A';
path = RecordObjectPath(r2); path = RecordObjectPath(r2);
n++; n++;
} else if (RecordObjectCompare(r1, r2) != 0) { } else {
switch (field) { enum Fields err = NONE;
if (RecordObjectCompare(r1, r2, &err) != 0) {
switch (err) {
case TARGET: case TARGET:
code = 'L'; code = 'L';
break; break;
@@ -138,6 +130,7 @@ int diff(const char *file1, const char *file2) {
m++; m++;
n++; n++;
} }
}
if (code != 0) { if (code != 0) {
fprintf(stdout, "%c\t%s\n", code, path); fprintf(stdout, "%c\t%s\n", code, path);
@@ -156,26 +149,12 @@ int diff(const char *file1, const char *file2) {
n++; n++;
} }
if (records1 != NULL) { rc = 0;
cleanup:
if (records1 != NULL)
dealloc(records1, len1); dealloc(records1, len1);
records1 = NULL; if (records2 != NULL)
}
if (records2 != NULL) {
dealloc(records2, len2); dealloc(records2, len2);
records2 = NULL; return rc;
}
return 0;
failure:
if (records1 != NULL) {
dealloc(records1, len1);
records1 = NULL;
}
if (records2 != NULL) {
dealloc(records2, len2);
records2 = NULL;
}
return exit_failure;
} }

View File

@@ -43,5 +43,8 @@ uint32_t crc32(FILE *stream) {
crc = crc32_update(crc, buffer, bytes_read); crc = crc32_update(crc, buffer, bytes_read);
} }
if (ferror(stream))
return 0;
return crc ^ 0xFFFFFFFF; return crc ^ 0xFFFFFFFF;
} }

View File

@@ -38,9 +38,11 @@ int list(const char *snapshot) {
rc = -1; rc = -1;
goto out; goto out;
} }
int ret = RecordObjectUnpack(objp, buf); enum Fields err = NONE;
if (field != NONE) { int ret = RecordObjectUnpack(objp, buf, &err);
fprintf(stderr, "%s:%d: invalid %s\n", snapshot, i + 1, RecordObjectFieldName(field)); if (err != NONE) {
fprintf(stderr, "%s:%d: invalid %s\n", snapshot, i + 1,
RecordObjectFieldName(err));
rc = 128; rc = 128;
RecordObjectRelease(objp); RecordObjectRelease(objp);
break; break;

View File

@@ -14,8 +14,6 @@
#define NFIELDS 9 #define NFIELDS 9
enum Fields field = NONE;
static const char *field_names[] = { static const char *field_names[] = {
"file type", "permissions", "uid", "gid", "size", "file type", "permissions", "uid", "gid", "size",
"time", "target", "fingerprint", "path", "none"}; "time", "target", "fingerprint", "path", "none"};
@@ -255,40 +253,44 @@ struct RecordObject *RecordObjectNew() {
return ret; return ret;
} }
int RecordObjectUnpack(struct RecordObject *self, char *s) { int RecordObjectUnpack(struct RecordObject *self, char *s, enum Fields *err) {
char *fields[NFIELDS]; char *fields[NFIELDS];
if (split_record(s, fields, '|') == -1) if (split_record(s, fields, '|') == -1)
return -1; return -1;
field = NONE;
if (parse_file_type(fields[0], &self->file_type) == -1) { if (parse_file_type(fields[0], &self->file_type) == -1) {
field = FILE_TYPE; if (err)
*err = FILE_TYPE;
return -1; return -1;
} }
if (parse_perm(fields[1], &self->perm) == -1) { if (parse_perm(fields[1], &self->perm) == -1) {
field = PERMISSIONS; if (err)
*err = PERMISSIONS;
return -1; return -1;
} }
if (parse_uintmax(fields[2], &self->uid) == -1) { if (parse_uintmax(fields[2], &self->uid) == -1) {
field = UID; if (err)
*err = UID;
return -1; return -1;
} }
if (parse_uintmax(fields[3], &self->gid) == -1) { if (parse_uintmax(fields[3], &self->gid) == -1) {
field = GID; if (err)
*err = GID;
return -1; return -1;
} }
if (parse_intmax_nonnegative(fields[4], &self->size) == -1) { if (parse_intmax_nonnegative(fields[4], &self->size) == -1) {
field = SIZE; if (err)
*err = SIZE;
return -1; return -1;
} }
if (parse_intmax_nonnegative(fields[5], &self->time) == -1) { if (parse_intmax_nonnegative(fields[5], &self->time) == -1) {
field = TIME; if (err)
*err = TIME;
return -1; return -1;
} }
@@ -296,7 +298,8 @@ int RecordObjectUnpack(struct RecordObject *self, char *s) {
char *target = unescape_path(fields[6], '|'); char *target = unescape_path(fields[6], '|');
if (target == NULL) { if (target == NULL) {
if (errno == EINVAL) { if (errno == EINVAL) {
field = TARGET; if (err)
*err = TARGET;
return -1; return -1;
} }
return -1; return -1;
@@ -305,12 +308,14 @@ int RecordObjectUnpack(struct RecordObject *self, char *s) {
self->target = target; self->target = target;
if (parse_uintmax(fields[7], &self->fingerprint) == -1) { if (parse_uintmax(fields[7], &self->fingerprint) == -1) {
field = FINGERPRINT; if (err)
*err = FINGERPRINT;
return -1; return -1;
} }
if (fields[8][0] == '\0') { if (fields[8][0] == '\0') {
field = PATH; if (err)
*err = PATH;
return -1; return -1;
} }
@@ -318,7 +323,8 @@ int RecordObjectUnpack(struct RecordObject *self, char *s) {
char *path = unescape_path(fields[8], '|'); char *path = unescape_path(fields[8], '|');
if (path == NULL) { if (path == NULL) {
if (errno == EINVAL) { if (errno == EINVAL) {
field = PATH; if (err)
*err = PATH;
return -1; return -1;
} }
return -1; return -1;
@@ -329,9 +335,9 @@ int RecordObjectUnpack(struct RecordObject *self, char *s) {
return 0; return 0;
} }
int RecordObjectCompare(const struct RecordObject *self, const struct RecordObject *other) { int RecordObjectCompare(const struct RecordObject *self,
field = NONE; const struct RecordObject *other,
enum Fields *err) {
if (self == NULL) if (self == NULL)
return 128; return 128;
if (other == NULL) if (other == NULL)
@@ -340,35 +346,44 @@ int RecordObjectCompare(const struct RecordObject *self, const struct RecordObje
int exit_code = 0; int exit_code = 0;
if (self->file_type != other->file_type) { if (self->file_type != other->file_type) {
exit_code = ((int)(self->file_type - other->file_type)); exit_code = ((int)(self->file_type - other->file_type));
field = FILE_TYPE; if (err)
*err = FILE_TYPE;
} else if (self->perm != other->perm) { } else if (self->perm != other->perm) {
exit_code = ((int)(self->perm - other->perm)); exit_code = ((int)(self->perm - other->perm));
field = PERMISSIONS; if (err)
*err = PERMISSIONS;
} else if (self->uid != other->uid) { } else if (self->uid != other->uid) {
exit_code = ((int)(self->uid - other->uid)); exit_code = ((int)(self->uid - other->uid));
field = UID; if (err)
*err = UID;
} else if (self->gid != other->gid) { } else if (self->gid != other->gid) {
exit_code = ((int)(self->gid - other->gid)); exit_code = ((int)(self->gid - other->gid));
field = GID; if (err)
*err = GID;
} else if (self->size != other->size) { } else if (self->size != other->size) {
exit_code = ((int)(self->size - other->size)); exit_code = ((int)(self->size - other->size));
field = SIZE; if (err)
*err = SIZE;
} else if (self->time != other->time) { } else if (self->time != other->time) {
exit_code = ((int)(self->time - other->time)); exit_code = ((int)(self->time - other->time));
field = TIME; if (err)
*err = TIME;
} else if (self->fingerprint != other->fingerprint) { } else if (self->fingerprint != other->fingerprint) {
exit_code = ((int)(self->fingerprint - other->fingerprint)); exit_code = ((int)(self->fingerprint - other->fingerprint));
field = FINGERPRINT; if (err)
*err = FINGERPRINT;
} else { } else {
int retp = strcmp(self->path, other->path); int retp = strcmp(self->path, other->path);
if (retp != 0) { if (retp != 0) {
exit_code = retp; exit_code = retp;
field = PATH; if (err)
*err = PATH;
} else { } else {
int rett = strcmp(self->target, other->target); int rett = strcmp(self->target, other->target);
if (rett != 0) { if (rett != 0) {
exit_code = rett; exit_code = rett;
field = TARGET; if (err)
*err = TARGET;
} }
} }
} }
@@ -377,10 +392,14 @@ int RecordObjectCompare(const struct RecordObject *self, const struct RecordObje
} }
int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) { int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
int rc = -1;
struct stat st; struct stat st;
if (lstat(path, &st) == -1) { char *s = NULL;
return -1; char *target = NULL;
} FILE *f = NULL;
if (lstat(path, &st) == -1)
goto cleanup;
self->file_type = file_type(st.st_mode); self->file_type = file_type(st.st_mode);
self->perm = (unsigned int)(st.st_mode & 07777); self->perm = (unsigned int)(st.st_mode & 07777);
@@ -388,14 +407,14 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
self->gid = (uintmax_t)st.st_gid; self->gid = (uintmax_t)st.st_gid;
self->size = (intmax_t)st.st_size; self->size = (intmax_t)st.st_size;
self->time = (intmax_t)st.st_mtime; self->time = (intmax_t)st.st_mtime;
char *s = sanitize_path(path); s = sanitize_path(path);
if (s == NULL) if (s == NULL)
return -1; goto cleanup;
if (self->path != NULL) if (self->path != NULL)
free(self->path); free(self->path);
self->path = s; self->path = s;
s = NULL;
char *target = NULL;
if (self->file_type == 'L') { if (self->file_type == 'L') {
size_t bufsiz = ((size_t)(st.st_size + 1)); size_t bufsiz = ((size_t)(st.st_size + 1));
@@ -404,33 +423,43 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
target = calloc(bufsiz, sizeof(char)); target = calloc(bufsiz, sizeof(char));
if (target == NULL) if (target == NULL)
return -1; goto cleanup;
if (readlink(path, target, bufsiz) == -1) { if (readlink(path, target, bufsiz) == -1)
free(target); goto cleanup;
return -1;
}
} }
if (self->target != NULL) if (self->target != NULL)
free(self->target); free(self->target);
self->target = target; self->target = target;
target = NULL;
FILE *f = NULL;
uint32_t hash = 0; uint32_t hash = 0;
if (!S_ISDIR(st.st_mode)) { if (!S_ISDIR(st.st_mode)) {
if ((f = fopen(path, "r")) == NULL) if ((f = fopen(path, "r")) == NULL)
return -1; goto cleanup;
hash = crc32(f); hash = crc32(f);
if (fclose(f) != 0) if (ferror(f) || fclose(f) != 0) {
return -1; f = NULL;
goto cleanup;
}
f = NULL;
} }
self->fingerprint = hash; self->fingerprint = hash;
return fprintf(out, "%c|%04o|%ju|%ju|%jd|%jd|%s|%ju|%s\n", self->file_type, rc = fprintf(out, "%c|%04o|%ju|%ju|%jd|%jd|%s|%ju|%s\n", self->file_type,
self->perm, self->uid, self->gid, self->size, self->time, self->perm, self->uid, self->gid, self->size, self->time,
self->target == NULL ? "" : self->target, self->fingerprint, self->target == NULL ? "" : self->target, self->fingerprint,
self->path); self->path);
cleanup:
if (s != NULL)
free(s);
if (target != NULL)
free(target);
if (f != NULL)
fclose(f);
return rc;
} }
int RecordObjectRelease(struct RecordObject *self) { int RecordObjectRelease(struct RecordObject *self) {

View File

@@ -48,40 +48,39 @@ char *resolve_path(const char *path) {
} }
int copy(const char *src, const char *dst) { int copy(const char *src, const char *dst) {
FILE *file1 = fopen(src, "r"); int rc = -1;
if (file1 == NULL) { FILE *file1 = NULL;
return -1; FILE *file2 = NULL;
}
FILE *file2 = fopen(dst, "w+"); file1 = fopen(src, "r");
if (file2 == NULL) { if (file1 == NULL)
fclose(file1); goto cleanup;
return -1;
} file2 = fopen(dst, "w+");
if (file2 == NULL)
goto cleanup;
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
size_t bytes; size_t bytes;
while ((bytes = fread(buffer, 1, sizeof(buffer), file1)) > 0) { while ((bytes = fread(buffer, 1, sizeof(buffer), file1)) > 0) {
if (fwrite(buffer, 1, bytes, file2) != bytes) { if (fwrite(buffer, 1, bytes, file2) != bytes)
fclose(file1); goto cleanup;
fclose(file2);
return -1;
}
} }
if (ferror(file1)) { if (ferror(file1))
fclose(file1); goto cleanup;
fclose(file2);
return -1;
}
int rc = 0; rc = 0;
if (fclose(file1) != 0)
cleanup:;
const int saved_errno = errno;
if (file1 != NULL && fclose(file1) != 0 && rc == 0)
rc = -1; rc = -1;
if (fclose(file2) != 0) if (file2 != NULL && fclose(file2) != 0 && rc == 0)
rc = -1; rc = -1;
if (rc == -1)
errno = saved_errno;
return rc; return rc;
} }
@@ -98,7 +97,6 @@ int count_lines(const char *file) {
count++; count++;
if (ferror(f)) { if (ferror(f)) {
/* Capture before the cleanup: free and fclose may both touch errno. */
const int saved_errno = errno; const int saved_errno = errno;
free(buf); free(buf);
fclose(f); fclose(f);
@@ -107,8 +105,7 @@ int count_lines(const char *file) {
} }
free(buf); free(buf);
if (fclose(f) != 0) { if (fclose(f) != 0)
return -1; return -1;
}
return count; return count;
} }

View File

@@ -96,7 +96,9 @@ int walk(const char *directory, WalkOp op, void *context) {
out: out:
free(path); free(path);
const int saved_errno = errno;
if (closedir(dirp) == -1) if (closedir(dirp) == -1)
rc = -1; rc = -1;
errno = saved_errno;
return rc; return rc;
} }