Added lots of extra compiler checks and fixed a few minor issues.

This commit is contained in:
Bruce Hill 2021-05-31 13:25:11 -07:00
parent 60db11fbb8
commit 67b62c7294
4 changed files with 30 additions and 14 deletions

View File

@ -4,7 +4,19 @@ CC=cc
G= G=
O=-O2 O=-O2
CFLAGS=-std=c99 -Werror -D_XOPEN_SOURCE=700 -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L CFLAGS=-std=c99 -Werror -D_XOPEN_SOURCE=700 -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L
CWARN=-Wall -Wpedantic -Wextra -Wsign-conversion -Wtype-limits -Wunused-result -Wnull-dereference CWARN=-Wall -Wpedantic -Wextra \
-Wsign-conversion -Wtype-limits -Wunused-result -Wnull-dereference \
-Waggregate-return -Walloc-zero -Walloca -Warith-conversion -Wcast-align -Wcast-align=strict \
-Wdangling-else -Wdate-time -Wdisabled-optimization -Wdouble-promotion -Wduplicated-branches \
-Wduplicated-cond -Wexpansion-to-defined -Wfloat-conversion -Wfloat-equal -Wformat-nonliteral \
-Wformat-security -Wformat-signedness -Wframe-address -Winline -Winvalid-pch -Wjump-misses-init \
-Wlogical-op -Wlong-long -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn \
-Wnull-dereference -Woverlength-strings -Wpacked -Wpacked-not-aligned -Wpointer-arith \
-Wredundant-decls -Wshadow -Wshadow=compatible-local -Wshadow=global -Wshadow=local \
-Wsign-conversion -Wstack-protector -Wsuggest-attribute=const -Wswitch-default -Wswitch-enum \
-Wsync-nand -Wtrampolines -Wundef -Wunused -Wunused-but-set-variable \
-Wunused-const-variable -Wunused-local-typedefs -Wunused-macros -Wvariadic-macros -Wvector-operation-performance \
-Wvla -Wwrite-strings
#CFLAGS += -fsanitize=address -fno-omit-frame-pointer #CFLAGS += -fsanitize=address -fno-omit-frame-pointer
CFLAGS += '-DBB_NAME="$(NAME)"' CFLAGS += '-DBB_NAME="$(NAME)"'

18
bb.c
View File

