[libdvbpsi-devel] examples/dvbinfo/tcp.c: Resource leak (CID 17269)

Jean-Paul Saman git at videolan.org
Wed Apr 16 15:53:31 CEST 2014


libdvbpsi | branch: master | Jean-Paul Saman <jpsaman at videolan.org> | Fri Apr 11 15:20:53 2014 +0200| [f70707bbd022f88a12cacf83b57dcdf07c6dd57a] | committer: Jean-Paul Saman

examples/dvbinfo/tcp.c: Resource leak (CID 17269)

The code does not leak a socket descriptor while looping over addrinfo pointer structs.However initializing s_ctl to -1 again after calling close(s_ctl) is good practise.

> http://git.videolan.org/gitweb.cgi/libdvbpsi.git/?a=commit;h=f70707bbd022f88a12cacf83b57dcdf07c6dd57a
---

 examples/dvbinfo/tcp.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/examples/dvbinfo/tcp.c b/examples/dvbinfo/tcp.c
index cae9849..280439f 100644
--- a/examples/dvbinfo/tcp.c
+++ b/examples/dvbinfo/tcp.c
@@ -126,7 +126,7 @@ int tcp_open(const char *ipaddress, int port)
         sflags = SOCK_CLOEXEC;
 #endif
         s_ctl = socket(ptr->ai_family, ptr->ai_socktype | sflags, ptr->ai_protocol);
-        if (s_ctl <= 0)
+        if (s_ctl < 0)
         {
             perror("tcp socket error");
             continue;
@@ -136,6 +136,7 @@ int tcp_open(const char *ipaddress, int port)
         if (!set_fdsocketclosexec(s_ctl))
         {
             close(s_ctl);
+            s_ctl = -1;
             continue;
         }
 #endif
@@ -146,6 +147,7 @@ int tcp_open(const char *ipaddress, int port)
         if (result < 0)
         {
             close(s_ctl);
+            s_ctl = -1;
             perror( "tcp connect error" );
             continue;
         }



More information about the libdvbpsi-devel mailing list