You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by bc...@apache.org on 2019/04/24 02:57:11 UTC
[trafficserver] branch master updated: cppcheck: Reduces variable
scope for files in mgmt/...
This is an automated email from the ASF dual-hosted git repository.
bcall 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 4338d9e cppcheck: Reduces variable scope for files in mgmt/...
4338d9e is described below
commit 4338d9efe21c9b6585b1d41cc3a16d4d0589b8b5
Author: Randall Meyer <ra...@yahoo.com>
AuthorDate: Wed Apr 24 07:39:12 2019 +0800
cppcheck: Reduces variable scope for files in mgmt/...
The scope of the variable 'X' can be reduced
---
mgmt/LocalManager.cc | 13 ++++---------
mgmt/ProxyConfig.cc | 5 ++---
mgmt/Rollback.cc | 28 +++++++++++-----------------
mgmt/WebMgmtUtils.cc | 15 +++++----------
mgmt/api/APITestCliRemote.cc | 9 +++------
mgmt/api/CoreAPI.cc | 7 ++-----
mgmt/api/EventCallback.cc | 4 +---
mgmt/api/EventControlMain.cc | 8 ++------
mgmt/api/INKMgmtAPI.cc | 23 +++++++----------------
mgmt/api/NetworkUtilsRemote.cc | 3 +--
mgmt/api/TSControlMain.cc | 3 +--
mgmt/utils/MgmtUtils.cc | 16 +++++++---------
12 files changed, 46 insertions(+), 88 deletions(-)
diff --git a/mgmt/LocalManager.cc b/mgmt/LocalManager.cc
index 8caf6f0..fae81d3 100644
--- a/mgmt/LocalManager.cc
+++ b/mgmt/LocalManager.cc
@@ -312,7 +312,6 @@ LocalManager::initMgmtProcessServer()
void
LocalManager::pollMgmtProcessServer()
{
- int num;
struct timeval timeout;
fd_set fdlist;
@@ -355,7 +354,7 @@ LocalManager::pollMgmtProcessServer()
}
#endif
- num = mgmt_select(FD_SETSIZE, &fdlist, nullptr, nullptr, &timeout);
+ int num = mgmt_select(FD_SETSIZE, &fdlist, nullptr, nullptr, &timeout);
switch (num) {
case 0:
@@ -402,16 +401,14 @@ LocalManager::pollMgmtProcessServer()
if (ts::NO_FD != watched_process_fd && FD_ISSET(watched_process_fd, &fdlist)) {
int res;
MgmtMessageHdr mh_hdr;
- MgmtMessageHdr *mh_full;
- char *data_raw;
keep_polling = true;
// read the message
if ((res = mgmt_read_pipe(watched_process_fd, (char *)&mh_hdr, sizeof(MgmtMessageHdr))) > 0) {
- mh_full = (MgmtMessageHdr *)alloca(sizeof(MgmtMessageHdr) + mh_hdr.data_len);
+ MgmtMessageHdr *mh_full = (MgmtMessageHdr *)alloca(sizeof(MgmtMessageHdr) + mh_hdr.data_len);
memcpy(mh_full, &mh_hdr, sizeof(MgmtMessageHdr));
- data_raw = (char *)mh_full + sizeof(MgmtMessageHdr);
+ char *data_raw = (char *)mh_full + sizeof(MgmtMessageHdr);
if ((res = mgmt_read_pipe(watched_process_fd, data_raw, mh_hdr.data_len)) > 0) {
handleMgmtMsgFromProcesses(mh_full);
} else if (res < 0) {
@@ -781,10 +778,8 @@ LocalManager::signalEvent(int msg_id, const char *data_raw, int data_len)
void
LocalManager::processEventQueue()
{
- bool handled_by_mgmt;
-
while (!this->queue_empty()) {
- handled_by_mgmt = false;
+ bool handled_by_mgmt = false;
MgmtMessageHdr *mh = this->dequeue();
auto payload = mh->payload();
diff --git a/mgmt/ProxyConfig.cc b/mgmt/ProxyConfig.cc
index 2753ca4..9c189ca 100644
--- a/mgmt/ProxyConfig.cc
+++ b/mgmt/ProxyConfig.cc
@@ -69,13 +69,12 @@ config_string_alloc_cb(void *data, void *value)
char *_ss = (char *)value;
char *_new_value = nullptr;
-//#define DEBUG_CONFIG_STRING_UPDATE
#if defined(DEBUG_CONFIG_STRING_UPDATE)
printf("config callback [new, old] = [%s : %s]\n", (_ss) ? (_ss) : (""), (*(char **)data) ? (*(char **)data) : (""));
#endif
- int len = -1;
+
if (_ss) {
- len = strlen(_ss);
+ int len = strlen(_ss);
_new_value = (char *)ats_malloc(len + 1);
memcpy(_new_value, _ss, len + 1);
}
diff --git a/mgmt/Rollback.cc b/mgmt/Rollback.cc
index 0fcc8e7..05e4c42 100644
--- a/mgmt/Rollback.cc
+++ b/mgmt/Rollback.cc
@@ -72,11 +72,6 @@ Rollback::Rollback(const char *fileName_, const char *configName_, bool root_acc
int testFD; // For open test
int testErrno; // For open test
- // In case the file is missing
- char *highestSeenStr;
- char *activeVerStr;
- bool needZeroLength;
-
ink_assert(fileName_ != nullptr);
// parent must not also have a parent
@@ -133,11 +128,12 @@ Rollback::Rollback(const char *fileName_, const char *configName_, bool root_acc
// If it does not, create a zero length file to prevent total havoc
//
if (errno == ENOENT) {
+ bool needZeroLength;
mgmt_log("[RollBack::Rollback] Missing Configuration File: %s\n", fileName);
if (highestSeen > 0) {
- highestSeenStr = createPathStr(highestSeen);
- activeVerStr = createPathStr(ACTIVE_VERSION);
+ char *highestSeenStr = createPathStr(highestSeen);
+ char *activeVerStr = createPathStr(ACTIVE_VERSION);
if (rename(highestSeenStr, activeVerStr) < 0) {
mgmt_log("[RollBack::Rollback] Automatic Rollback to prior version failed for %s : %s\n", fileName, strerror(errno));
@@ -687,7 +683,7 @@ Rollback::findVersions_ml(ExpandingArray *listNames)
version_t
Rollback::extractVersionInfo(ExpandingArray *listNames, const char *testFileName)
{
- const char *currentVersionStr, *str;
+ const char *str;
version_t version = INVALID_VERSION;
// Check to see if the current entry is a rollback file
@@ -700,7 +696,7 @@ Rollback::extractVersionInfo(ExpandingArray *listNames, const char *testFileName
// Check for the underscore
if (*(testFileName + fileNameLen) == '_') {
// Check for the integer version number
- currentVersionStr = str = testFileName + fileNameLen + 1;
+ const char *currentVersionStr = str = testFileName + fileNameLen + 1;
for (; isdigit(*str) && *str != '\0'; str++) {
;
@@ -714,12 +710,11 @@ Rollback::extractVersionInfo(ExpandingArray *listNames, const char *testFileName
// Add info about version number and modTime
if (listNames != nullptr) {
struct stat fileInfo;
- versionInfo *verInfo;
if (statFile(version, &fileInfo) >= 0) {
- verInfo = (versionInfo *)ats_malloc(sizeof(versionInfo));
- verInfo->version = version;
- verInfo->modTime = fileInfo.st_mtime;
+ versionInfo *verInfo = (versionInfo *)ats_malloc(sizeof(versionInfo));
+ verInfo->version = version;
+ verInfo->modTime = fileInfo.st_mtime;
listNames->addEntry((void *)verInfo);
}
}
@@ -744,7 +739,6 @@ Rollback::findVersions_ml(Queue<versionInfo> &q)
ExpandingArray versions(25, true);
int num;
versionInfo *foundVer;
- versionInfo *addInfo;
version_t highest;
// Get the version info and sort it
@@ -757,9 +751,9 @@ Rollback::findVersions_ml(Queue<versionInfo> &q)
foundVer = (versionInfo *)versions[i];
// We need to create our own copy so that
// constructor gets run
- addInfo = new versionInfo;
- addInfo->version = foundVer->version;
- addInfo->modTime = foundVer->modTime;
+ versionInfo *addInfo = new versionInfo;
+ addInfo->version = foundVer->version;
+ addInfo->modTime = foundVer->modTime;
q.enqueue(addInfo);
}
diff --git a/mgmt/WebMgmtUtils.cc b/mgmt/WebMgmtUtils.cc
index 474114c..fa7fd9e 100644
--- a/mgmt/WebMgmtUtils.cc
+++ b/mgmt/WebMgmtUtils.cc
@@ -519,7 +519,6 @@ bytesFromInt(RecInt bytes, char *bufVal)
const int64_t gb = 1073741824;
const long int mb = 1048576;
const long int kb = 1024;
- int bytesP;
double unitBytes;
if (bytes >= gb) {
@@ -531,7 +530,7 @@ bytesFromInt(RecInt bytes, char *bufVal)
// has plenty of precision for a regular int
// and saves from 64 bit arithmetic which may
// be expensive on some processors
- bytesP = (int)bytes;
+ int bytesP = (int)bytes;
if (bytesP >= mb) {
unitBytes = bytes / (double)mb;
snprintf(bufVal, 15, "%.1f MB", unitBytes);
@@ -1041,9 +1040,6 @@ recordRegexCheck(const char *pattern, const char *value)
bool
recordRangeCheck(const char *pattern, const char *value)
{
- int l_limit;
- int u_limit;
- int val;
char *p = (char *)pattern;
Tokenizer dashTok("-");
@@ -1052,9 +1048,9 @@ recordRangeCheck(const char *pattern, const char *value)
p++;
} // skip to '['
if (dashTok.Initialize(++p, COPY_TOKS) == 2) {
- l_limit = atoi(dashTok[0]);
- u_limit = atoi(dashTok[1]);
- val = atoi(value);
+ int l_limit = atoi(dashTok[0]);
+ int u_limit = atoi(dashTok[1]);
+ int val = atoi(value);
if (val >= l_limit && val <= u_limit) {
return true;
}
@@ -1075,12 +1071,11 @@ recordIPCheck(const char *pattern, const char *value)
Tokenizer dotTok1(".");
Tokenizer dotTok2(".");
- int i;
check = true;
if (recordRegexCheck(range_pattern, pattern) && recordRegexCheck(ip_pattern, value)) {
if (dotTok1.Initialize((char *)pattern, COPY_TOKS) == 4 && dotTok2.Initialize((char *)value, COPY_TOKS) == 4) {
- for (i = 0; i < 4 && check; i++) {
+ for (int i = 0; i < 4 && check; i++) {
if (!recordRangeCheck(dotTok1[i], dotTok2[i])) {
check = false;
}
diff --git a/mgmt/api/APITestCliRemote.cc b/mgmt/api/APITestCliRemote.cc
index 80f8cca..aa1e7a2 100644
--- a/mgmt/api/APITestCliRemote.cc
+++ b/mgmt/api/APITestCliRemote.cc
@@ -126,7 +126,6 @@ void
print_string_list(TSStringList list)
{
int i, count, buf_pos = 0;
- char *str;
char buf[1000];
if (!list) {
@@ -134,7 +133,7 @@ print_string_list(TSStringList list)
}
count = TSStringListLen(list);
for (i = 0; i < count; i++) {
- str = TSStringListDequeue(list);
+ char *str = TSStringListDequeue(list);
snprintf(buf + buf_pos, sizeof(buf) - buf_pos, "%s,", str);
buf_pos = strlen(buf);
TSStringListEnqueue(list, str);
@@ -149,12 +148,11 @@ void
print_int_list(TSIntList list)
{
int i, count, buf_pos = 0;
- int *elem;
char buf[1000];
count = TSIntListLen(list);
for (i = 0; i < count; i++) {
- elem = TSIntListDequeue(list);
+ int *elem = TSIntListDequeue(list);
snprintf(buf + buf_pos, sizeof(buf) - buf_pos, "%d:", *elem);
buf_pos = strlen(buf);
TSIntListEnqueue(list, elem);
@@ -504,7 +502,6 @@ test_rec_get(char *rec_name)
void
test_record_get_mlt()
{
- TSRecordEle *rec_ele;
TSStringList name_list;
TSList rec_list;
int i, num;
@@ -547,7 +544,7 @@ test_record_get_mlt()
}
for (i = 0; i < num; i++) {
- rec_ele = (TSRecordEle *)TSListDequeue(rec_list);
+ TSRecordEle *rec_ele = (TSRecordEle *)TSListDequeue(rec_list);
if (!rec_ele) {
printf("ERROR\n");
break;
diff --git a/mgmt/api/CoreAPI.cc b/mgmt/api/CoreAPI.cc
index d75d766..56da4d4 100644
--- a/mgmt/api/CoreAPI.cc
+++ b/mgmt/api/CoreAPI.cc
@@ -789,9 +789,6 @@ EventResolve(const char *event_name)
TSMgmtError
ActiveEventGetMlt(LLQ *active_events)
{
- int event_id;
- char *event_name;
-
if (!active_events) {
return TS_ERR_PARAMS;
}
@@ -804,8 +801,8 @@ ActiveEventGetMlt(LLQ *active_events)
// iterate through hash-table and insert event_name's into active_events list
for (auto &&it : event_ht) {
// convert key to int; insert into llQ
- event_id = ink_atoi(it.first.c_str());
- event_name = get_event_name(event_id);
+ int event_id = ink_atoi(it.first.c_str());
+ char *event_name = get_event_name(event_id);
if (event_name) {
if (!enqueue(active_events, event_name)) { // returns true if successful
return TS_ERR_FAIL;
diff --git a/mgmt/api/EventCallback.cc b/mgmt/api/EventCallback.cc
index 2fc463a..59bdf93 100644
--- a/mgmt/api/EventCallback.cc
+++ b/mgmt/api/EventCallback.cc
@@ -183,7 +183,6 @@ TSMgmtError
cb_table_register(CallbackTable *cb_table, const char *event_name, TSEventSignalFunc func, void *data, bool *first_cb)
{
bool first_time = false;
- int id;
EventCallbackT *event_cb; // create new EventCallbackT EACH TIME enqueue
// the data and event_name can be NULL
@@ -211,8 +210,7 @@ cb_table_register(CallbackTable *cb_table, const char *event_name, TSEventSignal
enqueue(i, event_cb);
}
} else { // register callback for specific alarm
- // printf("[EventSignalCbRegister] Register callback for %s\n", event_name);
- id = get_event_id(event_name);
+ int id = get_event_id(event_name);
if (id != -1) {
if (!cb_table->event_callback_l[id]) {
cb_table->event_callback_l[id] = create_queue();
diff --git a/mgmt/api/EventControlMain.cc b/mgmt/api/EventControlMain.cc
index 4fc2668..b4859eb 100644
--- a/mgmt/api/EventControlMain.cc
+++ b/mgmt/api/EventControlMain.cc
@@ -177,11 +177,8 @@ delete_event_queue(LLQ *q)
return;
}
- // now for every element, dequeue and free
- TSMgmtEvent *ele;
-
while (!queue_is_empty(q)) {
- ele = (TSMgmtEvent *)dequeue(q);
+ TSMgmtEvent *ele = (TSMgmtEvent *)dequeue(q);
ats_free(ele);
}
@@ -257,7 +254,6 @@ event_callback_main(void *arg)
fd_set selectFDs; // for select call
EventClientT *client_entry; // an entry of fd to alarms mapping
- int fds_ready; // return value for select go here
struct timeval timeout;
while (true) {
@@ -280,7 +276,7 @@ event_callback_main(void *arg)
}
// select call - timeout is set so we can check events at regular intervals
- fds_ready = mgmt_select(FD_SETSIZE, &selectFDs, (fd_set *)nullptr, (fd_set *)nullptr, &timeout);
+ int fds_ready = mgmt_select(FD_SETSIZE, &selectFDs, (fd_set *)nullptr, (fd_set *)nullptr, &timeout);
// check return
if (fds_ready > 0) {
diff --git a/mgmt/api/INKMgmtAPI.cc b/mgmt/api/INKMgmtAPI.cc
index 15d2caf..e332764 100644
--- a/mgmt/api/INKMgmtAPI.cc
+++ b/mgmt/api/INKMgmtAPI.cc
@@ -147,7 +147,6 @@ tsapi bool
TSListIsValid(TSList l)
{
int i, len;
- void *ele;
if (!l) {
return false;
@@ -155,7 +154,7 @@ TSListIsValid(TSList l)
len = queue_len((LLQ *)l);
for (i = 0; i < len; i++) {
- ele = (void *)dequeue((LLQ *)l);
+ void *ele = (void *)dequeue((LLQ *)l);
if (!ele) {
return false;
}
@@ -175,15 +174,13 @@ TSStringListCreate()
tsapi void
TSStringListDestroy(TSStringList strl)
{
- char *str;
-
if (!strl) {
return;
}
/* dequeue each element and free it */
while (!queue_is_empty((LLQ *)strl)) {
- str = (char *)dequeue((LLQ *)strl);
+ char *str = (char *)dequeue((LLQ *)strl);
ats_free(str);
}
@@ -246,7 +243,6 @@ tsapi bool
TSStringListIsValid(TSStringList strl)
{
int i, len;
- char *str;
if (!strl) {
return false;
@@ -254,7 +250,7 @@ TSStringListIsValid(TSStringList strl)
len = queue_len((LLQ *)strl);
for (i = 0; i < len; i++) {
- str = (char *)dequeue((LLQ *)strl);
+ char *str = (char *)dequeue((LLQ *)strl);
if (!str) {
return false;
}
@@ -274,15 +270,13 @@ TSIntListCreate()
tsapi void
TSIntListDestroy(TSIntList intl)
{
- int *iPtr;
-
if (!intl) {
return;
}
/* dequeue each element and free it */
while (!queue_is_empty((LLQ *)intl)) {
- iPtr = (int *)dequeue((LLQ *)intl);
+ int *iPtr = (int *)dequeue((LLQ *)intl);
ats_free(iPtr);
}
@@ -534,8 +528,6 @@ END:
tsapi TSMgmtError
TSRecordGetMlt(TSStringList rec_names, TSList rec_vals)
{
- TSRecordEle *ele;
- char *rec_name;
int num_recs, i, j;
TSMgmtError ret;
@@ -545,12 +537,12 @@ TSRecordGetMlt(TSStringList rec_names, TSList rec_vals)
num_recs = queue_len((LLQ *)rec_names);
for (i = 0; i < num_recs; i++) {
- rec_name = (char *)dequeue((LLQ *)rec_names); // remove name from list
+ char *rec_name = (char *)dequeue((LLQ *)rec_names); // remove name from list
if (!rec_name) {
return TS_ERR_PARAMS; // NULL is invalid record name
}
- ele = TSRecordEleCreate();
+ TSRecordEle *ele = TSRecordEleCreate();
ret = MgmtRecordGet(rec_name, ele);
enqueue((LLQ *)rec_names, rec_name); // return name to list
@@ -632,7 +624,6 @@ tsapi TSMgmtError
TSRecordSetMlt(TSList rec_list, TSActionNeedT *action_need)
{
int num_recs, ret, i;
- TSRecordEle *ele;
TSMgmtError status = TS_ERR_OKAY;
TSActionNeedT top_action_req = TS_ACTION_UNDEFINED;
@@ -643,7 +634,7 @@ TSRecordSetMlt(TSList rec_list, TSActionNeedT *action_need)
num_recs = queue_len((LLQ *)rec_list);
for (i = 0; i < num_recs; i++) {
- ele = (TSRecordEle *)dequeue((LLQ *)rec_list);
+ TSRecordEle *ele = (TSRecordEle *)dequeue((LLQ *)rec_list);
if (ele) {
switch (ele->rec_type) {
case TS_REC_INT:
diff --git a/mgmt/api/NetworkUtilsRemote.cc b/mgmt/api/NetworkUtilsRemote.cc
index 4c75f80..35e731b 100644
--- a/mgmt/api/NetworkUtilsRemote.cc
+++ b/mgmt/api/NetworkUtilsRemote.cc
@@ -507,7 +507,6 @@ send_register_all_callbacks(int fd, CallbackTable *cb_table)
TSMgmtError
send_unregister_all_callbacks(int fd, CallbackTable *cb_table)
{
- int event_id;
LLQ *events_with_cb; // list of events with at least one callback
int reg_callback[NUM_EVENTS];
TSMgmtError err, send_err = TS_ERR_FAIL;
@@ -525,7 +524,7 @@ send_unregister_all_callbacks(int fd, CallbackTable *cb_table)
int num_events = queue_len(events_with_cb);
// iterate through the LLQ and mark events that have a callback
for (int i = 0; i < num_events; i++) {
- event_id = *(int *)dequeue(events_with_cb);
+ int event_id = *(int *)dequeue(events_with_cb);
reg_callback[event_id] = 1; // mark the event as having a callback
}
delete_queue(events_with_cb);
diff --git a/mgmt/api/TSControlMain.cc b/mgmt/api/TSControlMain.cc
index 2f59f53..57a6761 100644
--- a/mgmt/api/TSControlMain.cc
+++ b/mgmt/api/TSControlMain.cc
@@ -133,7 +133,6 @@ ts_ctrl_main(void *arg)
fd_set selectFDs; // for select call
ClientT *client_entry; // an entry of fd to alarms mapping
- int fds_ready; // stores return value for select
struct timeval timeout;
// loops until TM dies; waits for and processes requests from clients
@@ -158,7 +157,7 @@ ts_ctrl_main(void *arg)
}
// select call - timeout is set so we can check events at regular intervals
- fds_ready = mgmt_select(FD_SETSIZE, &selectFDs, (fd_set *)nullptr, (fd_set *)nullptr, &timeout);
+ int fds_ready = mgmt_select(FD_SETSIZE, &selectFDs, (fd_set *)nullptr, (fd_set *)nullptr, &timeout);
// check if have any connections or requests
if (fds_ready > 0) {
diff --git a/mgmt/utils/MgmtUtils.cc b/mgmt/utils/MgmtUtils.cc
index 57ad7ed..71e5fd6 100644
--- a/mgmt/utils/MgmtUtils.cc
+++ b/mgmt/utils/MgmtUtils.cc
@@ -53,11 +53,11 @@ mgmt_use_syslog()
int
mgmt_readline(int soc, char *buf, int maxlen)
{
- int n = 0, rc;
+ int n = 0;
char c;
for (; n < maxlen; n++) {
- rc = read_socket(soc, &c, 1);
+ int rc = read_socket(soc, &c, 1);
if (rc == 1) {
*buf++ = c;
if (c == '\n') {
@@ -105,12 +105,12 @@ mgmt_readline(int soc, char *buf, int maxlen)
int
mgmt_writeline(int soc, const char *data, int nbytes)
{
- int nleft, nwritten, n;
+ int nleft, n;
const char *tmp = data;
nleft = nbytes;
while (nleft > 0) {
- nwritten = write_socket(soc, tmp, nleft);
+ int nwritten = write_socket(soc, tmp, nleft);
if (nwritten <= 0) { /* Error or nothing written */
return nwritten;
}
@@ -141,12 +141,11 @@ mgmt_writeline(int soc, const char *data, int nbytes)
int
mgmt_read_pipe(int fd, char *buf, int bytes_to_read)
{
- int err = 0;
char *p = buf;
int bytes_read = 0;
while (bytes_to_read > 0) {
- err = read_socket(fd, p, bytes_to_read);
+ int err = read_socket(fd, p, bytes_to_read);
if (err == 0) {
return err;
} else if (err < 0) {
@@ -183,12 +182,11 @@ mgmt_read_pipe(int fd, char *buf, int bytes_to_read)
int
mgmt_write_pipe(int fd, char *buf, int bytes_to_write)
{
- int err = 0;
char *p = buf;
int bytes_written = 0;
while (bytes_to_write > 0) {
- err = write_socket(fd, p, bytes_to_write);
+ int err = write_socket(fd, p, bytes_to_write);
if (err == 0) {
return err;
} else if (err < 0) {
@@ -272,7 +270,6 @@ void
mgmt_fatal(const int lerrno, const char *message_format, ...)
{
va_list ap;
- char extended_format[4096], message[4096];
va_start(ap, message_format);
@@ -283,6 +280,7 @@ mgmt_fatal(const int lerrno, const char *message_format, ...)
FatalV(message_format, ap);
} else {
+ char extended_format[4096], message[4096];
snprintf(extended_format, sizeof(extended_format), "FATAL ==> %s", message_format);
vsprintf(message, extended_format, ap);