xmppipe_send_message: fix memory leak

Fix a memory leak caused by improper usage of
xmpp_stanza_new()/xmpp_stanza_release() by replacing usage with the
simpler xmpp_message_new()/xmpp_message_set_body() API available in
libstrophe 0.9.0, as advised by @pasis.

Fixes https://github.com/msantos/xmppipe/issues/3.
master
Michael Santos 5 years ago
parent 8792a8a05c
commit 1bb03b563e

@ -1,4 +1,4 @@
/* Copyright (c) 2015-2017, Michael Santos <michael.santos@gmail.com>
/* Copyright (c) 2015-2019, Michael Santos <michael.santos@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@ -145,6 +145,10 @@ char *xmppipe_strdup(const char *);
void *xmppipe_malloc(size_t);
void *xmppipe_calloc(size_t, size_t);
xmpp_stanza_t *xmppipe_message_new(xmpp_ctx_t *ctx, const char *const type,
const char *const to, const char *const id);
void xmppipe_message_set_body(xmpp_stanza_t *msg, const char *const text);
xmpp_stanza_t *xmppipe_stanza_new(xmpp_ctx_t *);
void xmppipe_stanza_set_attribute(xmpp_stanza_t *const, const char *const,
const char *const);

@ -135,8 +135,6 @@ XMPPIPE_DONE:
void xmppipe_send_message(xmppipe_state_t *state, char *to, char *type,
char *buf, size_t len) {
xmpp_stanza_t *message = NULL;
xmpp_stanza_t *body = NULL;
xmpp_stanza_t *text = NULL;
char *id = NULL;
id = xmpp_uuid_gen(state->ctx);
@ -145,16 +143,7 @@ void xmppipe_send_message(xmppipe_state_t *state, char *to, char *type,
errx(EXIT_FAILURE, "unable to allocate message id");
}
message = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(message, "message");
xmppipe_stanza_set_type(message, type);
xmppipe_stanza_set_attribute(message, "to", to);
xmppipe_stanza_set_id(message, id);
body = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(body, "body");
text = xmppipe_stanza_new(state->ctx);
message = xmppipe_message_new(state->ctx, type, to, id);
if (state->encode) {
size_t len = strlen(buf);
@ -163,17 +152,13 @@ void xmppipe_send_message(xmppipe_state_t *state, char *to, char *type,
if (b64 == NULL)
errx(EXIT_FAILURE, "encode: invalid input: %zu", len);
xmppipe_stanza_set_text(text, b64);
xmppipe_message_set_body(message, b64);
xmpp_free(state->ctx, b64);
} else {
xmppipe_stanza_set_text(text, buf);
xmppipe_message_set_body(message, buf);
}
xmppipe_stanza_add_child(body, text);
xmppipe_stanza_add_child(message, body);
xmppipe_send(state, message);
(void)xmpp_stanza_release(message);
xmpp_free(state->ctx, id);
}

@ -1,4 +1,4 @@
/* Copyright (c) 2015-2018, Michael Santos <michael.santos@gmail.com>
/* Copyright (c) 2015-2019, Michael Santos <michael.santos@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@ -94,6 +94,24 @@ void *xmppipe_calloc(size_t nmemb, size_t size) {
return buf;
}
xmpp_stanza_t *xmppipe_message_new(xmpp_ctx_t *ctx, const char *const type,
const char *const to, const char *const id) {
xmpp_stanza_t *m = xmpp_message_new(ctx, type, to, id);
if (m == NULL)
err(3, "xmppipe_stanza_new");
return m;
}
void xmppipe_message_set_body(xmpp_stanza_t *msg, const char *const text) {
int rv;
rv = xmpp_message_set_body(msg, text);
if (rv != XMPP_EOK)
errx(EXIT_FAILURE, "xmpp_message_set_body: %u", rv);
}
xmpp_stanza_t *xmppipe_stanza_new(xmpp_ctx_t *ctx) {
xmpp_stanza_t *s = xmpp_stanza_new(ctx);

Loading…
Cancel
Save