@ -45,12 +45,13 @@ static int compare_files(void *v, const void *v1, const void *v2);
#else #else
static int compare_files(const void *v1, const void *v2, void *v); static int compare_files(const void *v1, const void *v2, void *v);
#endif #endif
__attribute__((format(printf,2,3)))
void flash_warn(bb_t *bb, const char *fmt, ...); void flash_warn(bb_t *bb, const char *fmt, ...);
static void handle_next_key_binding(bb_t *bb); static void handle_next_key_binding(bb_t *bb);
static void init_term(void); static void init_term(void);
static int is_simple_bbcmd(const char *s); static int is_simple_bbcmd(const char *s);
static entry_t* load_entry(bb_t *bb, const char *path); static entry_t* load_entry(bb_t *bb, const char *path);
static inline int matches_cmd(const char *str, const char *cmd); static int matches_cmd(const char *str, const char *cmd);
static void* memcheck(void *p); static void* memcheck(void *p);
static char* normalize_path(const char *root, const char *path, char *pbuf); static char* normalize_path(const char *root, const char *path, char *pbuf);
static int populate_files(bb_t *bb, const char *path); static int populate_files(bb_t *bb, const char *path);
@ -240,6 +241,7 @@ static int compare_files(const void *v1, const void *v2, void *v)
case COL_CTIME: COMPARE_TIME(ctime(e1->info), ctime(e2->info)); break; case COL_CTIME: COMPARE_TIME(ctime(e1->info), ctime(e2->info)); break;
case COL_ATIME: COMPARE_TIME(atime(e1->info), atime(e2->info)); break; case COL_ATIME: COMPARE_TIME(atime(e1->info), atime(e2->info)); break;
case COL_RANDOM: COMPARE(e2->shufflepos, e1->shufflepos); break; case COL_RANDOM: COMPARE(e2->shufflepos, e1->shufflepos); break;
default: break;
} }
} }
return 0; return 0;
@ -418,7 +420,7 @@ static entry_t* load_entry(bb_t *bb, const char *path)
// Return whether a string matches a command // Return whether a string matches a command
// e.g. matches_cmd("sel:x", "select:") == 1, matches_cmd("q", "quit") == 1 // e.g. matches_cmd("sel:x", "select:") == 1, matches_cmd("q", "quit") == 1
// //
static inline int matches_cmd(const char *str, const char *cmd) static int matches_cmd(const char *str, const char *cmd)
{ {
if ((strchr(cmd, ':') == NULL) != (strchr(str, ':') == NULL)) if ((strchr(cmd, ':') == NULL) != (strchr(str, ':') == NULL))
return 0; return 0;
@ -642,7 +644,7 @@ static void run_bbcmd(bb_t *bb, const char *cmd)
char *description; char *description;
if (script[0] == '#') { if (script[0] == '#') {
description = trim(strsep(&script, "\n") + 1); description = trim(strsep(&script, "\n") + 1);
if (!script) script = ""; if (!script) script = (char*)"";
else script = trim(script); else script = trim(script);
} else description = script; } else description = script;
for (char *key; (key = strsep(&keys, ",")); ) { for (char *key; (key = strsep(&keys, ",")); ) {
@ -834,7 +836,7 @@ static int run_script(bb_t *bb, const char *cmd)
(void)setpgid(0, pgrp); (void)setpgid(0, pgrp);
if (tcsetpgrp(STDIN_FILENO, pgrp)) if (tcsetpgrp(STDIN_FILENO, pgrp))
clean_err("Couldn't set pgrp"); clean_err("Couldn't set pgrp");
char **args = xcalloc(4 + (size_t)bb->nselected + 1, sizeof(char*)); const char **args = xcalloc(4 + (size_t)bb->nselected + 1, sizeof(char*));
int i = 0; int i = 0;
args[i++] = "sh"; args[i++] = "sh";
args[i++] = "-c"; args[i++] = "-c";
@ -850,7 +852,7 @@ static int run_script(bb_t *bb, const char *cmd)
dup2(fileno(tty_out), STDOUT_FILENO); dup2(fileno(tty_out), STDOUT_FILENO);
dup2(fileno(tty_in), STDIN_FILENO); dup2(fileno(tty_in), STDIN_FILENO);
execvp(args[0], args); execvp(args[0], (char**)args);
clean_err("Failed to execute command: '%s'", cmd); clean_err("Failed to execute command: '%s'", cmd);
return -1; return -1;
} }
@ -970,8 +972,8 @@ static void set_selected(bb_t *bb, entry_t *e, int selected)
// //
static void set_sort(bb_t *bb, const char *sort) static void set_sort(bb_t *bb, const char *sort)
{ {
char sortbuf[strlen(sort)+1]; char sortbuf[MAX_SORT+1];
strcpy(sortbuf, sort); strncpy(sortbuf, sort, MAX_SORT);
for (char *s = sortbuf; s[0] && s[1]; s += 2) { for (char *s = sortbuf; s[0] && s[1]; s += 2) {
char *found; char *found;
if ((found = strchr(bb->sort, s[1]))) { if ((found = strchr(bb->sort, s[1]))) {
@ -1074,7 +1076,7 @@ static int wait_for_process(proc_t **proc)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *initial_path; const char *initial_path;
if (argc >= 3 && streq(argv[argc-2], "--")) { if (argc >= 3 && streq(argv[argc-2], "--")) {
initial_path = argv[argc-1]; initial_path = argv[argc-1];
argc -= 2; argc -= 2;

4
draw.c
View File

@ -46,7 +46,7 @@ static char* stpcpy_escaped(char *buf, const char *str, const char *color)
if (*c > 0 && *c <= '\x1b' && escapes[(int)*c] != ' ') { // "\n", etc. if (*c > 0 && *c <= '\x1b' && escapes[(int)*c] != ' ') { // "\n", etc.
buf += sprintf(buf, "\033[31m\\%c%s", escapes[(int)*c], color); buf += sprintf(buf, "\033[31m\\%c%s", escapes[(int)*c], color);
} else if (*c >= 0 && !(' ' <= *c && *c <= '~')) { // "\x02", etc. } else if (*c >= 0 && !(' ' <= *c && *c <= '~')) { // "\x02", etc.
buf += sprintf(buf, "\033[31m\\x%02X%s", *c, color); buf += sprintf(buf, "\033[31m\\x%02X%s", (unsigned int)*c, color);
} else { } else {
*(buf++) = *c; *(buf++) = *c;
} }
@ -69,7 +69,7 @@ static int fputs_escaped(FILE *f, const char *str, const char *color)
fprintf(f, "\033[31m\\%c%s", escapes[(int)*c], color); fprintf(f, "\033[31m\\%c%s", escapes[(int)*c], color);
++escaped; ++escaped;
} else if (*c >= 0 && !(' ' <= *c && *c <= '~')) { // "\x02", etc. } else if (*c >= 0 && !(' ' <= *c && *c <= '~')) { // "\x02", etc.
fprintf(f, "\033[31m\\x%02X%s", *c, color); fprintf(f, "\033[31m\\x%02X%s", (unsigned int)*c, color);
++escaped; ++escaped;
} else { } else {
fputc(*c, f); fputc(*c, f);

View File

@ -58,13 +58,13 @@ static keyname_t key_names[] = {
{':', "Colon"}, {',', "Comma"}, {':', "Colon"}, {',', "Comma"},
}; };
static inline int nextchar(int fd) static int nextchar(int fd)
{ {
char c; char c;
return read(fd, &c, 1) == 1 ? c : -1; return read(fd, &c, 1) == 1 ? c : -1;
} }
static inline int nextnum(int fd, int c, int *n) static int nextnum(int fd, int c, int *n)
{ {
for (*n = 0; '0' <= c && c <= '9'; c = nextchar(fd)) for (*n = 0; '0' <= c && c <= '9'; c = nextchar(fd))
*n = 10*(*n) + (c - '0'); *n = 10*(*n) + (c - '0');
@ -143,6 +143,7 @@ int bgetkey(FILE *in, int *mouse_x, int *mouse_y)
case 21: return modifiers | KEY_F10; case 21: return modifiers | KEY_F10;
case 23: return modifiers | KEY_F11; case 23: return modifiers | KEY_F11;
case 24: return modifiers | KEY_F12; case 24: return modifiers | KEY_F12;
default: break;
} }
return -1; return -1;
case '<': { // Mouse clicks case '<': { // Mouse clicks
@ -185,6 +186,7 @@ int bgetkey(FILE *in, int *mouse_x, int *mouse_y)
case MOUSE_LEFT_RELEASE: key = MOUSE_LEFT_DOUBLE; break; case MOUSE_LEFT_RELEASE: key = MOUSE_LEFT_DOUBLE; break;
case MOUSE_RIGHT_RELEASE: key = MOUSE_RIGHT_DOUBLE; break; case MOUSE_RIGHT_RELEASE: key = MOUSE_RIGHT_DOUBLE; break;
case MOUSE_MIDDLE_RELEASE: key = MOUSE_MIDDLE_DOUBLE; break; case MOUSE_MIDDLE_RELEASE: key = MOUSE_MIDDLE_DOUBLE; break;
default: break;
} }
} }
} }
@ -238,7 +240,7 @@ char *bkeyname(int key, char *buf)
if (' ' < key && key <= '~') if (' ' < key && key <= '~')
return buf + sprintf(buf, "%c", key); return buf + sprintf(buf, "%c", key);
else else
return buf + sprintf(buf, "\\x%02X", key); return buf + sprintf(buf, "\\x%02X", (unsigned int)key);
} }
// //