libavformat/tcp.c:98: error: Resource Leak
  resource acquired to `return` by call to `socket()` at line 59, column 10 is not released after line 98, column 9.

libavformat/tcp.c:35:1: start of procedure tcp_open()
33. 
34. /* return non zero if error */
35. static int tcp_open(URLContext *h, const char *uri, int flags)
    ^
36. {
37.     struct sockaddr_in dest_addr;

libavformat/tcp.c:38:5: 
36. {
37.     struct sockaddr_in dest_addr;
38.     int port, fd = -1;
        ^
39.     TCPContext *s = NULL;
40.     fd_set wfds;

libavformat/tcp.c:39:5: 
37.     struct sockaddr_in dest_addr;
38.     int port, fd = -1;
39.     TCPContext *s = NULL;
        ^
40.     fd_set wfds;
41.     int fd_max, ret;

libavformat/tcp.c:46:9: 
44.     char hostname[1024],proto[1024],path[1024];
45. 
46.     if(!ff_network_init())
            ^
47.         return AVERROR(EIO);
48. 

libavformat/network.h:49:1: start of procedure ff_network_init()
  47. int ff_socket_nonblock(int socket, int enable);
  48. 
  49. static inline int ff_network_init(void)
      ^
  50. {
  51. #if HAVE_WINSOCK2_H

libavformat/network.h:56:5: 
  54.         return 0;
  55. #endif
  56.     return 1;
          ^
  57. }
  58. 

libavformat/network.h:57:1: return from a call to ff_network_init
  55. #endif
  56.     return 1;
  57. }
      ^
  58. 
  59. static inline void ff_network_close(void)

libavformat/tcp.c:46:9: Taking false branch
44.     char hostname[1024],proto[1024],path[1024];
45. 
46.     if(!ff_network_init())
            ^
47.         return AVERROR(EIO);
48. 

libavformat/tcp.c:49:5: Skipping url_split(): empty list of specs
47.         return AVERROR(EIO);
48. 
49.     url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
        ^
50.         &port, path, sizeof(path), uri);
51.     if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)

libavformat/tcp.c:51:9: Taking false branch
49.     url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
50.         &port, path, sizeof(path), uri);
51.     if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
            ^
52.         return AVERROR(EINVAL);
53. 

libavformat/tcp.c:51:32: Taking false branch
49.     url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
50.         &port, path, sizeof(path), uri);
51.     if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
                                   ^
52.         return AVERROR(EINVAL);
53. 

libavformat/tcp.c:51:45: Taking false branch
49.     url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
50.         &port, path, sizeof(path), uri);
51.     if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
                                                ^
52.         return AVERROR(EINVAL);
53. 

libavformat/tcp.c:54:5: 
52.         return AVERROR(EINVAL);
53. 
54.     dest_addr.sin_family = AF_INET;
        ^
55.     dest_addr.sin_port = htons(port);
56.     if (resolve_host(&dest_addr.sin_addr, hostname) < 0)

libavformat/tcp.c:55:5: Skipping htons(): method has no implementation
53. 
54.     dest_addr.sin_family = AF_INET;
55.     dest_addr.sin_port = htons(port);
        ^
56.     if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
57.         return AVERROR(EIO);

libavformat/tcp.c:56:9: 
54.     dest_addr.sin_family = AF_INET;
55.     dest_addr.sin_port = htons(port);
56.     if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
            ^
57.         return AVERROR(EIO);
58. 

