From 924db2678641f028a5701af31783def219d8df0d Mon Sep 17 00:00:00 2001 From: Fotios Valasiadis Date: Wed, 8 Feb 2023 12:11:53 +0200 Subject: [PATCH] add checks for errors --- src/hash.c | 4 +++- src/record.c | 4 +++- src/tracer.c | 29 ++++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/hash.c b/src/hash.c index a8b1204..a182911 100644 --- a/src/hash.c +++ b/src/hash.c @@ -91,7 +91,9 @@ hash_file_contents(char *name, size_t sz) SHA1_Update(&ctx, buf, sz); SHA1_Final(hash, &ctx); - close(fd); + if (close(fd) < 0) { + error(EXIT_FAILURE, errno, "on hash_file_contents close"); + } if (munmap(buf, sz) < 0) { error(EXIT_FAILURE, errno, "unmapping `%s'", name); diff --git a/src/record.c b/src/record.c index 1d0f985..20b6af1 100644 --- a/src/record.c +++ b/src/record.c @@ -68,7 +68,9 @@ get_cmdline(pid_t pid) char data[CMD_LINE_SIZE + 1]; ssize_t n = read(fd, data, CMD_LINE_SIZE); - close(fd); + if (close(fd) < 0) { + error(EXIT_FAILURE, errno, "on get_cmdline close"); + } if (n < 0) { error(EXIT_FAILURE, errno, "on get_cmdline read"); diff --git a/src/tracer.c b/src/tracer.c index 7b18267..82bdad5 100644 --- a/src/tracer.c +++ b/src/tracer.c @@ -56,11 +56,20 @@ init(void) { pinfo_size = DEFAULT_PINFO_SIZE; pinfo = calloc(pinfo_size, sizeof (PROCESS_INFO)); + if (pinfo == NULL) { + error(EXIT_FAILURE, errno, "on init pinfo calloc"); + } pids = malloc(pinfo_size * sizeof (int)); + if (pids == NULL) { + error(EXIT_FAILURE, errno, "on init pids malloc"); + } numpinfo = -1; finfo_size = DEFAULT_FINFO_SIZE; finfo = calloc(finfo_size, sizeof (FILE_INFO)); + if (finfo == NULL) { + error(EXIT_FAILURE, errno, "on init finfo calloc"); + } numfinfo = -1; } @@ -104,7 +113,13 @@ pinfo_new(PROCESS_INFO *self, char ignore_one_sigstop) self->finfo_size = DEFAULT_FINFO_SIZE; self->numfinfo = -1; self->finfo = malloc(self->finfo_size * sizeof (FILE_INFO)); + if (self->finfo == NULL) { + error(EXIT_FAILURE, errno, "on pinfo_new self->finfo malloc"); + } self->fds = malloc(self->finfo_size * sizeof (int)); + if (self->fds == NULL) { + error(EXIT_FAILURE, errno, "on pinfo_new self->fds malloc"); + } self->ignore_one_sigstop = ignore_one_sigstop; } @@ -203,9 +218,13 @@ get_str_from_process(pid_t pid, void *addr) size_t i = 0; do { + errno = 0; data.lval = ptrace(PTRACE_PEEKDATA, pid, (char *) addr + i * sizeof (long), NULL); + if (errno != 0) { + error(EXIT_FAILURE, errno, "on get_str_from_process ptrace"); + } for (unsigned j = 0; j < sizeof (long); j++) { *dest++ = data.cval[j]; if (data.cval[j] == 0) @@ -339,6 +358,10 @@ static void handle_rename_entry(pid_t pid, PROCESS_INFO *pi, int olddirfd, char *oldpath) { char *abspath = absolutepath(pid, olddirfd, oldpath); + + if (abspath == NULL) { + error(EXIT_FAILURE, errno, "on handle_rename_entry absolutepath"); + } char *hash = get_file_hash(abspath); FILE_INFO *f = find_finfo(abspath, hash); @@ -356,7 +379,7 @@ handle_rename_entry(pid_t pid, PROCESS_INFO *pi, int olddirfd, char *oldpath) pi->entry_info = (void *) (f - finfo); if (pi->entry_info == NULL) - error(EXIT_FAILURE, errno, "on handle_rename_entry absolutepath"); + error(EXIT_FAILURE, errno, "on handle_rename_entry entry_info"); } static void @@ -366,6 +389,10 @@ handle_rename_exit(pid_t pid, PROCESS_INFO *pi, int newdirfd, char *newpath) char *abspath = absolutepath(pid, newdirfd, newpath); + if (abspath == NULL) { + error(EXIT_FAILURE, errno, "on handle_rename_exit absolutepath"); + } + FILE_INFO *to = next_finfo(); finfo_new(to, newpath, abspath, from->hash);