[nginx] Syslog: fixed possible resource leak and more verbose lo...

Homutov Vladimir vl at nginx.com
Tue May 27 10:53:56 UTC 2014


details:   http://hg.nginx.org/nginx/rev/aacd994167d3
branches:  
changeset: 5708:aacd994167d3
user:      Vladimir Homutov <vl at nginx.com>
date:      Mon May 26 23:34:44 2014 +0400
description:
Syslog: fixed possible resource leak and more verbose logging.

Found by Coverity (CID 1215646).

diffstat:

 src/core/ngx_syslog.c |  34 ++++++++++++++++++++++------------
 1 files changed, 22 insertions(+), 12 deletions(-)

diffs (66 lines):

diff -r 31dd63dcb9ea -r aacd994167d3 src/core/ngx_syslog.c
--- a/src/core/ngx_syslog.c	Fri May 23 16:37:05 2014 +0400
+++ b/src/core/ngx_syslog.c	Mon May 26 23:34:44 2014 +0400
@@ -289,14 +289,6 @@ ngx_syslog_init_peer(ngx_syslog_peer_t *
 
     ngx_syslog_dummy_event.log = &ngx_syslog_dummy_log;
 
-    cln = ngx_pool_cleanup_add(peer->pool, 0);
-    if (cln == NULL) {
-        return NGX_ERROR;
-    }
-
-    cln->data = peer;
-    cln->handler = ngx_syslog_cleanup;
-
     fd = ngx_socket(peer->server.sockaddr->sa_family, SOCK_DGRAM, 0);
     if (fd == (ngx_socket_t) -1) {
         ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno,
@@ -307,17 +299,34 @@ ngx_syslog_init_peer(ngx_syslog_peer_t *
     if (ngx_nonblocking(fd) == -1) {
         ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno,
                       ngx_nonblocking_n " failed");
-        return NGX_ERROR;
+        goto failed;
     }
 
     if (connect(fd, peer->server.sockaddr, peer->server.socklen) == -1) {
         ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno,
                       "connect() failed");
-        return NGX_ERROR;
+        goto failed;
     }
 
+    cln = ngx_pool_cleanup_add(peer->pool, 0);
+    if (cln == NULL) {
+        goto failed;
+    }
+
+    cln->data = peer;
+    cln->handler = ngx_syslog_cleanup;
+
     peer->conn.fd = fd;
     return NGX_OK;
+
+failed:
+
+    if (ngx_close_socket(fd) == -1) {
+        ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno,
+                      ngx_close_socket_n " failed");
+    }
+
+    return NGX_ERROR;
 }
 
 
@@ -326,7 +335,8 @@ ngx_syslog_cleanup(void *data)
 {
     ngx_syslog_peer_t  *peer = data;
 
-    if (peer->conn.fd != (ngx_socket_t) -1) {
-        (void) ngx_close_socket(peer->conn.fd);
+    if (ngx_close_socket(peer->conn.fd) == -1) {
+        ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno,
+                      ngx_close_socket_n " failed");
     }
 }



More information about the nginx-devel mailing list