diff --git a/include/types.h b/include/types.h index f922a6d..9185156 100644 --- a/include/types.h +++ b/include/types.h @@ -57,7 +57,7 @@ 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); +/* Free self and all owned resources. Does nothing if self is NULL. */ +void RecordObjectRelease(struct RecordObject *self); #endif diff --git a/src/create.c b/src/create.c index 1826d2d..3a4281d 100644 --- a/src/create.c +++ b/src/create.c @@ -3,7 +3,6 @@ #include "utility.h" #include "walk.h" #include -#include #include #include #include @@ -27,11 +26,11 @@ static int write_record(const char *path, void *context) { } int create(const char *directory, const char *output_file) { - if (directory == NULL || strlen(directory) == 0) { + if (directory == NULL || directory[0] == '\0') { errno = EINVAL; return -1; } - if (output_file == NULL || strlen(output_file) == 0) { + if (output_file == NULL || output_file[0] == '\0') { errno = EINVAL; return -1; } diff --git a/src/list.c b/src/list.c index 546f727..bbea65a 100644 --- a/src/list.c +++ b/src/list.c @@ -2,10 +2,8 @@ #include "types.h" #include "utility.h" #include -#include #include #include -#include int list(const char *snapshot) { FILE *snapshot_file = fopen(snapshot, "r"); diff --git a/src/scan.c b/src/scan.c index a1fc82d..48fd89f 100644 --- a/src/scan.c +++ b/src/scan.c @@ -4,7 +4,6 @@ #include #include #include -#include static int print_file(const char *file, void *context) { (void)context; @@ -13,7 +12,7 @@ static int print_file(const char *file, void *context) { /* Scan recursively directory, printing all entry */ int scan(const char *directory) { - if (directory == NULL || strlen(directory) == 0) { + if (directory == NULL || directory[0] == '\0') { errno = EINVAL; return -1; } diff --git a/src/types.c b/src/types.c index a043d05..69df78b 100644 --- a/src/types.c +++ b/src/types.c @@ -16,7 +16,9 @@ static const char *field_names[] = { "time", "target", "fingerprint", "path", "none"}; const char *RecordObjectFieldName(enum Fields f) { - return (f <= NONE) ? field_names[f] : "unknown"; + /* The underlying type of an enum is the compiler's choice, so guard the + low side too: as unsigned, a negative value wraps past NONE. */ + return ((unsigned)f <= NONE) ? field_names[f] : "unknown"; } /*======================= static functions ===================================*/ @@ -38,7 +40,7 @@ static int split_record(char *line, char *fields[NFIELDS], char delim) { } /* Only the path may contain an escaped delimiter, and it is the last field, - so the six separators found above are always the real ones. Validating + so the separators found above are always the real ones. Validating what is left is the unescaper's job. */ fields[NFIELDS - 1] = p; @@ -411,8 +413,7 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) { s = sanitize_path(path); if (s == NULL) goto cleanup; - if (self->path != NULL) - free(self->path); + free(self->path); self->path = s; s = NULL; @@ -448,8 +449,7 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) { } target[n] = '\0'; } - if (self->target != NULL) - free(self->target); + free(self->target); self->target = target; target = NULL; @@ -474,28 +474,19 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) { self->path); cleanup: - if (s != NULL) - free(s); - if (target != NULL) - free(target); + free(s); + free(target); if (f != NULL) fclose(f); return rc; } -int RecordObjectRelease(struct RecordObject *self) { +void RecordObjectRelease(struct RecordObject *self) { if (self == NULL) - return -1; - if (self->path != NULL) { - free(self->path); - self->path = NULL; - } - if (self->target != NULL) { - free(self->target); - self->target = NULL; - } + return; + free(self->path); + free(self->target); free(self); - return 0; } /* Accessors */ diff --git a/src/utility.c b/src/utility.c index 4c1397d..990db36 100644 --- a/src/utility.c +++ b/src/utility.c @@ -10,8 +10,8 @@ char *strconcat(const char *src, int n, ...) { va_list ap; - /* First pass: total length. strcat never reallocates, so the buffer has to - be sized up front. */ + /* First pass: total length. The result is one allocation, so it has to be + sized before anything is copied into it. */ size_t len = strlen(src); va_start(ap, n); for (int i = 0; i < n; i++) { @@ -55,7 +55,7 @@ int copy(const char *src, const char *dst) { if (file1 == NULL) goto cleanup; - file2 = fopen(dst, "w+"); + file2 = fopen(dst, "w"); if (file2 == NULL) goto cleanup;