Skip to content

Commit 62d1063

Browse files
committed
prevent memory leak in xmpp_stanza_new_from_string()
Only remember the first stanza in case the string contains multiple stanzas. Signed-off-by: Steffen Jaeckel <[email protected]>
1 parent d8c9ca4 commit 62d1063

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/stanza.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,8 +1640,11 @@ static void _stub_stream_end(char *name, void *userdata)
16401640

16411641
static void _stream_stanza(xmpp_stanza_t *stanza, void *userdata)
16421642
{
1643-
stanza = xmpp_stanza_clone(stanza);
1644-
*(xmpp_stanza_t **)userdata = stanza;
1643+
xmpp_stanza_t **dest = userdata;
1644+
if (*dest == NULL) {
1645+
stanza = xmpp_stanza_clone(stanza);
1646+
*dest = stanza;
1647+
}
16451648
}
16461649

16471650
/** Create a stanza object from the string.

tests/test_stanza.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static void test_stanza_from_string(xmpp_ctx_t *ctx)
9999
size_t buflen;
100100
int ret;
101101

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

116+
/* create a string with two stanzas to make sure we don't
117+
* leak any memory when we convert them to a xmpp_stanza_t
118+
*/
119+
buf = malloc(strlen(str) * 2 + 1);
120+
assert(buf != NULL);
121+
memcpy(buf, str, strlen(str) + 1);
122+
memcpy(&buf[strlen(str)], str, strlen(str) + 1);
123+
stanza = xmpp_stanza_new_from_string(ctx, buf);
124+
assert(stanza != NULL);
125+
free(buf);
126+
ret = xmpp_stanza_to_text(stanza, &buf, &buflen);
127+
assert(ret == XMPP_EOK);
128+
COMPARE(str, buf);
129+
xmpp_free(ctx, buf);
130+
xmpp_stanza_release(stanza);
131+
116132
/* Error path. */
117133
stanza = xmpp_stanza_new_from_string(ctx, "<uu><uu>tt");
118134
assert(stanza == NULL);

0 commit comments

Comments
 (0)