diff options
author | Andreas Eversberg <jolly@eversberg.eu> | 2024-02-09 15:39:00 +0100 |
---|---|---|
committer | Andreas Eversberg <jolly@eversberg.eu> | 2024-02-15 11:59:11 +0100 |
commit | 81080ac56d3b9755182355b499811886ec7a222f (patch) | |
tree | b263deec1a682db3887367e5db5bd24a4d7cab97 | |
parent | e55e417ba4a2d1d0a49663566c1096e6edc933ae (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.c | 4 | ||||
-rw-r--r-- | examples/stream-server.c | 2 | ||||
-rw-r--r-- | src/stream_cli.c | 8 | ||||
-rw-r--r-- | src/stream_srv.c | 8 |
4 files changed, 15 insertions, 7 deletions
diff --git a/examples/stream-client.c b/examples/stream-client.c index e42748f..b9cbe2f 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 9faf307..f11e53f 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 e7e0ec6..4a39166 100644 --- a/src/stream_cli.c +++ b/src/stream_cli.c @@ -946,6 +946,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); @@ -956,9 +958,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) - 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 @@ 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) { @@ -955,9 +957,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) - 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); |