pespin has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/41706?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 --- M src/core/gsmtap_util.c 1 file changed, 11 insertions(+), 3 deletions(-)
Approvals: fixeria: Looks good to me, but someone else must approve Jenkins Builder: Verified pespin: Looks good to me, approved osmith: Looks good to me, but someone else must approve
diff --git a/src/core/gsmtap_util.c b/src/core/gsmtap_util.c index f1a2abf..903821d 100644 --- a/src/core/gsmtap_util.c +++ b/src/core/gsmtap_util.c @@ -421,7 +421,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 @@ -436,7 +436,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. */ @@ -520,7 +528,7 @@ close(gti->source_fd);
- if (gti->sink_fd != -1) { + if (gti->sink_fd >= 0) { close(gti->sink_fd); gti->sink_fd = -1; }