pespin has uploaded this change for review.

View Change

gsmtap_util: Avoid sink_fd leak gsmtap_source_add_sinki() called multiple times

gti->sink_fd could leak if gsmtap_source_add_sink() was called multiple
times.

Also if gsmtap_source_add_sink_fd() failed, rc != -1 was returned and
hence a close() on a wrong fd would be attempted.

This commit fixes both issues, making the whole sink_fd field much more
robust.

Change-Id: I7af5a6c7d64954ee2cc013711702b846dfaa02b1
(cherry picked from commit 85112c4911252412f0d127f01bde92794e232d00)
---
M src/core/gsmtap_util.c
1 file changed, 11 insertions(+), 3 deletions(-)

git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/10/41710/1
diff --git a/src/core/gsmtap_util.c b/src/core/gsmtap_util.c
index 35bbf77..9b9c3b6 100644
--- a/src/core/gsmtap_util.c
+++ b/src/core/gsmtap_util.c
@@ -435,7 +435,7 @@

/*! Add a local sink to an existing GSMTAP source and return fd
* \param[in] gti existing GSMTAP source
- * \returns file descriptor of locally bound receive socket
+ * \returns file descriptor of locally bound receive socket; negative on error
*
* In case the GSMTAP socket is connected to a local destination
* IP/port, this function creates a corresponding receiving socket
@@ -450,7 +450,15 @@
*/
int gsmtap_source_add_sink(struct gsmtap_inst *gti)
{
- return gti->sink_fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd2(gti));
+ int rc;
+
+ if (gti->sink_fd >= 0)
+ return -EALREADY;
+
+ rc = gsmtap_source_add_sink_fd(gsmtap_inst_fd2(gti));
+ if (rc >= 0)
+ gti->sink_fd = rc;
+ return rc;
}

/* Registered in Osmo IO as a no-op to set the write callback. */
@@ -535,7 +543,7 @@
close(gti->wq.bfd.fd);


- if (gti->sink_fd != -1) {
+ if (gti->sink_fd >= 0) {
close(gti->sink_fd);
gti->sink_fd = -1;
}

To view, visit change 41710. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: libosmocore
Gerrit-Branch: pespin/rel-1.12.0
Gerrit-Change-Id: I7af5a6c7d64954ee2cc013711702b846dfaa02b1
Gerrit-Change-Number: 41710
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin@sysmocom.de>