From 4ab6fcd676549814e9b4a029b6eac1f13ea8b6e6 Mon Sep 17 00:00:00 2001 From: KWSys Upstream Date: Fri, 10 Apr 2020 07:55:47 -0400 Subject: KWSys 2020-04-10 (b62956f5) Code extracted from: https://gitlab.kitware.com/utils/kwsys.git at commit b62956f5d70fef750e8414342f1566a29e8e7899 (master). Upstream Shortlog ----------------- Ben Boeckel (6): ccab3808 clang-tidy: address readability-isolate-declaration lints 87b57076 clang-tidy: address readability-braces-around-statements lints 13b45a41 clang-tidy: address readability-else-after-return lints ebb48d58 clang-tidy: address google-readability-casting lint 09942f51 testSystemTools: add tests for SplitString 986519af SystemTools: handle splitting a string starting with the separator --- Base64.c | 5 +- MD5.c | 24 ++++--- ProcessUNIX.c | 197 +++++++++++++++++++++++++++------------------------- SystemTools.cxx | 2 +- testProcess.c | 3 +- testSystemTools.cxx | 38 ++++++++++ 6 files changed, 162 insertions(+), 107 deletions(-) diff --git a/Base64.c b/Base64.c index bf876f2..4265018 100644 --- a/Base64.c +++ b/Base64.c @@ -130,7 +130,10 @@ size_t kwsysBase64_Encode(const unsigned char* input, size_t length, /* Decode 4 bytes into a 3 byte string. */ int kwsysBase64_Decode3(const unsigned char* src, unsigned char* dest) { - unsigned char d0, d1, d2, d3; + unsigned char d0; + unsigned char d1; + unsigned char d2; + unsigned char d3; d0 = kwsysBase64DecodeChar(src[0]); d1 = kwsysBase64DecodeChar(src[1]); diff --git a/MD5.c b/MD5.c index 97cf9ba..7ae2630 100644 --- a/MD5.c +++ b/MD5.c @@ -171,8 +171,10 @@ typedef struct md5_state_s static void md5_process(md5_state_t* pms, const md5_byte_t* data /*[64]*/) { - md5_word_t a = pms->abcd[0], b = pms->abcd[1], c = pms->abcd[2], - d = pms->abcd[3]; + md5_word_t a = pms->abcd[0]; + md5_word_t b = pms->abcd[1]; + md5_word_t c = pms->abcd[2]; + md5_word_t d = pms->abcd[3]; md5_word_t t; #if BYTE_ORDER > 0 /* Define storage only for big-endian CPUs. */ @@ -227,9 +229,10 @@ static void md5_process(md5_state_t* pms, const md5_byte_t* data /*[64]*/) # else # define xbuf X /* (static only) */ # endif - for (i = 0; i < 16; ++i, xp += 4) + for (i = 0; i < 16; ++i, xp += 4) { xbuf[i] = (md5_word_t)(xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24)); + } } #endif } @@ -367,34 +370,39 @@ static void md5_append(md5_state_t* pms, const md5_byte_t* data, size_t nbytes) size_t offset = (pms->count[0] >> 3) & 63; md5_word_t nbits = (md5_word_t)(nbytes << 3); - if (nbytes <= 0) + if (nbytes <= 0) { return; + } /* Update the message length. */ pms->count[1] += (md5_word_t)(nbytes >> 29); pms->count[0] += nbits; - if (pms->count[0] < nbits) + if (pms->count[0] < nbits) { pms->count[1]++; + } /* Process an initial partial block. */ if (offset) { size_t copy = (offset + nbytes > 64 ? 64 - offset : nbytes); memcpy(pms->buf + offset, p, copy); - if (offset + copy < 64) + if (offset + copy < 64) { return; + } p += copy; left -= copy; md5_process(pms, pms->buf); } /* Process full blocks. */ - for (; left >= 64; p += 64, left -= 64) + for (; left >= 64; p += 64, left -= 64) { md5_process(pms, p); + } /* Process a final partial block. */ - if (left) + if (left) { memcpy(pms->buf, p, left); + } } /* Finish the message and return the digest. */ diff --git a/ProcessUNIX.c b/ProcessUNIX.c index 5fde0b0..d3a09ed 100644 --- a/ProcessUNIX.c +++ b/ProcessUNIX.c @@ -432,8 +432,8 @@ int kwsysProcess_AddCommand(kwsysProcess* cp, char const* const* command) char const* const* c = command; kwsysProcess_ptrdiff_t n = 0; kwsysProcess_ptrdiff_t i = 0; - while (*c++) - ; + while (*c++) { + } n = c - command - 1; newCommands[cp->NumberOfCommands] = (char**)malloc((size_t)(n + 1) * sizeof(char*)); @@ -685,7 +685,8 @@ const char* kwsysProcess_GetErrorString(kwsysProcess* cp) { if (!cp) { return "Process management structure could not be allocated"; - } else if (cp->State == kwsysProcess_State_Error) { + } + if (cp->State == kwsysProcess_State_Error) { return cp->ErrorMessage; } return "Success"; @@ -695,7 +696,8 @@ const char* kwsysProcess_GetExceptionString(kwsysProcess* cp) { if (!(cp && cp->ProcessResults && (cp->NumberOfCommands > 0))) { return "GetExceptionString called with NULL process management structure"; - } else if (cp->State == kwsysProcess_State_Exception) { + } + if (cp->State == kwsysProcess_State_Exception) { return cp->ProcessResults[cp->NumberOfCommands - 1].ExitExceptionString; } return "No exception"; @@ -787,8 +789,8 @@ void kwsysProcess_Execute(kwsysProcess* cp) /* Some platforms specify that the chdir call may be interrupted. Repeat the call until it finishes. */ - while (((r = chdir(cp->WorkingDirectory)) < 0) && (errno == EINTR)) - ; + while (((r = chdir(cp->WorkingDirectory)) < 0) && (errno == EINTR)) { + } if (r < 0) { kwsysProcessCleanup(cp, 1); return; @@ -1014,8 +1016,8 @@ void kwsysProcess_Execute(kwsysProcess* cp) if (cp->RealWorkingDirectory) { /* Some platforms specify that the chdir call may be interrupted. Repeat the call until it finishes. */ - while ((chdir(cp->RealWorkingDirectory) < 0) && (errno == EINTR)) - ; + while ((chdir(cp->RealWorkingDirectory) < 0) && (errno == EINTR)) { + } free(cp->RealWorkingDirectory); cp->RealWorkingDirectory = 0; } @@ -1100,22 +1102,22 @@ int kwsysProcess_WaitForData(kwsysProcess* cp, char** data, int* length, if (wd.PipeId) { /* Data are ready on a pipe. */ return wd.PipeId; - } else if (wd.Expired) { + } + if (wd.Expired) { /* A timeout has expired. */ if (wd.User) { /* The user timeout has expired. It has no time left. */ return kwsysProcess_Pipe_Timeout; - } else { - /* The process timeout has expired. Kill the children now. */ - kwsysProcess_Kill(cp); - cp->Killed = 0; - cp->TimeoutExpired = 1; - return kwsysProcess_Pipe_None; } - } else { - /* No pipes are left open. */ + + /* The process timeout has expired. Kill the children now. */ + kwsysProcess_Kill(cp); + cp->Killed = 0; + cp->TimeoutExpired = 1; return kwsysProcess_Pipe_None; } + /* No pipes are left open. */ + return kwsysProcess_Pipe_None; } static int kwsysProcessWaitForPipe(kwsysProcess* cp, char** data, int* length, @@ -1184,7 +1186,7 @@ static int kwsysProcessWaitForPipe(kwsysProcess* cp, char** data, int* length, /* Make sure the set is empty (it should always be empty here anyway). */ - FD_ZERO(&cp->PipeSet); + FD_ZERO(&cp->PipeSet); // NOLINT(readability-isolate-declaration) /* Setup a timeout if required. */ if (wd->TimeoutTime.tv_sec < 0) { @@ -1227,7 +1229,8 @@ static int kwsysProcessWaitForPipe(kwsysProcess* cp, char** data, int* length, /* Select's timeout expired. */ wd->Expired = 1; return 1; - } else if (numReady < 0) { + } + if (numReady < 0) { /* Select returned an error. Leave the error description in the pipe buffer. */ strncpy(cp->ErrorMessage, strerror(errno), KWSYSPE_PIPE_BUFFER_SIZE); @@ -1367,11 +1370,13 @@ int kwsysProcess_WaitForExit(kwsysProcess* cp, double* userTimeout) cp->ProcessResults[prPipe].State = kwsysProcess_StateByIndex_Exited; cp->ProcessResults[prPipe].ExitException = kwsysProcess_Exception_None; cp->ProcessResults[prPipe].ExitValue = + // NOLINTNEXTLINE(google-readability-casting) (int)WEXITSTATUS(cp->ProcessResults[prPipe].ExitCode); } else if (WIFSIGNALED(cp->ProcessResults[prPipe].ExitCode)) { /* The child received an unhandled signal. */ cp->ProcessResults[prPipe].State = kwsysProcess_State_Exception; kwsysProcessSetExitExceptionByIndex( + // NOLINTNEXTLINE(google-readability-casting) cp, (int)WTERMSIG(cp->ProcessResults[prPipe].ExitCode), prPipe); } else { /* Error getting the child return code. */ @@ -1450,8 +1455,8 @@ void kwsysProcess_Kill(kwsysProcess* cp) /* Reap the child. Keep trying until the call is not interrupted. */ - while ((waitpid(cp->ForkPIDs[i], &status, 0) < 0) && (errno == EINTR)) - ; + while ((waitpid(cp->ForkPIDs[i], &status, 0) < 0) && (errno == EINTR)) { + } } } @@ -1502,7 +1507,7 @@ static int kwsysProcessInitialize(kwsysProcess* cp) cp->PipesLeft = 0; cp->CommandsLeft = 0; #if KWSYSPE_USE_SELECT - FD_ZERO(&cp->PipeSet); + FD_ZERO(&cp->PipeSet); // NOLINT(readability-isolate-declaration) #endif cp->State = kwsysProcess_State_Starting; cp->Killed = 0; @@ -1591,16 +1596,16 @@ static void kwsysProcessCleanup(kwsysProcess* cp, int error) /* Reap the child. Keep trying until the call is not interrupted. */ while ((waitpid(cp->ForkPIDs[i], &status, 0) < 0) && - (errno == EINTR)) - ; + (errno == EINTR)) { + } } } } /* Restore the working directory. */ if (cp->RealWorkingDirectory) { - while ((chdir(cp->RealWorkingDirectory) < 0) && (errno == EINTR)) - ; + while ((chdir(cp->RealWorkingDirectory) < 0) && (errno == EINTR)) { + } } } @@ -1636,8 +1641,8 @@ static void kwsysProcessCleanupDescriptor(int* pfd) if (pfd && *pfd > 2) { /* Keep trying to close until it is not interrupted by a * signal. */ - while ((close(*pfd) < 0) && (errno == EINTR)) - ; + while ((close(*pfd) < 0) && (errno == EINTR)) { + } *pfd = -1; } } @@ -1662,8 +1667,8 @@ static void kwsysProcessClosePipes(kwsysProcess* cp) read until the operation is not interrupted. */ while ((read(cp->PipeReadEnds[i], cp->PipeBuffer, KWSYSPE_PIPE_BUFFER_SIZE) < 0) && - (errno == EINTR)) - ; + (errno == EINTR)) { + } } #endif @@ -1690,7 +1695,8 @@ int decc$set_child_standard_streams(int fd1, int fd2, int fd3); static int kwsysProcessCreate(kwsysProcess* cp, int prIndex, kwsysProcessCreateInformation* si) { - sigset_t mask, old_mask; + sigset_t mask; + sigset_t old_mask; int pgidPipe[2]; char tmp; ssize_t readRes; @@ -1818,8 +1824,8 @@ static int kwsysProcessCreate(kwsysProcess* cp, int prIndex, /* Make sure the child is in the process group before we proceed. This avoids race conditions with calls to the kill function that we make for signalling process groups. */ - while ((readRes = read(pgidPipe[0], &tmp, 1)) > 0) - ; + while ((readRes = read(pgidPipe[0], &tmp, 1)) > 0) { + } if (readRes < 0) { sigprocmask(SIG_SETMASK, &old_mask, 0); kwsysProcessCleanupDescriptor(&si->ErrorPipe[0]); @@ -1847,8 +1853,8 @@ static int kwsysProcessCreate(kwsysProcess* cp, int prIndex, /* Keep trying to read until the operation is not interrupted. */ while (((n = read(si->ErrorPipe[0], cp->ErrorMessage + total, (size_t)(KWSYSPE_PIPE_BUFFER_SIZE - total))) < 0) && - (errno == EINTR)) - ; + (errno == EINTR)) { + } if (n > 0) { total += n; } @@ -2000,28 +2006,26 @@ static int kwsysProcessGetTimeoutLeft(kwsysProcessTime* timeoutTime, if (timeoutTime->tv_sec < 0) { /* No timeout time has been requested. */ return 0; - } else { - /* Calculate the remaining time. */ - kwsysProcessTime currentTime = kwsysProcessTimeGetCurrent(); - kwsysProcessTime timeLeft = - kwsysProcessTimeSubtract(*timeoutTime, currentTime); - if (timeLeft.tv_sec < 0 && userTimeout && *userTimeout <= 0) { - /* Caller has explicitly requested a zero timeout. */ - timeLeft.tv_sec = 0; - timeLeft.tv_usec = 0; - } + } + /* Calculate the remaining time. */ + kwsysProcessTime currentTime = kwsysProcessTimeGetCurrent(); + kwsysProcessTime timeLeft = + kwsysProcessTimeSubtract(*timeoutTime, currentTime); + if (timeLeft.tv_sec < 0 && userTimeout && *userTimeout <= 0) { + /* Caller has explicitly requested a zero timeout. */ + timeLeft.tv_sec = 0; + timeLeft.tv_usec = 0; + } - if (timeLeft.tv_sec < 0 || - (timeLeft.tv_sec == 0 && timeLeft.tv_usec == 0 && zeroIsExpired)) { - /* Timeout has already expired. */ - return 1; - } else { - /* There is some time left. */ - timeoutLength->tv_sec = timeLeft.tv_sec; - timeoutLength->tv_usec = timeLeft.tv_usec; - return 0; - } + if (timeLeft.tv_sec < 0 || + (timeLeft.tv_sec == 0 && timeLeft.tv_usec == 0 && zeroIsExpired)) { + /* Timeout has already expired. */ + return 1; } + /* There is some time left. */ + timeoutLength->tv_sec = timeLeft.tv_sec; + timeoutLength->tv_usec = timeLeft.tv_usec; + return 0; } static kwsysProcessTime kwsysProcessTimeGetCurrent(void) @@ -2426,41 +2430,39 @@ static pid_t kwsysProcessFork(kwsysProcess* cp, if (middle_pid < 0) { /* Fork failed. Return as if we were not detaching. */ return middle_pid; - } else if (middle_pid == 0) { + } + if (middle_pid == 0) { /* This is the intermediate process. Create the real child. */ pid_t child_pid = fork(); if (child_pid == 0) { /* This is the real child process. There is nothing to do here. */ return 0; - } else { - /* Use the error pipe to report the pid to the real parent. */ - while ((write(si->ErrorPipe[1], &child_pid, sizeof(child_pid)) < 0) && - (errno == EINTR)) - ; - - /* Exit without cleanup. The parent holds all resources. */ - kwsysProcessExit(); - return 0; /* Never reached, but avoids SunCC warning. */ } - } else { - /* This is the original parent process. The intermediate - process will use the error pipe to report the pid of the - detached child. */ - pid_t child_pid; - int status; - while ((read(si->ErrorPipe[0], &child_pid, sizeof(child_pid)) < 0) && - (errno == EINTR)) - ; + /* Use the error pipe to report the pid to the real parent. */ + while ((write(si->ErrorPipe[1], &child_pid, sizeof(child_pid)) < 0) && + (errno == EINTR)) { + } - /* Wait for the intermediate process to exit and clean it up. */ - while ((waitpid(middle_pid, &status, 0) < 0) && (errno == EINTR)) - ; - return child_pid; + /* Exit without cleanup. The parent holds all resources. */ + kwsysProcessExit(); + return 0; /* Never reached, but avoids SunCC warning. */ } - } else { - /* Not creating a detached process. Use normal fork. */ - return fork(); + /* This is the original parent process. The intermediate + process will use the error pipe to report the pid of the + detached child. */ + pid_t child_pid; + int status; + while ((read(si->ErrorPipe[0], &child_pid, sizeof(child_pid)) < 0) && + (errno == EINTR)) { + } + + /* Wait for the intermediate process to exit and clean it up. */ + while ((waitpid(middle_pid, &status, 0) < 0) && (errno == EINTR)) { + } + return child_pid; } + /* Not creating a detached process. Use normal fork. */ + return fork(); } #endif @@ -2727,8 +2729,8 @@ static int kwsysProcessesAdd(kwsysProcess* cp) sigemptyset(&newSigAction.sa_mask); while ((sigaction(SIGCHLD, &newSigAction, &kwsysProcessesOldSigChldAction) < 0) && - (errno == EINTR)) - ; + (errno == EINTR)) { + } /* Install our handler for SIGINT / SIGTERM. Repeat call until it is not interrupted. */ @@ -2736,15 +2738,15 @@ static int kwsysProcessesAdd(kwsysProcess* cp) sigaddset(&newSigAction.sa_mask, SIGTERM); while ((sigaction(SIGINT, &newSigAction, &kwsysProcessesOldSigIntAction) < 0) && - (errno == EINTR)) - ; + (errno == EINTR)) { + } sigemptyset(&newSigAction.sa_mask); sigaddset(&newSigAction.sa_mask, SIGINT); while ((sigaction(SIGTERM, &newSigAction, &kwsysProcessesOldSigIntAction) < 0) && - (errno == EINTR)) - ; + (errno == EINTR)) { + } } } @@ -2775,14 +2777,14 @@ static void kwsysProcessesRemove(kwsysProcess* cp) /* Restore the signal handlers. Repeat call until it is not interrupted. */ while ((sigaction(SIGCHLD, &kwsysProcessesOldSigChldAction, 0) < 0) && - (errno == EINTR)) - ; + (errno == EINTR)) { + } while ((sigaction(SIGINT, &kwsysProcessesOldSigIntAction, 0) < 0) && - (errno == EINTR)) - ; + (errno == EINTR)) { + } while ((sigaction(SIGTERM, &kwsysProcessesOldSigTermAction, 0) < 0) && - (errno == EINTR)) - ; + (errno == EINTR)) { + } /* Free the table of process pointers since it is now empty. This is safe because the signal handler has been removed. */ @@ -2808,7 +2810,10 @@ static void kwsysProcessesSignalHandler(int signum #endif ) { - int i, j, procStatus, old_errno = errno; + int i; + int j; + int procStatus; + int old_errno = errno; #if KWSYSPE_USE_SIGINFO (void)info; (void)ucontext; @@ -2865,8 +2870,8 @@ static void kwsysProcessesSignalHandler(int signum memset(&defSigAction, 0, sizeof(defSigAction)); defSigAction.sa_handler = SIG_DFL; sigemptyset(&defSigAction.sa_mask); - while ((sigaction(signum, &defSigAction, 0) < 0) && (errno == EINTR)) - ; + while ((sigaction(signum, &defSigAction, 0) < 0) && (errno == EINTR)) { + } /* Unmask the signal. */ sigemptyset(&unblockSet); sigaddset(&unblockSet, signum); diff --git a/SystemTools.cxx b/SystemTools.cxx index 8ec3058..3a6ceec 100644 --- a/SystemTools.cxx +++ b/SystemTools.cxx @@ -1904,7 +1904,7 @@ std::vector SystemTools::SplitString(const std::string& p, paths.emplace_back("/"); } std::string::size_type pos1 = 0; - std::string::size_type pos2 = path.find(sep, pos1 + 1); + std::string::size_type pos2 = path.find(sep, pos1); while (pos2 != std::string::npos) { paths.push_back(path.substr(pos1, pos2 - pos1)); pos1 = pos2 + 1; diff --git a/testProcess.c b/testProcess.c index 39aaa23..cde466a 100644 --- a/testProcess.c +++ b/testProcess.c @@ -631,7 +631,8 @@ int main(int argc, const char* argv[]) } fprintf(stderr, "Invalid test number %d.\n", n); return 1; - } else if (n >= 1 && n <= 10) { + } + if (n >= 1 && n <= 10) { /* This is the parent process for a requested test number. */ int states[10] = { kwsysProcess_State_Exited, kwsysProcess_State_Exited, diff --git a/testSystemTools.cxx b/testSystemTools.cxx index aa38aec..8909b49 100644 --- a/testSystemTools.cxx +++ b/testSystemTools.cxx @@ -1120,6 +1120,42 @@ static bool CheckURLParsing() return ret; } +static bool CheckSplitString() +{ + bool ret = true; + + auto check_split = [](std::string const& input, + std::initializer_list expected) -> bool { + auto const components = kwsys::SystemTools::SplitString(input, '/'); + if (components.size() != expected.size()) { + std::cerr << "Incorrect split count for " << input << ": " + << components.size() << std::endl; + return false; + } + size_t i = 0; + for (auto& part : expected) { + if (components[i] != part) { + std::cerr << "Incorrect split component " << i << " for " << input + << ": " << components[i] << std::endl; + return false; + } + ++i; + } + return true; + }; + + // No separators + ret &= check_split("nosep", { "nosep" }); + // Simple + ret &= check_split("first/second", { "first", "second" }); + // Separator at beginning + ret &= check_split("/starts/sep", { "", "starts", "sep" }); + // Separator at end + ret &= check_split("ends/sep/", { "ends", "sep", "" }); + + return ret; +} + int testSystemTools(int, char* []) { bool res = true; @@ -1169,5 +1205,7 @@ int testSystemTools(int, char* []) res &= CheckURLParsing(); + res &= CheckSplitString(); + return res ? 0 : 1; } -- cgit v0.12