From e98574570ec0e355c6cff2b6e30bd992f20fe610 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 18 Jan 2021 11:15:53 -0800 Subject: More static analysis cleanup --- Makefile | 5 ++++- bp.c | 9 +++++---- definitions.c | 4 ++-- match.c | 2 +- pattern.c | 8 ++++---- print.c | 8 ++++---- utils.c | 4 ++-- 7 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index dd12609..00ef21f 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,10 @@ leaktest: valgrind --leak-check=full ./bp -l -g grammars/bp.bp -p Grammar grammars/bp.bp splint: - splint -posix-lib -weak -initallelements $(CFILES) bp.c + splint -posix-lib -standard -mustfreefresh -mustfreeonly -temptrans -immediatetrans -branchstate \ + -compmempass -nullret -nullpass -nullderef -kepttrans -boolops -initallelements -fullinitblock \ + -compdef -usereleased -unrecog -dependenttrans -predboolothers -ownedtrans -unqualifiedtrans \ + -onlytrans $(CFILES) bp.c install: $(NAME) mkdir -p -m 755 "$(PREFIX)/share/man/man1" "$(PREFIX)/bin" "$(SYSCONFDIR)/xdg/$(NAME)" diff --git a/bp.c b/bp.c index 46b5c2b..ea6360c 100644 --- a/bp.c +++ b/bp.c @@ -3,6 +3,7 @@ // // See `man ./bp.1` for more details // + #include #include #include @@ -132,11 +133,11 @@ static int is_text_file(const char *filename) int fd = open(filename, O_RDONLY); if (fd < 0) return 0; unsigned char buf[64]; - int len = read(fd, buf, sizeof(buf)/sizeof(unsigned char)); + ssize_t len = read(fd, buf, sizeof(buf)/sizeof(unsigned char)); if (len < 0) return 0; (void)close(fd); - for (int i = 0; i < len; i++) { + for (ssize_t i = 0; i < len; i++) { if (!(buf[i] == '\t' || buf[i] == '\n' || buf[i] == '\r' || buf[i] >= '\x20')) return 0; @@ -281,7 +282,7 @@ static int inplace_modify_file(def_t *defs, file_t *f, pat_t *pattern) exit(EXIT_FAILURE); // Lazy-open file for writing upon first match: if (inplace_file == NULL) { - check(snprintf(tmp_filename, PATH_MAX, "%s.tmp.XXXXXX", f->filename) <= PATH_MAX, + check(snprintf(tmp_filename, PATH_MAX, "%s.tmp.XXXXXX", f->filename) <= (int)PATH_MAX, "Failed to build temporary file template"); int out_fd = mkstemp(tmp_filename); check(out_fd >= 0, "Failed to create temporary inplace file"); @@ -396,7 +397,7 @@ static int process_dir(def_t *defs, const char *dirname, pat_t *pattern) int matches = 0; glob_t globbuf; char globpath[PATH_MAX+1] = {0}; - check(snprintf(globpath, PATH_MAX, "%s/*", dirname) <= PATH_MAX, + check(snprintf(globpath, PATH_MAX, "%s/*", dirname) <= (int)PATH_MAX, "Filename is too long: %s/*", dirname); int status = glob(globpath, 0, NULL, &globbuf); check(status != GLOB_ABORTED && status != GLOB_NOSPACE, diff --git a/definitions.c b/definitions.c index 32399d7..ca22b2e 100644 --- a/definitions.c +++ b/definitions.c @@ -36,7 +36,7 @@ def_t *load_grammar(def_t *defs, file_t *f) src = after_name(name); if (src <= name) { fprint_line(stdout, f, name, src, "Invalid name for definition: %s", name); - exit(1); + exit(EXIT_FAILURE); } size_t namelen = (size_t)(src - name); check(matchchar(&src, ':'), "Expected ':' in definition"); @@ -50,7 +50,7 @@ def_t *load_grammar(def_t *defs, file_t *f) } if (src < f->end) { fprint_line(stderr, f, src, NULL, "Invalid BP pattern"); - exit(1); + exit(EXIT_FAILURE); } return defs; } diff --git a/match.c b/match.c index e2e00c2..a8451e6 100644 --- a/match.c +++ b/match.c @@ -518,7 +518,7 @@ static match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, bool } default: { fprintf(stderr, "Unknown pattern type: %d", pat->type); - exit(1); + exit(EXIT_FAILURE); return NULL; } } diff --git a/pattern.c b/pattern.c index cd154b4..cf5b095 100644 --- a/pattern.c +++ b/pattern.c @@ -3,6 +3,7 @@ // #include +#include #include #include #include @@ -11,7 +12,7 @@ #include "pattern.h" #include "utils.h" -#define file_err(f, ...) do { fprint_line(stderr, f, __VA_ARGS__); exit(1); } while(0) +#define file_err(f, ...) do { fprint_line(stderr, f, __VA_ARGS__); exit(EXIT_FAILURE); } while(0) __attribute__((nonnull)) static pat_t *expand_chain(file_t *f, pat_t *first); @@ -161,7 +162,7 @@ static pat_t *bp_simplepattern(file_t *f, const char *str) // Expand postfix operators (if any) str = after_spaces(pat->end); while (str+2 < f->end && (matchstr(&str, "!=") || matchstr(&str, "=="))) { // Equality == and inequality != - int equal = str[-2] == '='; + bool equal = str[-2] == '='; pat_t *first = pat; pat_t *second = bp_simplepattern(f, str); if (!second) @@ -224,7 +225,7 @@ static pat_t *_bp_simplepattern(file_t *f, const char *str) case '`': { pat_t *all = NULL; do { - char c = *str; + c = *str; if (!c || c == '\n') file_err(f, str, str, "There should be a character here after the '`'"); const char *opstart = str-1; @@ -490,7 +491,6 @@ static pat_t *_bp_simplepattern(file_t *f, const char *str) return ref; } } - return NULL; } // diff --git a/print.c b/print.c index a683f2f..c5610a9 100644 --- a/print.c +++ b/print.c @@ -68,9 +68,9 @@ static void _visualize_matches(match_node_t *firstmatch, int depth, const char * // literal string being matched. (Backrefs have start/end inside the text // input, instead of something the user typed in) if (viz_type >= text && viz_type <= &text[textlen]) - printf("\033[%ldG\033[0;2m\"\033[%s;1m", 2*textlen+3, color); + printf("\033[%luG\033[0;2m\"\033[%s;1m", 2*textlen+3, color); else - printf("\033[%ldG\033[%s;1m", 2*textlen+3, color); + printf("\033[%luG\033[%s;1m", 2*textlen+3, color); for (size_t i = 0; i < viz_typelen; i++) { switch (viz_type[i]) { @@ -192,8 +192,8 @@ static inline void print_line_number(FILE *out, printer_t *pr, size_t line_numbe if (color) fprintf(out, "\033[0;2m \033(0\x78\033(B%s", color); else fprintf(out, " |"); } else { - if (color) fprintf(out, "\033[0;2m% 5ld\033(0\x78\033(B%s", line_number, color); - else fprintf(out, "% 5ld|", line_number); + if (color) fprintf(out, "\033[0;2m%5lu\033(0\x78\033(B%s", line_number, color); + else fprintf(out, "%5lu|", line_number); } pr->needs_line_number = 0; current_color = color; diff --git a/utils.c b/utils.c index 58f8e2d..93de1f4 100644 --- a/utils.c +++ b/utils.c @@ -145,7 +145,7 @@ void *memcheck(void *p) { if (p == NULL) { fprintf(stderr, "memory allocation failure\n"); - exit(1); + exit(EXIT_FAILURE); } return p; } @@ -169,7 +169,7 @@ void xfree(void *p) { if (*(void**)p == NULL) { fprintf(stderr, "Attempt to free(NULL)\n"); - exit(1); + exit(EXIT_FAILURE); } free(*(void**)p); p = NULL; -- cgit v1.2.3