aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Eversberg <jolly@eversberg.eu>2024-02-09 15:39:00 +0100
committerAndreas Eversberg <jolly@eversberg.eu>2024-02-29 13:23:20 +0100
commitd467b6348cacfab8e3103362f583294531245563 (patch)
treef6c0eb5d2940416af9e95c705144b9c37137cc4d
parent7e6d2e0f99ff095f4714f03b1ed991d6c9cb9c61 (diff)
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
-rw-r--r--examples/stream-client.c4
-rw-r--r--examples/stream-server.c2
-rw-r--r--src/stream_cli.c8
-rw-r--r--src/stream_srv.c8
4 files changed, 15 insertions, 7 deletions
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 @@ static int kbd_cb(struct osmo_fd *fd, unsigned int what)
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 @@ static int kbd_cb(struct osmo_fd *fd, unsigned int what)
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 @@ static void cli_timer_cb(void *data)
* \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 @@ void osmo_stream_cli_send(struct osmo_stream_cli *cli, struct msgb *msg)
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 @@ void osmo_stream_srv_destroy(struct osmo_stream_srv *conn)
* \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 @@ void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg)
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);