Skip to content

Commit cafe5ae

Browse files
committed
It's still better to strdup at least topic.
1 parent 22bdbd5 commit cafe5ae

File tree

5 files changed

+10
-5
lines changed

5 files changed

+10
-5
lines changed

Lib/module.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ int flush_pubsub_msg(void *data, void *m) {
330330
const msg_t msg = { .is_pubsub = 1, .pubsub_msg = mm };
331331
mod->hook.recv(&msg, mod->userdata);
332332
}
333-
memhook._free(mm);
333+
destroy_pubsub_msg(mm);
334334
}
335335
return 0;
336336
}
@@ -362,13 +362,18 @@ static pubsub_msg_t *create_pubsub_msg(const unsigned char *message, const self_
362362
if (m) {
363363
m->message = message;
364364
m->sender = sender;
365-
m->topic = topic;
365+
m->topic = mem_strdup(topic);
366366
*(int *)&m->type = type;
367367
*(size_t *)&m->size = size;
368368
}
369369
return m;
370370
}
371371

372+
void destroy_pubsub_msg(pubsub_msg_t *m) {
373+
memhook._free((char *)m->topic);
374+
memhook._free(m);
375+
}
376+
372377
static module_ret_code tell_pubsub_msg(pubsub_msg_t *m, module *mod, m_context *c) {
373378
if (mod) {
374379
tell_if(m, mod);

Lib/module_priv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ typedef struct {
7373
int evaluate_module(void *data, void *m);
7474
module_ret_code tell_system_pubsub_msg(m_context *c, enum msg_type type, const char *topic);
7575
int flush_pubsub_msg(void *data, void *m);
76+
void destroy_pubsub_msg(pubsub_msg_t *m);
7677
char *mem_strdup(const char *s);
7778

7879
extern map_t ctx;

Lib/modules.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ module_ret_code modules_ctx_loop_events(const char *ctx_name, const int max_even
8989

9090
/* Properly free pubsub msg */
9191
if (p->fd == mod->pubsub_fd[0]) {
92-
memhook._free((void *)msg.pubsub_msg);
92+
destroy_pubsub_msg((pubsub_msg_t *)msg.pubsub_msg);
9393
}
9494
}
9595
evaluate_new_state(c);

TODO.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
- [x] pubsub messaging to send bytes instead of string (ie: add size to pubsub_msg_t)
44
- [x] Avoid strdup for pubsub msg and trust user-provided pointer
5-
- [x] Same as above for topic
65
- [x] Use memhook malloc/free in epoll_priv, kqueue_priv poll_set_data()
76
- [x] Avoid strdup and use internal strdup that makes use of memhook
87
- [x] modules_quit should take an "exit value" parameter

docs/src/pubsub.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ PubSub notes
3232
Note that a context must be looping to receive any pubsub message. |br|
3333
Moreover, when a context stops looping, all pubsub messages will be flushed and thus delivered to each RUNNING module. |br|
3434
Pubsub message sent while context is not looping are buffered until context starts looping. For more information, see `pipe capacity <https://linux.die.net/man/7/pipe>`_. |br|
35-
Finally, please be aware that both data and topic sent through pubsub messaging are trusted, ie: you should pay attention to their scopes.
35+
Finally, please be aware that data pointer sent through pubsub messaging is trusted, ie: you should pay attention to its scope.

0 commit comments

Comments
 (0)