Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checks for errors #161

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/record.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
29 changes: 28 additions & 1 deletion src/tracer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand Down