libavformat/os_support.c:64:1: start of procedure resolve_host()
  62. 
  63. /* resolve host with also IP address parsing */
  64. int resolve_host(struct in_addr *sin_addr, const char *hostname)
      ^
  65. {
  66.     struct hostent *hp;

libavformat/os_support.c:68:10: Taking true branch
  66.     struct hostent *hp;
  67. 
  68.     if (!inet_aton(hostname, sin_addr)) {
               ^
  69.         hp = gethostbyname(hostname);
  70.         if (!hp)

libavformat/os_support.c:69:9: Skipping gethostbyname(): method has no implementation
  67. 
  68.     if (!inet_aton(hostname, sin_addr)) {
  69.         hp = gethostbyname(hostname);
              ^
  70.         if (!hp)
  71.             return -1;

libavformat/os_support.c:70:14: Taking false branch
  68.     if (!inet_aton(hostname, sin_addr)) {
  69.         hp = gethostbyname(hostname);
  70.         if (!hp)
                   ^
  71.             return -1;
  72.         memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));

libavformat/os_support.c:72:9: 
  70.         if (!hp)
  71.             return -1;
  72.         memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
              ^
  73.     }
  74.     return 0;

libavformat/os_support.c:74:5: 
  72.         memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
  73.     }
  74.     return 0;
          ^
  75. }
  76. 

libavformat/os_support.c:75:1: return from a call to resolve_host
  73.     }
  74.     return 0;
  75. }
      ^
  76. 
  77. int ff_socket_nonblock(int socket, int enable)

libavformat/tcp.c:56:9: Taking false branch
54.     dest_addr.sin_family = AF_INET;
55.     dest_addr.sin_port = htons(port);
56.     if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
            ^
57.         return AVERROR(EIO);
58. 

libavformat/tcp.c:59:5: 
57.         return AVERROR(EIO);
58. 
59.     fd = socket(AF_INET, SOCK_STREAM, 0);
        ^
60.     if (fd < 0)
61.         return AVERROR(EIO);

libavformat/tcp.c:60:9: Taking false branch
58. 
59.     fd = socket(AF_INET, SOCK_STREAM, 0);
60.     if (fd < 0)
            ^
61.         return AVERROR(EIO);
62.     ff_socket_nonblock(fd, 1);

libavformat/tcp.c:62:5: 
60.     if (fd < 0)
61.         return AVERROR(EIO);
62.     ff_socket_nonblock(fd, 1);
        ^
63. 
64.  redo:

libavformat/os_support.c:77:1: start of procedure ff_socket_nonblock()
  75. }
  76. 
  77. int ff_socket_nonblock(int socket, int enable)
      ^
  78. {
  79. #if HAVE_WINSOCK2_H

libavformat/os_support.c:82:8: Taking true branch
  80.    return ioctlsocket(socket, FIONBIO, &enable);
  81. #else
  82.    if (enable)
             ^
  83.       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
  84.    else

libavformat/os_support.c:83:7: 
  81. #else
  82.    if (enable)
  83.       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
            ^
  84.    else
  85.       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);

libavformat/os_support.c:87:1: return from a call to ff_socket_nonblock
  85.       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
  86. #endif
  87. }
      ^
  88. #endif /* CONFIG_NETWORK */
  89. 

libavformat/tcp.c:69:13: 
67.     if (ret < 0) {
68.         if (ff_neterrno() == FF_NETERROR(EINTR))
69.             goto redo;
                ^
70.         if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
71.             ff_neterrno() != FF_NETERROR(EAGAIN))

libavformat/tcp.c:65:5: Skipping connect(): method has no implementation
63. 
64.  redo:
65.     ret = connect(fd, (struct sockaddr *)&dest_addr,
        ^
66.                   sizeof(dest_addr));
67.     if (ret < 0) {

libavformat/tcp.c:67:9: Taking true branch
65.     ret = connect(fd, (struct sockaddr *)&dest_addr,
66.                   sizeof(dest_addr));
67.     if (ret < 0) {
            ^
68.         if (ff_neterrno() == FF_NETERROR(EINTR))
69.             goto redo;

libavformat/tcp.c:68:13: Taking false branch
66.                   sizeof(dest_addr));
67.     if (ret < 0) {
68.         if (ff_neterrno() == FF_NETERROR(EINTR))
                ^
69.             goto redo;
70.         if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&

libavformat/tcp.c:70:13: Taking false branch
68.         if (ff_neterrno() == FF_NETERROR(EINTR))
69.             goto redo;
70.         if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
                ^
71.             ff_neterrno() != FF_NETERROR(EAGAIN))
72.             goto fail;

libavformat/tcp.c:88:9: Loop condition is true. Entering loop body
86.             if (ret > 0 && FD_ISSET(fd, &wfds))
87.                 break;
88.         }
            ^
