Skip to content

Commit

Permalink
Actually check read and write return values. Fixes #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeDP committed Nov 24, 2018
1 parent ceb5160 commit a75a5e3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
4 changes: 3 additions & 1 deletion Lib/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,9 @@ static int tell_if(void *data, void *m) {
MODULE_DEBUG("Telling a message to %s.\n", mod->name);

pubsub_msg_t *mm = create_pubsub_msg(msg->message, msg->sender, msg->topic, msg->type, msg->size);
write(mod->pubsub_fd[1], &mm, sizeof(pubsub_msg_t *));
if (write(mod->pubsub_fd[1], &mm, sizeof(pubsub_msg_t *)) != sizeof(pubsub_msg_t *)) {
MODULE_DEBUG("Failed to write message for %s: %s\n", mod->name, strerror(errno));
}
}
return MAP_OK;
}
Expand Down
29 changes: 17 additions & 12 deletions Lib/modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ module_ret_code modules_ctx_loop_events(const char *ctx_name, const int max_even
if (p->fd == mod->pubsub_fd[0]) {
/* Received on pubsub interface */
*(bool *)&msg.is_pubsub = true;
read(p->fd, (void **)&msg.pubsub_msg, sizeof(pubsub_msg_t *));
if (read(p->fd, (void **)&msg.pubsub_msg, sizeof(pubsub_msg_t *)) != sizeof(pubsub_msg_t *)) {
MODULE_DEBUG("Failed to read message for %s: %s\n", mod->name, strerror(errno));
*((pubsub_msg_t **)&msg.pubsub_msg) = NULL;
}
} else {
/* Received from FD */
*(bool *)&msg.is_pubsub = false;
Expand All @@ -84,17 +87,19 @@ module_ret_code modules_ctx_loop_events(const char *ctx_name, const int max_even
*(fd_msg_t **)&msg.fd_msg = &fd_msg;
}

/* If module is using some different receive function, honor it. */
recv_cb cb = stack_peek(mod->recvs);
if (!cb) {
/* Fallback to module default receive */
cb = mod->hook.recv;
}
cb(&msg, mod->userdata);

/* Properly free pubsub msg */
if (p->fd == mod->pubsub_fd[0]) {
destroy_pubsub_msg((pubsub_msg_t *)msg.pubsub_msg);
if (!msg.is_pubsub || msg.pubsub_msg) {
/* If module is using some different receive function, honor it. */
recv_cb cb = stack_peek(mod->recvs);
if (!cb) {
/* Fallback to module default receive */
cb = mod->hook.recv;
}
cb(&msg, mod->userdata);

/* Properly free pubsub msg */
if (msg.is_pubsub) {
destroy_pubsub_msg((pubsub_msg_t *)msg.pubsub_msg);
}
}
} else {
/* Forward error to below handling code */
Expand Down

0 comments on commit a75a5e3

Please sign in to comment.