This is an automated email from the ASF dual-hosted git repository.

rrm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 4260241  cppcheck: fixes issues found in proxy/logging
4260241 is described below

commit 4260241bdc6f8ebd917920b0972b9e528b096243
Author: Randall Meyer <[email protected]>
AuthorDate: Wed Apr 24 09:44:54 2019 +0800

    cppcheck: fixes issues found in proxy/logging
    
    (style) The scope of the variable 'X' can be reduced.
    (style) Variable 'size' is reassigned a value before the old one has been 
used.
---
 proxy/logging/LogFieldAliasMap.cc |  3 +--
 proxy/logging/LogSock.cc          | 20 +++++++++-----------
 proxy/logging/LogUtils.cc         | 11 ++++++-----
 3 files changed, 16 insertions(+), 18 deletions(-)

diff --git a/proxy/logging/LogFieldAliasMap.cc 
b/proxy/logging/LogFieldAliasMap.cc
index 4e186d0..caf68a9 100644
--- a/proxy/logging/LogFieldAliasMap.cc
+++ b/proxy/logging/LogFieldAliasMap.cc
@@ -44,7 +44,6 @@ LogFieldAliasTable::init(size_t numPairs, ...)
   size_t n;
   va_list ap;
   va_start(ap, numPairs);
-  char *name;
 
   /* A note on the varargs -
      Although IntType is used internally the compiler doesn't know that
@@ -73,7 +72,7 @@ LogFieldAliasTable::init(size_t numPairs, ...)
   for (n = 0; n < numPairs; n++) {
     IntType val = va_arg(ap, int);
     size_t i    = val - m_min;
-    name        = va_arg(ap, char *);
+    char *name  = va_arg(ap, char *);
 
     m_table[i].name   = ats_strdup(name);
     m_table[i].length = strlen(name);
diff --git a/proxy/logging/LogSock.cc b/proxy/logging/LogSock.cc
index 7746b96..bf9a9cd 100644
--- a/proxy/logging/LogSock.cc
+++ b/proxy/logging/LogSock.cc
@@ -81,7 +81,6 @@ int
 LogSock::listen(int accept_port, int family)
 {
   IpEndpoint bind_addr;
-  int size = sizeof(bind_addr);
   char this_host[MAXDNAME];
   int ret;
   ats_scoped_fd accept_sd;
@@ -95,7 +94,8 @@ LogSock::listen(int accept_port, int family)
     return -1;
   }
   bind_addr.port() = htons(accept_port);
-  size             = ats_ip_size(&bind_addr.sa);
+
+  int size = ats_ip_size(&bind_addr.sa);
 
   //
   // create the socket for accepting new connections
@@ -287,7 +287,7 @@ LogSock::connect(sockaddr const *ip)
 bool
 LogSock::pending_data(int *cid, int timeout_msec, bool include_connects)
 {
-  int start_index, ret, n_poll_fds, i;
+  int ret, n_poll_fds, i;
   static struct pollfd fds[LS_CONST_MAX_CONNS];
   int fd_to_cid[LS_CONST_MAX_CONNS];
 
@@ -312,7 +312,7 @@ LogSock::pending_data(int *cid, int timeout_msec, bool 
include_connects)
     n_poll_fds     = 1;
 
   } else { // look for data on any INCOMING socket
-
+    int start_index;
     if (include_connects) {
       start_index = 0;
     } else {
@@ -561,8 +561,6 @@ LogSock::read_alloc(int cid, int *size)
 bool
 LogSock::is_connected(int cid, bool ping) const
 {
-  int i, j, flags;
-
   ink_assert(cid >= 0 && cid < m_max_connections);
 
   if (ct[cid].state == LogSock::LS_STATE_UNUSED) {
@@ -570,9 +568,10 @@ LogSock::is_connected(int cid, bool ping) const
   }
 
   if (ping) {
-    flags = fcntl(ct[cid].sd, F_GETFL);
+    int flags = fcntl(ct[cid].sd, F_GETFL);
     ::fcntl(ct[cid].sd, F_SETFL, O_NONBLOCK);
-    j = ::recv(ct[cid].sd, (char *)&i, sizeof(int), MSG_PEEK);
+    int i;
+    int j = ::recv(ct[cid].sd, (char *)&i, sizeof(int), MSG_PEEK);
     ::fcntl(ct[cid].sd, F_SETFL, flags);
     if (j != 0) {
       return true;
@@ -723,11 +722,10 @@ LogSock::read_body(int sd, void *buf, int bytes)
   }
 
   unsigned bytes_left = bytes;
-  unsigned bytes_read;
-  char *to = (char *)buf;
+  char *to            = (char *)buf;
 
   while (bytes_left) {
-    bytes_read = ::recv(sd, to, bytes_left, 0);
+    unsigned bytes_read = ::recv(sd, to, bytes_left, 0);
     to += bytes_read;
     bytes_left -= bytes_read;
   }
diff --git a/proxy/logging/LogUtils.cc b/proxy/logging/LogUtils.cc
index 49587c4..61f2891 100644
--- a/proxy/logging/LogUtils.cc
+++ b/proxy/logging/LogUtils.cc
@@ -99,12 +99,11 @@ char *
 LogUtils::timestamp_to_netscape_str(long timestamp)
 {
   static char timebuf[64]; // NOTE: not MT safe
-  static char gmtstr[16];
   static long last_timestamp = 0;
-  static char bad_time[]     = "Bad timestamp";
 
   // safety check
   if (timestamp < 0) {
+    static char bad_time[] = "Bad timestamp";
     return bad_time;
   }
   //
@@ -134,6 +133,8 @@ LogUtils::timestamp_to_netscape_str(long timestamp)
       offset = zone / -60;
       sign   = '+';
     }
+
+    static char gmtstr[16];
     int glen = snprintf(gmtstr, 16, "%c%.2d%.2d", sign, offset / 60, offset % 
60);
 
     strftime(timebuf, 64 - glen, "%d/%b/%Y:%H:%M:%S ", tms);
@@ -155,10 +156,10 @@ LogUtils::timestamp_to_date_str(long timestamp)
 {
   static char timebuf[64]; // NOTE: not MT safe
   static long last_timestamp = 0;
-  static char bad_time[]     = "Bad timestamp";
 
   // safety check
   if (timestamp < 0) {
+    static char bad_time[] = "Bad timestamp";
     return bad_time;
   }
   //
@@ -187,10 +188,10 @@ LogUtils::timestamp_to_time_str(long timestamp)
 {
   static char timebuf[64]; // NOTE: not MT safe
   static long last_timestamp = 0;
-  static char bad_time[]     = "Bad timestamp";
 
   // safety check
   if (timestamp < 0) {
+    static char bad_time[] = "Bad timestamp";
     return bad_time;
   }
   //
@@ -221,13 +222,13 @@ void
 LogUtils::manager_alarm(LogUtils::AlarmType alarm_type, const char *msg, ...)
 {
   char msg_buf[LOG_MAX_FORMATTED_LINE];
-  va_list ap;
 
   ink_assert(alarm_type >= 0 && alarm_type < LogUtils::LOG_ALARM_N_TYPES);
 
   if (msg == nullptr) {
     snprintf(msg_buf, sizeof(msg_buf), "No Message");
   } else {
+    va_list ap;
     va_start(ap, msg);
     vsnprintf(msg_buf, LOG_MAX_FORMATTED_LINE, msg, ap);
     va_end(ap);

Reply via email to