89. 
90.         /* test error */

libavformat/tcp.c:76:17: Taking false branch
74.         /* wait until we are connected or until abort */
75.         for(;;) {
76.             if (url_interrupt_cb()) {
                    ^
77.                 ret = AVERROR(EINTR);
78.                 goto fail1;

libavformat/tcp.c:80:13: 
78.                 goto fail1;
79.             }
80.             fd_max = fd;
                ^
81.             FD_ZERO(&wfds);
82.             FD_SET(fd, &wfds);

libavformat/tcp.c:81:13: Loop condition is true. Entering loop body
79.             }
80.             fd_max = fd;
81.             FD_ZERO(&wfds);
                ^
82.             FD_SET(fd, &wfds);
83.             tv.tv_sec = 0;

libavformat/tcp.c:81:13: Loop condition is false. Leaving loop
79.             }
80.             fd_max = fd;
81.             FD_ZERO(&wfds);
                ^
82.             FD_SET(fd, &wfds);
83.             tv.tv_sec = 0;

libavformat/tcp.c:81:13: Loop condition is false. Leaving loop
79.             }
80.             fd_max = fd;
81.             FD_ZERO(&wfds);
                ^
82.             FD_SET(fd, &wfds);
83.             tv.tv_sec = 0;

libavformat/tcp.c:82:13: 
80.             fd_max = fd;
81.             FD_ZERO(&wfds);
82.             FD_SET(fd, &wfds);
                ^
83.             tv.tv_sec = 0;
84.             tv.tv_usec = 100 * 1000;

libavformat/tcp.c:83:13: 
81.             FD_ZERO(&wfds);
82.             FD_SET(fd, &wfds);
83.             tv.tv_sec = 0;
                ^
84.             tv.tv_usec = 100 * 1000;
85.             ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);

libavformat/tcp.c:84:13: 
82.             FD_SET(fd, &wfds);
83.             tv.tv_sec = 0;
84.             tv.tv_usec = 100 * 1000;
                ^
85.             ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
86.             if (ret > 0 && FD_ISSET(fd, &wfds))

libavformat/tcp.c:85:13: Skipping select(): method has no implementation
83.             tv.tv_sec = 0;
84.             tv.tv_usec = 100 * 1000;
85.             ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
                ^
86.             if (ret > 0 && FD_ISSET(fd, &wfds))
87.                 break;

libavformat/tcp.c:86:17: Taking true branch
84.             tv.tv_usec = 100 * 1000;
85.             ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
86.             if (ret > 0 && FD_ISSET(fd, &wfds))
                    ^
87.                 break;
88.         }

libavformat/tcp.c:86:28: Taking true branch
84.             tv.tv_usec = 100 * 1000;
85.             ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
86.             if (ret > 0 && FD_ISSET(fd, &wfds))
                               ^
87.                 break;
88.         }

libavformat/tcp.c:91:9: 
89. 
90.         /* test error */
91.         optlen = sizeof(ret);
            ^
92.         getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
93.         if (ret != 0)

libavformat/tcp.c:92:9: Skipping getsockopt(): method has no implementation
90.         /* test error */
91.         optlen = sizeof(ret);
92.         getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
            ^
93.         if (ret != 0)
94.             goto fail;

libavformat/tcp.c:93:13: Taking false branch
91.         optlen = sizeof(ret);
92.         getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
93.         if (ret != 0)
                ^
