Skip to content

Commit

Permalink
prevent memory leak in xmpp_stanza_new_from_string()
Browse files Browse the repository at this point in the history
Only remember the first stanza in case the string contains
multiple stanzas.

Signed-off-by: Steffen Jaeckel <[email protected]>
  • Loading branch information
sjaeckel committed May 5, 2022
1 parent d8c9ca4 commit 62d1063
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/stanza.c
Original file line number Diff line number Diff line change
Expand Up @@ -1640,8 +1640,11 @@ static void _stub_stream_end(char *name, void *userdata)

static void _stream_stanza(xmpp_stanza_t *stanza, void *userdata)
{
stanza = xmpp_stanza_clone(stanza);
*(xmpp_stanza_t **)userdata = stanza;
xmpp_stanza_t **dest = userdata;
if (*dest == NULL) {
stanza = xmpp_stanza_clone(stanza);
*dest = stanza;
}
}

/** Create a stanza object from the string.
Expand Down
18 changes: 17 additions & 1 deletion tests/test_stanza.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static void test_stanza_from_string(xmpp_ctx_t *ctx)
size_t buflen;
int ret;

static const char *str =
const char *str =
"<signcrypt xmlns=\"urn:xmpp:openpgp:0\"><to "
"jid=\"[email protected]\"/><time "
"stamp=\"2020-06-03T21:26:24+0200\"/><rpad/><payload><body "
Expand All @@ -113,6 +113,22 @@ static void test_stanza_from_string(xmpp_ctx_t *ctx)
xmpp_free(ctx, buf);
xmpp_stanza_release(stanza);

/* create a string with two stanzas to make sure we don't
* leak any memory when we convert them to a xmpp_stanza_t
*/
buf = malloc(strlen(str) * 2 + 1);
assert(buf != NULL);
memcpy(buf, str, strlen(str) + 1);
memcpy(&buf[strlen(str)], str, strlen(str) + 1);
stanza = xmpp_stanza_new_from_string(ctx, buf);
assert(stanza != NULL);
free(buf);
ret = xmpp_stanza_to_text(stanza, &buf, &buflen);
assert(ret == XMPP_EOK);
COMPARE(str, buf);
xmpp_free(ctx, buf);
xmpp_stanza_release(stanza);

/* Error path. */
stanza = xmpp_stanza_new_from_string(ctx, "<uu><uu>tt");
assert(stanza == NULL);
Expand Down

0 comments on commit 62d1063

Please sign in to comment.