mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 03:09:51 +00:00
No behavioural change; verified byte-identical scan output over /usr/include and /etc and byte-identical snapshots against the previous build, with valgrind clean on every command. - free() accepts NULL, so drop six guards around it in types.c. - RecordObjectRelease returned an int no caller ever read; make it void, which is the idiom for a free-like function. - RecordObjectFieldName bounded its index only from above. The underlying type of an enum is the compiler's choice, so compare as unsigned and catch a negative value too. - copy() opened the destination "w+" but never reads it back; "w" says what it does. - Testing strlen(s) == 0 walks the whole string to answer a question about its first byte. - Two comments contradicted the code: split_record finds eight separators, not six, and strconcat explains itself in terms of a strcat it does not call. - Drop includes nothing uses: <inttypes.h> from list.c and create.c, and <string.h> from list.c and from scan.c, which stopped needing it once the strlen above was gone. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
97 lines
2.1 KiB
C
97 lines
2.1 KiB
C
#include "list.h"
|
|
#include "types.h"
|
|
#include "utility.h"
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int list(const char *snapshot) {
|
|
FILE *snapshot_file = fopen(snapshot, "r");
|
|
if (snapshot_file == NULL) {
|
|
return -1;
|
|
}
|
|
|
|
int rc = 0;
|
|
struct RecordObject **records = NULL;
|
|
char *buf = NULL;
|
|
const int arr_size = count_lines(snapshot);
|
|
if (arr_size == 0) {
|
|
goto out;
|
|
} else if (arr_size < 0) {
|
|
rc = -1;
|
|
goto out;
|
|
}
|
|
|
|
int i = 0;
|
|
size_t size = 0;
|
|
records = calloc((size_t)arr_size, sizeof(*records));
|
|
if (records == NULL) {
|
|
errno = ENOMEM;
|
|
rc = -1;
|
|
goto out;
|
|
}
|
|
while (getline(&buf, &size, snapshot_file) != -1 && i < arr_size) {
|
|
struct RecordObject *objp = RecordObjectNew();
|
|
if (objp == NULL) {
|
|
rc = -1;
|
|
goto out;
|
|
}
|
|
enum Fields err = NONE;
|
|
errno = 0;
|
|
int ret = RecordObjectUnpack(objp, buf, &err);
|
|
if (ret == -1) {
|
|
/* A failed allocation inside the parser is not a malformed record and
|
|
must not be reported as one; errno tells them apart. */
|
|
if (errno != EINVAL) {
|
|
rc = -1;
|
|
RecordObjectRelease(objp);
|
|
goto out;
|
|
}
|
|
|
|
if (err != NONE)
|
|
fprintf(stderr, "%s:%d: invalid %s\n", snapshot, i + 1,
|
|
RecordObjectFieldName(err));
|
|
else
|
|
fprintf(stderr, "%s:%d: unparsable line\n", snapshot, i + 1);
|
|
rc = FSNAP_EPARSE;
|
|
RecordObjectRelease(objp);
|
|
break;
|
|
}
|
|
|
|
records[i] = objp;
|
|
i++;
|
|
}
|
|
|
|
if (ferror(snapshot_file)) {
|
|
rc = -1;
|
|
goto out;
|
|
}
|
|
|
|
out:;
|
|
/* An error that happened before the cleanup owns errno; one raised by the
|
|
cleanup itself owns it only when nothing had failed yet. */
|
|
const int saved_errno = errno;
|
|
int cleanup_errno = 0;
|
|
|
|
if (snapshot_file != NULL && fclose(snapshot_file) != 0)
|
|
cleanup_errno = errno;
|
|
|
|
free(buf);
|
|
if (records != NULL) {
|
|
for (int j = 0; j < arr_size; j++) {
|
|
RecordObjectRelease(records[j]);
|
|
records[j] = NULL;
|
|
}
|
|
free(records);
|
|
records = NULL;
|
|
}
|
|
|
|
if (rc == 0 && cleanup_errno != 0) {
|
|
errno = cleanup_errno;
|
|
return -1;
|
|
}
|
|
if (rc == -1)
|
|
errno = saved_errno;
|
|
return rc;
|
|
}
|