94.             goto fail;
95.     }

libavformat/tcp.c:96:5: 
94.             goto fail;
95.     }
96.     s = av_malloc(sizeof(TCPContext));
        ^
97.     if (!s)
98.         return AVERROR(ENOMEM);

libavutil/mem.c:47:1: start of procedure av_malloc()
  45.    linker will do it automatically. */
  46. 
  47. void *av_malloc(unsigned int size)
      ^
  48. {
  49.     void *ptr = NULL;

libavutil/mem.c:49:5: 
  47. void *av_malloc(unsigned int size)
  48. {
  49.     void *ptr = NULL;
          ^
  50. #if CONFIG_MEMALIGN_HACK
  51.     long diff;

libavutil/mem.c:55:8: Taking false branch
  53. 
  54.     /* let's disallow possible ambiguous cases */
  55.     if(size > (INT_MAX-16) )
             ^
  56.         return NULL;
  57. 

libavutil/mem.c:66:9: Taking false branch
  64.     ((char*)ptr)[-1]= diff;
  65. #elif HAVE_POSIX_MEMALIGN
  66.     if (posix_memalign(&ptr,16,size))
              ^
  67.         ptr = NULL;
  68. #elif HAVE_MEMALIGN

libavutil/mem.c:99:5: 
   97.     ptr = malloc(size);
   98. #endif
   99.     return ptr;
           ^
  100. }
  101. 

libavutil/mem.c:100:1: return from a call to av_malloc
   98. #endif
   99.     return ptr;
  100. }
       ^
  101. 
  102. void *av_realloc(void *ptr, unsigned int size)

libavformat/tcp.c:97:10: Taking true branch
95.     }
96.     s = av_malloc(sizeof(TCPContext));
97.     if (!s)
             ^
98.         return AVERROR(ENOMEM);
99.     h->priv_data = s;

libavformat/tcp.c:98:9: 
 96.     s = av_malloc(sizeof(TCPContext));
 97.     if (!s)
 98.         return AVERROR(ENOMEM);
             ^
 99.     h->priv_data = s;
100.     h->is_streamed = 1;

