From 3ededef53d0704f8b89c55f60bef52a64b86d62c Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 18 Jan 2021 09:15:25 -0800 Subject: Static analysis part 1 --- Makefile | 5 ++++- bp.c | 8 ++++---- files.c | 4 +++- files.h | 2 -- match.c | 7 +++++-- match.h | 3 --- pattern.c | 4 +++- pattern.h | 2 -- print.c | 2 +- 9 files changed, 20 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 1c58302..73eba4f 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,9 @@ leaktest: make G=-ggdb O=-O0 EXTRA=-DDEBUG_HEAP clean bp valgrind --leak-check=full ./bp -l -g grammars/bp.bp -p Grammar grammars/bp.bp +splint: + splint +posixlib $(CFILES) bp.c + install: $(NAME) mkdir -p -m 755 "$(PREFIX)/share/man/man1" "$(PREFIX)/bin" "$(SYSCONFDIR)/xdg/$(NAME)" cp -r grammars/* "$(SYSCONFDIR)/xdg/$(NAME)/" @@ -42,4 +45,4 @@ uninstall: [ "$$confirm" != n ] && rm -rf ~/.config/$(NAME); \ fi -.PHONY: all clean install uninstall +.PHONY: all clean install uninstall leaktest splint diff --git a/bp.c b/bp.c index f23e830..3ae703c 100644 --- a/bp.c +++ b/bp.c @@ -65,10 +65,10 @@ static enum { } mode = MODE_NORMAL; // If a filename is put here, it will be deleted if a signal is received -const char *in_use_tempfile = NULL; +static const char *in_use_tempfile = NULL; // Used for user input/output that doesn't interfere with unix pipeline -FILE *tty_out = NULL, *tty_in = NULL; +static FILE *tty_out = NULL, *tty_in = NULL; // // Helper function to reduce code duplication @@ -616,8 +616,8 @@ int main(int argc, char *argv[]) } if (mode == MODE_JSON) printf("]\n"); - if (tty_out) fclose(tty_out); - if (tty_in) fclose(tty_in); + if (tty_out) { fclose(tty_out); tty_out = NULL; } + if (tty_in) { fclose(tty_in); tty_in = NULL; } #ifdef DEBUG_HEAP // This code frees up all residual heap-allocated memory. Since the program diff --git a/files.c b/files.c index 145d1c1..0296a35 100644 --- a/files.c +++ b/files.c @@ -18,6 +18,8 @@ __attribute__((nonnull)) static void populate_lines(file_t *f); +__attribute__((pure, nonnull)) +static size_t get_char_number(file_t *f, const char *p); // // In the file object, populate the `lines` array with pointers to the @@ -185,7 +187,7 @@ size_t get_line_number(file_t *f, const char *p) // // Given a pointer, determine which character offset within the line it points to. // -size_t get_char_number(file_t *f, const char *p) +static size_t get_char_number(file_t *f, const char *p) { size_t linenum = get_line_number(f, p); return 1 + (size_t)(p - f->lines[linenum-1]); diff --git a/files.h b/files.h index c7176b8..b78e77e 100644 --- a/files.h +++ b/files.h @@ -30,8 +30,6 @@ void destroy_file(file_t **f); __attribute__((pure, nonnull)) size_t get_line_number(file_t *f, const char *p); __attribute__((pure, nonnull)) -size_t get_char_number(file_t *f, const char *p); -__attribute__((pure, nonnull)) const char *get_line(file_t *f, size_t line_number); __attribute__((nonnull(1,2,3), format(printf,5,6))) void fprint_line(FILE *dest, file_t *f, const char *start, const char *end, const char *fmt, ...); diff --git a/match.c b/match.c index 128e9da..63f44c6 100644 --- a/match.c +++ b/match.c @@ -33,6 +33,7 @@ static match_t *unused_matches = NULL; static match_t *in_use_matches = NULL; #endif +static match_t *new_match(void); __attribute__((nonnull, pure)) static inline const char *next_char(file_t *f, const char *str); __attribute__((nonnull)) @@ -41,6 +42,8 @@ __attribute__((nonnull)) static match_t *get_capture_by_num(match_t *m, int *n); __attribute__((nonnull, pure)) static match_t *get_capture_by_name(match_t *m, const char *name); +__attribute__((hot, nonnull(2,3,4))) +static match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, unsigned int flags); // // Return the location of the next character or UTF8 codepoint. @@ -144,7 +147,7 @@ match_t *next_match(def_t *defs, file_t *f, match_t *prev, pat_t *pat, unsigned // match object, or NULL if no match is found. // The returned value should be free()'d to avoid memory leaking. // -match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, unsigned int ignorecase) +static match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, unsigned int ignorecase) { switch (pat->type) { case BP_LEFTRECURSION: { @@ -574,7 +577,7 @@ match_t *get_capture(match_t *m, const char **id) // // Return a match object which can be used (may be allocated or recycled). // -match_t *new_match(void) +static match_t *new_match(void) { match_t *m; diff --git a/match.h b/match.h index 9530321..3386a70 100644 --- a/match.h +++ b/match.h @@ -10,11 +10,8 @@ __attribute__((nonnull(2,4))) match_t *next_match(def_t *defs, file_t *f, match_t *prev, pat_t *pat, unsigned int flags); -__attribute__((hot, nonnull(2,3,4))) -match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, unsigned int flags); __attribute__((nonnull)) match_t *get_capture(match_t *m, const char **id); -match_t *new_match(void); __attribute__((nonnull)) void recycle_if_unused(match_t **at_m); #ifdef DEBUG_HEAP diff --git a/pattern.c b/pattern.c index 692deb5..2ec30c0 100644 --- a/pattern.c +++ b/pattern.c @@ -21,6 +21,8 @@ __attribute__((nonnull)) static pat_t *_bp_simplepattern(file_t *f, const char *str); __attribute__((nonnull(1,2,3,6))) static pat_t *new_range(file_t *f, const char *start, const char *end, ssize_t min, ssize_t max, pat_t *repeating, pat_t *sep); +__attribute__((nonnull(1,2))) +static pat_t *bp_simplepattern(file_t *f, const char *str); // // Allocate a new pattern for this file (ensuring it will be automatically @@ -149,7 +151,7 @@ pat_t *chain_together(file_t *f, pat_t *first, pat_t *second) // // Wrapper for _bp_simplepattern() that expands any postfix operators // -pat_t *bp_simplepattern(file_t *f, const char *str) +static pat_t *bp_simplepattern(file_t *f, const char *str) { pat_t *pat = _bp_simplepattern(f, str); if (pat == NULL) return pat; diff --git a/pattern.h b/pattern.h index c237b50..ff1105c 100644 --- a/pattern.h +++ b/pattern.h @@ -10,8 +10,6 @@ __attribute__((nonnull)) pat_t *new_pat(file_t *f, const char *start, enum pattype_e type); __attribute__((nonnull(1,2))) -pat_t *bp_simplepattern(file_t *f, const char *str); -__attribute__((nonnull(1,2))) pat_t *bp_stringpattern(file_t *f, const char *str); __attribute__((nonnull(1,2))) pat_t *bp_replacement(file_t *f, pat_t *replacepat, const char *replacement); diff --git a/print.c b/print.c index 77fe654..27f2ccc 100644 --- a/print.c +++ b/print.c @@ -265,7 +265,7 @@ static const char *context_after(printer_t *pr, const char *pos) // // Print the text of a match (no context). // -void _print_match(FILE *out, printer_t *pr, match_t *m) +static void _print_match(FILE *out, printer_t *pr, match_t *m) { pr->pos = m->start; if (m->pat->type == BP_REPLACE) { -- cgit v1.2.3