jolly has uploaded this change for review. ( 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(-)
git pull ssh://gerrit.osmocom.org:29418/libosmo-netif refs/changes/26/35926/1
diff --git a/examples/stream-client.c b/examples/stream-client.c index f17a7d8..af0e38f 100644 --- a/examples/stream-client.c +++ b/examples/stream-client.c @@ -86,9 +86,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 26dff95..2dee5f6 100644 --- a/examples/stream-server.c +++ b/examples/stream-server.c @@ -115,7 +115,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 e7e0ec6..4a39166 100644 --- a/src/stream_cli.c +++ b/src/stream_cli.c @@ -946,6 +946,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);
@@ -956,9 +958,11 @@ break; case OSMO_STREAM_MODE_OSMO_IO: if (cli->proto == IPPROTO_SCTP) - osmo_iofd_sctp_send_msgb(cli->iofd, msg, MSG_NOSIGNAL); + rc = osmo_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 ff38756..d493cdc 100644 --- a/src/stream_srv.c +++ b/src/stream_srv.c @@ -940,6 +940,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) { @@ -955,9 +957,11 @@ break; case OSMO_STREAM_MODE_OSMO_IO: if (conn->srv->proto == IPPROTO_SCTP) - osmo_iofd_sctp_send_msgb(conn->iofd, msg, MSG_NOSIGNAL); + rc = osmo_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);