Skip to content

Commit a75a5e3

Browse files
committed
Actually check read and write return values. Fixes #5.
1 parent ceb5160 commit a75a5e3

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

Lib/module.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,9 @@ static int tell_if(void *data, void *m) {
400400
MODULE_DEBUG("Telling a message to %s.\n", mod->name);
401401

402402
pubsub_msg_t *mm = create_pubsub_msg(msg->message, msg->sender, msg->topic, msg->type, msg->size);
403-
write(mod->pubsub_fd[1], &mm, sizeof(pubsub_msg_t *));
403+
if (write(mod->pubsub_fd[1], &mm, sizeof(pubsub_msg_t *)) != sizeof(pubsub_msg_t *)) {
404+
MODULE_DEBUG("Failed to write message for %s: %s\n", mod->name, strerror(errno));
405+
}
404406
}
405407
return MAP_OK;
406408
}

Lib/modules.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ module_ret_code modules_ctx_loop_events(const char *ctx_name, const int max_even
7575
if (p->fd == mod->pubsub_fd[0]) {
7676
/* Received on pubsub interface */
7777
*(bool *)&msg.is_pubsub = true;
78-
read(p->fd, (void **)&msg.pubsub_msg, sizeof(pubsub_msg_t *));
78+
if (read(p->fd, (void **)&msg.pubsub_msg, sizeof(pubsub_msg_t *)) != sizeof(pubsub_msg_t *)) {
79+
MODULE_DEBUG("Failed to read message for %s: %s\n", mod->name, strerror(errno));
80+
*((pubsub_msg_t **)&msg.pubsub_msg) = NULL;
81+
}
7982
} else {
8083
/* Received from FD */
8184
*(bool *)&msg.is_pubsub = false;
@@ -84,17 +87,19 @@ module_ret_code modules_ctx_loop_events(const char *ctx_name, const int max_even
8487
*(fd_msg_t **)&msg.fd_msg = &fd_msg;
8588
}
8689

87-
/* If module is using some different receive function, honor it. */
88-
recv_cb cb = stack_peek(mod->recvs);
89-
if (!cb) {
90-
/* Fallback to module default receive */
91-
cb = mod->hook.recv;
92-
}
93-
cb(&msg, mod->userdata);
94-
95-
/* Properly free pubsub msg */
96-
if (p->fd == mod->pubsub_fd[0]) {
97-
destroy_pubsub_msg((pubsub_msg_t *)msg.pubsub_msg);
90+
if (!msg.is_pubsub || msg.pubsub_msg) {
91+
/* If module is using some different receive function, honor it. */
92+
recv_cb cb = stack_peek(mod->recvs);
93+
if (!cb) {
94+
/* Fallback to module default receive */
95+
cb = mod->hook.recv;
96+
}
97+
cb(&msg, mod->userdata);
98+
99+
/* Properly free pubsub msg */
100+
if (msg.is_pubsub) {
101+
destroy_pubsub_msg((pubsub_msg_t *)msg.pubsub_msg);
102+
}
98103
}
99104
} else {
100105
/* Forward error to below handling code */

0 commit comments

Comments
 (0)