Checking more return values (per static analyzer)
This commit is contained in:
parent
2622d44dc4
commit
7f0c3804dc
56
bp.c
56
bp.c
@ -184,7 +184,7 @@ static int explain_matches(def_t *defs, file_t *f, pat_t *pattern)
|
||||
static void cleanup(void)
|
||||
{
|
||||
if (in_use_tempfile) {
|
||||
remove(in_use_tempfile);
|
||||
(void)remove(in_use_tempfile);
|
||||
in_use_tempfile = NULL;
|
||||
}
|
||||
}
|
||||
@ -195,7 +195,7 @@ static void cleanup(void)
|
||||
static void sig_handler(int sig)
|
||||
{
|
||||
cleanup();
|
||||
kill(0, sig);
|
||||
if (kill(0, sig)) _exit(1);
|
||||
}
|
||||
|
||||
//
|
||||
@ -230,7 +230,7 @@ static void confirm_replacements(file_t *f, match_t *m, confirm_t *confirm)
|
||||
|
||||
retry:
|
||||
fprintf(tty_out, "\033[1mReplace? (y)es (n)o (r)emaining (d)one\033[0m ");
|
||||
fflush(tty_out);
|
||||
(void)fflush(tty_out);
|
||||
|
||||
char *answer = NULL;
|
||||
size_t len = 0;
|
||||
@ -297,7 +297,7 @@ static int inplace_modify_file(def_t *defs, file_t *f, pat_t *pattern)
|
||||
print_match(inplace_file, &pr, NULL);
|
||||
if (confirm == CONFIRM_ALL)
|
||||
printf("%s\n", f->filename);
|
||||
fclose(inplace_file);
|
||||
(void)fclose(inplace_file);
|
||||
|
||||
// TODO: if I want to implement backup files then add a line like this:
|
||||
// if (backup) rename(f->filename, f->filename + ".bak");
|
||||
@ -381,7 +381,7 @@ static int process_file(def_t *defs, const char *filename, pat_t *pattern)
|
||||
check(recycle_all_matches() == 0, "Memory leak: there should no longer be any matches in use at this point.");
|
||||
#endif
|
||||
destroy_file(&f);
|
||||
fflush(stdout);
|
||||
(void)fflush(stdout);
|
||||
return matches;
|
||||
}
|
||||
|
||||
@ -396,16 +396,20 @@ static int process_dir(def_t *defs, const char *dirname, pat_t *pattern)
|
||||
char globpath[PATH_MAX+1] = {0};
|
||||
check(snprintf(globpath, PATH_MAX, "%s/*", dirname) <= PATH_MAX,
|
||||
"Filename is too long: %s/*", dirname);
|
||||
glob(globpath, 0, NULL, &globbuf);
|
||||
struct stat statbuf;
|
||||
for (size_t i = 0; i < globbuf.gl_pathc; i++) {
|
||||
if (lstat(globbuf.gl_pathv[i], &statbuf) != 0) continue;
|
||||
if (S_ISLNK(statbuf.st_mode))
|
||||
continue; // Skip symbolic links
|
||||
else if (S_ISDIR(statbuf.st_mode))
|
||||
matches += process_dir(defs, globbuf.gl_pathv[i], pattern);
|
||||
else if (is_text_file(globbuf.gl_pathv[i]))
|
||||
matches += process_file(defs, globbuf.gl_pathv[i], pattern);
|
||||
int status = glob(globpath, 0, NULL, &globbuf);
|
||||
check(status != GLOB_ABORTED && status != GLOB_NOSPACE,
|
||||
"Failed to get directory contents: %s", dirname);
|
||||
if (status != GLOB_NOMATCH) {
|
||||
struct stat statbuf;
|
||||
for (size_t i = 0; i < globbuf.gl_pathc; i++) {
|
||||
if (lstat(globbuf.gl_pathv[i], &statbuf) != 0) continue;
|
||||
if (S_ISLNK(statbuf.st_mode))
|
||||
continue; // Skip symbolic links
|
||||
else if (S_ISDIR(statbuf.st_mode))
|
||||
matches += process_dir(defs, globbuf.gl_pathv[i], pattern);
|
||||
else if (is_text_file(globbuf.gl_pathv[i]))
|
||||
matches += process_file(defs, globbuf.gl_pathv[i], pattern);
|
||||
}
|
||||
}
|
||||
globfree(&globbuf);
|
||||
return matches;
|
||||
@ -522,10 +526,10 @@ int main(int argc, char *argv[])
|
||||
int signals[] = {SIGTERM, SIGINT, SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, SIGSEGV, SIGTSTP};
|
||||
struct sigaction sa = {.sa_handler = &sig_handler, .sa_flags = (int)(SA_NODEFER | SA_RESETHAND)};
|
||||
for (size_t i = 0; i < sizeof(signals)/sizeof(signals[0]); i++)
|
||||
sigaction(signals[i], &sa, NULL);
|
||||
check(!sigaction(signals[i], &sa, NULL), "Failed to set signal handler");
|
||||
|
||||
// Handle exit() calls gracefully:
|
||||
atexit(&cleanup);
|
||||
check(!atexit(&cleanup), "Failed to set cleanup handler at exit");
|
||||
|
||||
// User input/output is handled through /dev/tty so that normal unix pipes
|
||||
// can work properly while simultaneously asking for user input.
|
||||
@ -536,7 +540,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
if (pattern == NULL) { // If no pattern argument, then ask the user for a pattern
|
||||
fprintf(tty_out, "\033[1mPattern> \033[0m");
|
||||
fflush(tty_out);
|
||||
(void)fflush(tty_out);
|
||||
char *patstr = NULL;
|
||||
size_t len = 0;
|
||||
check(getline(&patstr, &len, tty_in) > 0, "No pattern provided");
|
||||
@ -579,12 +583,12 @@ int main(int argc, char *argv[])
|
||||
git_args[g++] = "git";
|
||||
git_args[g++] = "ls-files";
|
||||
while (i < argc) git_args[g++] = argv[i++];
|
||||
dup2(fds[STDOUT_FILENO], STDOUT_FILENO);
|
||||
close(fds[STDIN_FILENO]);
|
||||
execvp("git", git_args);
|
||||
check(dup2(fds[STDOUT_FILENO], STDOUT_FILENO), "Failed to hook up pipe to stdout");
|
||||
check(!close(fds[STDIN_FILENO]), "Failed to close read end of pipe");
|
||||
(void)execvp("git", git_args);
|
||||
_exit(1);
|
||||
}
|
||||
close(fds[STDOUT_FILENO]);
|
||||
check(!close(fds[STDOUT_FILENO]), "Failed to close write end of pipe");
|
||||
char path[PATH_MAX+2] = {0}; // path + \n + \0
|
||||
while (read(fds[STDIN_FILENO], path, PATH_MAX+1) > 0) { // Iterate over chunks
|
||||
for (char *nl; (nl = strchr(path, '\n')); ) { // Iterate over nl-terminated lines
|
||||
@ -593,9 +597,9 @@ int main(int argc, char *argv[])
|
||||
memmove(path, nl+1, sizeof(path)-(size_t)(nl+1-path));
|
||||
}
|
||||
}
|
||||
close(fds[STDIN_FILENO]);
|
||||
check(!close(fds[STDIN_FILENO]), "Failed to close read end of pipe");
|
||||
int status;
|
||||
waitpid(child, &status, 0);
|
||||
while (waitpid(child, &status, 0) != child) continue;
|
||||
check(WIFEXITED(status) && WEXITSTATUS(status) == 0,
|
||||
"`git --ls-files` failed. Do you have git installed?");
|
||||
} else if (i < argc) {
|
||||
@ -616,8 +620,8 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
if (mode == MODE_JSON) printf("]\n");
|
||||
|
||||
if (tty_out) { fclose(tty_out); tty_out = NULL; }
|
||||
if (tty_in) { fclose(tty_in); tty_in = NULL; }
|
||||
if (tty_out) { (void)fclose(tty_out); tty_out = NULL; }
|
||||
if (tty_in) { (void)fclose(tty_in); tty_in = NULL; }
|
||||
|
||||
#ifdef DEBUG_HEAP
|
||||
// This code frees up all residual heap-allocated memory. Since the program
|
||||
|
16
files.c
16
files.c
@ -93,7 +93,7 @@ file_t *load_file(file_t **files, const char *filename)
|
||||
}
|
||||
|
||||
finished_loading:
|
||||
if (fd != STDIN_FILENO) close(fd);
|
||||
if (fd != STDIN_FILENO) check(!close(fd), "Failed to close file");
|
||||
f->end = &f->contents[length];
|
||||
populate_lines(f);
|
||||
if (files != NULL) {
|
||||
@ -131,7 +131,8 @@ void intern_file(file_t *f)
|
||||
size_t size = (size_t)(f->end - f->contents);
|
||||
char *buf = xcalloc(sizeof(char), size + 1);
|
||||
memcpy(buf, f->contents, size);
|
||||
munmap(f->contents, size);
|
||||
check(!munmap(f->contents, size),
|
||||
"Failure to un-memory-map some memory");
|
||||
f->contents = buf;
|
||||
f->end = buf + size;
|
||||
f->mmapped = 0;
|
||||
@ -155,7 +156,8 @@ void destroy_file(file_t **f)
|
||||
|
||||
if ((*f)->contents) {
|
||||
if ((*f)->mmapped) {
|
||||
munmap((*f)->contents, (size_t)((*f)->end - (*f)->contents));
|
||||
check(!munmap((*f)->contents, (size_t)((*f)->end - (*f)->contents)),
|
||||
"Failure to un-memory-map some memory");
|
||||
(*f)->contents = NULL;
|
||||
} else {
|
||||
xfree(&((*f)->contents));
|
||||
@ -219,9 +221,9 @@ void fprint_line(FILE *dest, file_t *f, const char *start, const char *end, cons
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vfprintf(dest, fmt, args);
|
||||
(void)vfprintf(dest, fmt, args);
|
||||
va_end(args);
|
||||
fputc('\n', dest);
|
||||
(void)fputc('\n', dest);
|
||||
|
||||
const char *eol = linenum == f->nlines ? strchr(line, '\0') : strchr(line, '\n');
|
||||
if (end == NULL || end > eol) end = eol;
|
||||
@ -232,14 +234,14 @@ void fprint_line(FILE *dest, file_t *f, const char *start, const char *end, cons
|
||||
(int)(eol - end - 1), end);
|
||||
fprintf(dest, " \033[34;1m");
|
||||
const char *p = line;
|
||||
for (; p < start; ++p) fputc(*p == '\t' ? '\t' : ' ', dest);
|
||||
for (; p < start; ++p) (void)fputc(*p == '\t' ? '\t' : ' ', dest);
|
||||
if (start == end) ++end;
|
||||
for (; p < end; ++p)
|
||||
if (*p == '\t')
|
||||
// Some janky hacks: 8 ^'s, backtrack 8 spaces, move forward a tab stop, clear any ^'s that overshot
|
||||
fprintf(dest, "^^^^^^^^\033[8D\033[I\033[K");
|
||||
else
|
||||
fputc('^', dest);
|
||||
(void)fputc('^', dest);
|
||||
fprintf(dest, "\033[0m\n");
|
||||
}
|
||||
|
||||
|
2
files.h
2
files.h
@ -13,7 +13,7 @@ typedef struct file_s {
|
||||
const char *filename;
|
||||
char *contents, **lines, *end;
|
||||
size_t nlines;
|
||||
struct allocated_pat_s *pats;
|
||||
/*@only@*/ struct allocated_pat_s *pats;
|
||||
unsigned int mmapped:1;
|
||||
} file_t;
|
||||
|
||||
|
2
json.c
2
json.c
@ -51,7 +51,7 @@ static int _json_match(const char *text, match_t *m, int comma, unsigned int ver
|
||||
//
|
||||
void json_match(const char *text, match_t *m, unsigned int verbose)
|
||||
{
|
||||
_json_match(text, m, 0, verbose);
|
||||
(void)_json_match(text, m, 0, verbose);
|
||||
}
|
||||
|
||||
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1
|
||||
|
6
match.c
6
match.c
@ -595,7 +595,7 @@ static match_t *new_match(void)
|
||||
if (unused_matches) {
|
||||
m = unused_matches;
|
||||
unused_matches = unused_matches->next;
|
||||
memset(m, 0, sizeof(match_t));
|
||||
(void)memset(m, 0, sizeof(match_t));
|
||||
} else {
|
||||
m = new(match_t);
|
||||
}
|
||||
@ -622,10 +622,10 @@ void recycle_if_unused(match_t **at_m)
|
||||
|
||||
#ifdef DEBUG_HEAP
|
||||
DLL_REMOVE(m); // Remove from in_use_matches
|
||||
memset(m, 0, sizeof(match_t));
|
||||
(void)memset(m, 0, sizeof(match_t));
|
||||
DLL_PREPEND(unused_matches, m);
|
||||
#else
|
||||
memset(m, 0, sizeof(match_t));
|
||||
(void)memset(m, 0, sizeof(match_t));
|
||||
m->next = unused_matches;
|
||||
unused_matches = m;
|
||||
#endif
|
||||
|
@ -545,7 +545,7 @@ pat_t *bp_stringpattern(file_t *f, const char *str)
|
||||
ret = chain_together(f, ret, interp);
|
||||
str = interp->end;
|
||||
// allow terminating seq
|
||||
matchchar(&str, ';');
|
||||
(void)matchchar(&str, ';');
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
@ -599,7 +599,7 @@ def_t *bp_definition(def_t *defs, file_t *f, const char *str)
|
||||
if (!matchchar(&str, ':')) return NULL;
|
||||
pat_t *defpat = bp_pattern(f, str);
|
||||
if (!defpat) return NULL;
|
||||
matchchar(&defpat->end, ';'); // TODO: verify this is safe to mutate
|
||||
(void)matchchar(&defpat->end, ';'); // TODO: verify this is safe to mutate
|
||||
return with_def(defs, namelen, name, defpat);
|
||||
}
|
||||
|
||||
|
6
print.c
6
print.c
@ -304,20 +304,20 @@ static void _print_match(FILE *out, printer_t *pr, match_t *m)
|
||||
if (*r == '\\') {
|
||||
++r;
|
||||
char c = unescapechar(r, &r);
|
||||
fputc(c, out);
|
||||
(void)fputc(c, out);
|
||||
if (c == '\n') {
|
||||
++line;
|
||||
pr->needs_line_number = 1;
|
||||
}
|
||||
continue;
|
||||
} else if (*r == '\n') {
|
||||
fputc('\n', out);
|
||||
(void)fputc('\n', out);
|
||||
++line;
|
||||
pr->needs_line_number = 1;
|
||||
++r;
|
||||
continue;
|
||||
} else {
|
||||
fputc(*r, out);
|
||||
(void)fputc(*r, out);
|
||||
++r;
|
||||
continue;
|
||||
}
|
||||
|
4
types.h
4
types.h
@ -106,7 +106,7 @@ typedef struct def_s {
|
||||
size_t namelen;
|
||||
const char *name;
|
||||
pat_t *pat;
|
||||
struct def_s *next;
|
||||
/*@only@*/ struct def_s *next;
|
||||
} def_t;
|
||||
|
||||
//
|
||||
@ -114,7 +114,7 @@ typedef struct def_s {
|
||||
// file is freed.
|
||||
//
|
||||
typedef struct allocated_pat_s {
|
||||
struct allocated_pat_s *next;
|
||||
/*@only@*/ struct allocated_pat_s *next;
|
||||
pat_t pat;
|
||||
} allocated_pat_t;
|
||||
|
||||
|
5
utils.h
5
utils.h
@ -11,7 +11,7 @@
|
||||
#include "match.h"
|
||||
|
||||
#define streq(a, b) (strcmp(a, b) == 0)
|
||||
#define check(cond, ...) do { if (!(cond)) { fprintf(stderr, __VA_ARGS__); fwrite("\n", 1, 1, stderr); exit(1); } } while(0)
|
||||
#define check(cond, ...) do { if (!(cond)) { (void)fprintf(stderr, __VA_ARGS__); (void)fwrite("\n", 1, 1, stderr); exit(1); } } while(0)
|
||||
#define new(t) memcheck(calloc(1, sizeof(t)))
|
||||
#define xcalloc(a,b) memcheck(calloc(a,b))
|
||||
#define xrealloc(a,b) memcheck(realloc(a,b))
|
||||
@ -28,7 +28,8 @@ __attribute__((nonnull))
|
||||
int matchstr(const char **str, const char *target);
|
||||
__attribute__((nonnull))
|
||||
size_t unescape_string(char *dest, const char *src, size_t bufsize);
|
||||
void *memcheck(void *p);
|
||||
__attribute__((returns_nonnull))
|
||||
void *memcheck(/*@null@*/ /*@out@*/ void *p);
|
||||
__attribute__((nonnull))
|
||||
int memicmp(const void *s1, const void *s2, size_t n);
|
||||
__attribute__((nonnull))
|
||||
|
Loading…
Reference in New Issue
Block a user