laforge has submitted this change. (
https://gerrit.osmocom.org/c/libosmo-netif/+/35926?usp=email )
Change subject: stream_{cli,srv}: Fix memory leak, if sending a message fails
......................................................................
stream_{cli,srv}: Fix memory leak, if sending a message fails
Also the example client/server must not access msgb after sending it,
especially if the msgb got freed due to a failure.
Change-Id: I627a71b4f0183cd83835c328a5cdd67a413ae614
---
M examples/stream-client.c
M examples/stream-server.c
M src/stream_cli.c
M src/stream_srv.c
4 files changed, 27 insertions(+), 7 deletions(-)
Approvals:
laforge: Looks good to me, but someone else must approve
pespin: Looks good to me, approved
Jenkins Builder: Verified
diff --git a/examples/stream-client.c b/examples/stream-client.c
index da90e8c..97aaa1c 100644
--- a/examples/stream-client.c
+++ b/examples/stream-client.c
@@ -83,9 +83,9 @@
ptr = msgb_put(msg, ret);
memcpy(ptr, buf, ret);
- osmo_stream_cli_send(conn, msg);
+ LOGP(DSTREAMTEST, LOGL_NOTICE, "sending %d bytes message: %s\n", msg->len,
msgb_hexdump(msg));
- LOGP(DSTREAMTEST, LOGL_NOTICE, "sent %d bytes message: %s\n", msg->len,
msgb_hexdump(msg));
+ osmo_stream_cli_send(conn, msg);
return 0;
}
diff --git a/examples/stream-server.c b/examples/stream-server.c
index 1cd3517..d0647b7 100644
--- a/examples/stream-server.c
+++ b/examples/stream-server.c
@@ -112,7 +112,7 @@
memcpy(ptr, buf, ret);
osmo_stream_srv_send(conn, msg);
- LOGP(DSTREAMTEST, LOGL_NOTICE, "message of %d bytes sent\n", msg->len);
+ LOGP(DSTREAMTEST, LOGL_NOTICE, "message of %d bytes sent\n", ret);
return 0;
}
diff --git a/src/stream_cli.c b/src/stream_cli.c
index cf639bc..b664117 100644
--- a/src/stream_cli.c
+++ b/src/stream_cli.c
@@ -990,6 +990,8 @@
* \param[in] msg Message buffer to enqueue in transmit queue */
void osmo_stream_cli_send(struct osmo_stream_cli *cli, struct msgb *msg)
{
+ int rc;
+
OSMO_ASSERT(cli);
OSMO_ASSERT(msg);
@@ -1000,9 +1002,11 @@
break;
case OSMO_STREAM_MODE_OSMO_IO:
if (cli->proto == IPPROTO_SCTP)
- stream_iofd_sctp_send_msgb(cli->iofd, msg, MSG_NOSIGNAL);
+ rc = stream_iofd_sctp_send_msgb(cli->iofd, msg, MSG_NOSIGNAL);
else
- osmo_iofd_write_msgb(cli->iofd, msg);
+ rc = osmo_iofd_write_msgb(cli->iofd, msg);
+ if (rc < 0)
+ msgb_free(msg);
break;
default:
OSMO_ASSERT(false);
diff --git a/src/stream_srv.c b/src/stream_srv.c
index 041a4ef..70ebef8 100644
--- a/src/stream_srv.c
+++ b/src/stream_srv.c
@@ -984,6 +984,8 @@
* \param[in] msg Message buffer to enqueue in transmit queue */
void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg)
{
+ int rc;
+
OSMO_ASSERT(conn);
OSMO_ASSERT(msg);
if (conn->flags & OSMO_STREAM_SRV_F_FLUSH_DESTROY) {
@@ -999,9 +1001,11 @@
break;
case OSMO_STREAM_MODE_OSMO_IO:
if (conn->srv->proto == IPPROTO_SCTP)
- stream_iofd_sctp_send_msgb(conn->iofd, msg, MSG_NOSIGNAL);
+ rc = stream_iofd_sctp_send_msgb(conn->iofd, msg, MSG_NOSIGNAL);
else
- osmo_iofd_write_msgb(conn->iofd, msg);
+ rc = osmo_iofd_write_msgb(conn->iofd, msg);
+ if (rc < 0)
+ msgb_free(msg);
break;
default:
OSMO_ASSERT(false);
--
To view, visit
https://gerrit.osmocom.org/c/libosmo-netif/+/35926?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.osmocom.org/settings
Gerrit-Project: libosmo-netif
Gerrit-Branch: master
Gerrit-Change-Id: I627a71b4f0183cd83835c328a5cdd67a413ae614
Gerrit-Change-Number: 35926
Gerrit-PatchSet: 6
Gerrit-Owner: jolly <andreas(a)eversberg.eu>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-Reviewer: pespin <pespin(a)sysmocom.de>
Gerrit-MessageType: merged