static int tcp_open(URLContext *h, const char *uri, int flags)
{
    struct sockaddr_in dest_addr;
    int port, fd = -1;
    TCPContext *s = NULL;
    fd_set wfds;
    int fd_max, ret;
    struct timeval tv;
    socklen_t optlen;
    char hostname[1024],proto[1024],path[1024];
    if(!ff_network_init())
        return AVERROR(EIO);
    url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
        &port, path, sizeof(path), uri);
    if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
        return AVERROR(EINVAL);
    dest_addr.sin_family = AF_INET;
    dest_addr.sin_port = htons(port);
    if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
        return AVERROR(EIO);
    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0)
        return AVERROR(EIO);
    ff_socket_nonblock(fd, 1);
 redo:
    ret = connect(fd, (struct sockaddr *)&dest_addr,
                  sizeof(dest_addr));
    if (ret < 0) {
        if (ff_neterrno() == FF_NETERROR(EINTR))
            goto redo;
        if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
            ff_neterrno() != FF_NETERROR(EAGAIN))
            goto fail;
        for(;;) {
            if (url_interrupt_cb()) {
                ret = AVERROR(EINTR);
                goto fail1;
            }
            fd_max = fd;
            FD_ZERO(&wfds);
            FD_SET(fd, &wfds);
            tv.tv_sec = 0;
            tv.tv_usec = 100 * 1000;
            ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
            if (ret > 0 && FD_ISSET(fd, &wfds))
                break;
        }
        optlen = sizeof(ret);
        getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
        if (ret != 0)
            goto fail;
    }
    s = av_malloc(sizeof(TCPContext));
    if (!s)
        return AVERROR(ENOMEM);
    h->priv_data = s;
    h->is_streamed = 1;
    s->fd = fd;
    return 0;
 fail:
    ret = AVERROR(EIO);
 fail1:
    if (fd >= 0)
        closesocket(fd);
    return ret;
}
['static int tcp_open(URLContext *h, const char *uri, int flags)\n{\n    struct sockaddr_in dest_addr;\n    int port, fd = -1;\n    TCPContext *s = NULL;\n    fd_set wfds;\n    int fd_max, ret;\n    struct timeval tv;\n    socklen_t optlen;\n    char hostname[1024],proto[1024],path[1024];\n    if(!ff_network_init())\n        return AVERROR(EIO);\n    url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),\n        &port, path, sizeof(path), uri);\n    if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)\n        return AVERROR(EINVAL);\n    dest_addr.sin_family = AF_INET;\n    dest_addr.sin_port = htons(port);\n    if (resolve_host(&dest_addr.sin_addr, hostname) < 0)\n        return AVERROR(EIO);\n    fd = socket(AF_INET, SOCK_STREAM, 0);\n    if (fd < 0)\n        return AVERROR(EIO);\n    ff_socket_nonblock(fd, 1);\n redo:\n    ret = connect(fd, (struct sockaddr *)&dest_addr,\n                  sizeof(dest_addr));\n    if (ret < 0) {\n        if (ff_neterrno() == FF_NETERROR(EINTR))\n            goto redo;\n        if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&\n            ff_neterrno() != FF_NETERROR(EAGAIN))\n            goto fail;\n        for(;;) {\n            if (url_interrupt_cb()) {\n                ret = AVERROR(EINTR);\n                goto fail1;\n            }\n            fd_max = fd;\n            FD_ZERO(&wfds);\n            FD_SET(fd, &wfds);\n            tv.tv_sec = 0;\n            tv.tv_usec = 100 * 1000;\n            ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);\n            if (ret > 0 && FD_ISSET(fd, &wfds))\n                break;\n        }\n        optlen = sizeof(ret);\n        getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);\n        if (ret != 0)\n            goto fail;\n    }\n    s = av_malloc(sizeof(TCPContext));\n    if (!s)\n        return AVERROR(ENOMEM);\n    h->priv_data = s;\n    h->is_streamed = 1;\n    s->fd = fd;\n    return 0;\n fail:\n    ret = AVERROR(EIO);\n fail1:\n    if (fd >= 0)\n        closesocket(fd);\n    return ret;\n}', 'static inline int ff_network_init(void)\n{\n#if HAVE_WINSOCK2_H\n    WSADATA wsaData;\n    if (WSAStartup(MAKEWORD(1,1), &wsaData))\n        return 0;\n#endif\n    return 1;\n}', 'int resolve_host(struct in_addr *sin_addr, const char *hostname)\n{\n    struct hostent *hp;\n    if (!inet_aton(hostname, sin_addr)) {\n        hp = gethostbyname(hostname);\n        if (!hp)\n            return -1;\n        memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));\n    }\n    return 0;\n}', 'int ff_socket_nonblock(int socket, int enable)\n{\n#if HAVE_WINSOCK2_H\n   return ioctlsocket(socket, FIONBIO, &enable);\n#else\n   if (enable)\n      return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);\n   else\n      return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);\n#endif\n}', 'void *av_malloc(unsigned int size)\n{\n    void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n    long diff;\n#endif\n    if(size > (INT_MAX-16) )\n        return NULL;\n#if CONFIG_MEMALIGN_HACK\n    ptr = malloc(size+16);\n    if(!ptr)\n        return ptr;\n    diff= ((-(long)ptr - 1)&15) + 1;\n    ptr = (char*)ptr + diff;\n    ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n    if (posix_memalign(&ptr,16,size))\n        ptr = NULL;\n#elif HAVE_MEMALIGN\n    ptr = memalign(16,size);\n#else\n    ptr = malloc(size);\n#endif\n    return ptr;\n}']