More static analysis cleanup

This commit is contained in:
Bruce Hill 2021-01-18 11:15:53 -08:00
parent 46f1747947
commit e98574570e
7 changed files with 22 additions and 18 deletions

View File

@ -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)"

9
bp.c
View File

@ -3,6 +3,7 @@
//
// See `man ./bp.1` for more details
//
#include <errno.h>
#include <fcntl.h>
#include <glob.h>
@ -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,

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -3,6 +3,7 @@
//
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@ -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 <pat1>==<pat2> and inequality <pat1>!=<pat2>
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;
}
//

View File

@ -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;

View File

@ -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;