mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 19:29:50 +00:00
Build with -O2 and a stricter warning set, and fix what it found
Without optimisation GCC skips the flow analysis most of these warnings are derived from, so -O2 is what makes the existing -W flags do their job. The new flags found three real defects: -Wstrict-prototypes/-Wold-style-definition: init_crc32_table() and RecordObjectNew() were declared with (), which in C11 means "no prototype", not "no arguments". RecordObjectNew even contradicted its own header, which declares (void). -Wwrite-strings: walk's blacklist held string literals in a non-const char *[], so they were writable through the array. Also make ignore_file size its loop with sizeof(blacklist[0]) instead of repeating the element type, and drop the order-only build/ prerequisite on the link rule, which the %.o rule's mkdir -p already covers. The tree builds clean under all twelve flags. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
This commit is contained in:
13
Makefile
13
Makefile
@@ -1,7 +1,13 @@
|
|||||||
CC := cc
|
CC := cc
|
||||||
|
|
||||||
CPPFLAGS := -Iinclude -MMD -MP
|
CPPFLAGS := -Iinclude -MMD -MP
|
||||||
CFLAGS := -std=c11 -Wall -Wextra -Wpedantic -Wconversion -ggdb
|
|
||||||
|
# -O2 is not only about speed: without optimisation GCC skips the flow
|
||||||
|
# analysis that most of these warnings are derived from.
|
||||||
|
WARNINGS := -Wall -Wextra -Wpedantic -Wconversion -Wshadow -Wvla \
|
||||||
|
-Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition \
|
||||||
|
-Wcast-qual -Wwrite-strings -Wdouble-promotion
|
||||||
|
CFLAGS := -std=c11 $(WARNINGS) -O2 -ggdb
|
||||||
LDFLAGS :=
|
LDFLAGS :=
|
||||||
LDLIBS :=
|
LDLIBS :=
|
||||||
|
|
||||||
@@ -26,16 +32,13 @@ DEPENDENCIES := $(OBJECTS:.o=.d)
|
|||||||
|
|
||||||
all: $(TARGET)
|
all: $(TARGET)
|
||||||
|
|
||||||
$(TARGET): $(OBJECTS) | $(BUILD_DIR)
|
$(TARGET): $(OBJECTS)
|
||||||
$(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $@
|
$(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $@
|
||||||
|
|
||||||
$(BUILD_DIR)/%.o: %.c
|
$(BUILD_DIR)/%.o: %.c
|
||||||
@mkdir -p $(dir $@)
|
@mkdir -p $(dir $@)
|
||||||
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
|
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
$(BUILD_DIR):
|
|
||||||
mkdir -p $@
|
|
||||||
|
|
||||||
run: $(TARGET)
|
run: $(TARGET)
|
||||||
./$(TARGET)
|
./$(TARGET)
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
static uint32_t crc32_table[256];
|
static uint32_t crc32_table[256];
|
||||||
static int table_computed = 0;
|
static int table_computed = 0;
|
||||||
|
|
||||||
static void init_crc32_table() {
|
static void init_crc32_table(void) {
|
||||||
for (uint32_t i = 0; i < 256; i++) {
|
for (uint32_t i = 0; i < 256; i++) {
|
||||||
uint32_t crc = i;
|
uint32_t crc = i;
|
||||||
for (int j = 0; j < 8; j++) {
|
for (int j = 0; j < 8; j++) {
|
||||||
|
|||||||
@@ -254,7 +254,7 @@ struct RecordObject {
|
|||||||
uintmax_t fingerprint;
|
uintmax_t fingerprint;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct RecordObject *RecordObjectNew() {
|
struct RecordObject *RecordObjectNew(void) {
|
||||||
struct RecordObject *ret = calloc(1, sizeof(*ret));
|
struct RecordObject *ret = calloc(1, sizeof(*ret));
|
||||||
if (ret == NULL)
|
if (ret == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -8,10 +8,10 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
static char *blacklist[] = {".", ".."};
|
static const char *const blacklist[] = {".", ".."};
|
||||||
|
|
||||||
static int ignore_file(const char *name) {
|
static int ignore_file(const char *name) {
|
||||||
for (long unsigned int i = 0; i < sizeof(blacklist) / sizeof(char *); i++) {
|
for (size_t i = 0; i < sizeof(blacklist) / sizeof(blacklist[0]); i++) {
|
||||||
if (strcmp(name, blacklist[i]) == 0)
|
if (strcmp(name, blacklist[i]) == 0)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user