pespin has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/41710?usp=email )
Change subject: gsmtap_util: Avoid sink_fd leak gsmtap_source_add_sinki() called multiple times ......................................................................
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(-)
Approvals: osmith: Looks good to me, approved Jenkins Builder: Verified
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; }