From b7582e93d353223881d6d4548eb653bcfaf6fe58 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Fri, 11 Jan 2019 16:56:31 +0100 Subject: [PATCH 1/3] working on removing undefined beaviour in configureMAC --- .../slsDetector/slsDetector.cpp | 75 ++++++++----------- slsDetectorSoftware/slsDetector/slsDetector.h | 2 +- slsSupportLib/include/string_utils.h | 5 ++ slsSupportLib/src/string_utils.cpp | 13 ++++ tests/src/test-string_utils.cpp | 6 ++ 5 files changed, 56 insertions(+), 45 deletions(-) diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 474066edf..0cfbe88d7 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -842,8 +842,8 @@ int slsDetector::setDetectorType(detectorType const type) { -int slsDetector::setDetectorType(std::string const stype) { - return setDetectorType(getDetectorType(stype)); +int slsDetector::setDetectorType(const std::string& detector_type) { + return setDetectorType(getDetectorType(detector_type)); } @@ -2027,18 +2027,18 @@ int slsDetector::readAll() { int slsDetector::configureMAC() { int fnum = F_CONFIGURE_MAC; int ret = FAIL; - char args[9][50]; - memset(args, 0, sizeof(args)); - char retvals[2][50]; - memset(retvals, 0, sizeof(retvals)); + const size_t array_size = 50; + const size_t n_args = 9; + const size_t n_retvals = 2; + char args[n_args][array_size] = {}; + char retvals[n_retvals][array_size] = {}; FILE_LOG(logDEBUG1) << "Configuring MAC"; - // if rx_udpip is none if (!(strcmp(thisDetector->receiverUDPIP,"none"))) { //hostname is an ip address if (strchr(thisDetector->receiver_hostname,'.') != nullptr) - strcpy(thisDetector->receiverUDPIP, thisDetector->receiver_hostname); + sls::strcpy_safe(thisDetector->receiverUDPIP, thisDetector->receiver_hostname); //if hostname not ip, convert it to ip else { struct addrinfo *result; @@ -2049,7 +2049,7 @@ int slsDetector::configureMAC() { // on failure, back to none if (dataSocket->ConvertInternetAddresstoIpString(result, thisDetector->receiverUDPIP, MAX_STR_LENGTH)) { - strcpy(thisDetector->receiverUDPIP, "none"); + sls::strcpy_safe(thisDetector->receiverUDPIP, "none"); } } } @@ -2064,12 +2064,12 @@ int slsDetector::configureMAC() { FILE_LOG(logDEBUG1) << "rx_hostname and rx_udpip is valid "; // copy to args - strcpy(args[0],thisDetector->receiverUDPIP); - strcpy(args[1],thisDetector->receiverUDPMAC); - sprintf(args[2],"%x",thisDetector->receiverUDPPort); - strcpy(args[3],thisDetector->detectorMAC); - strcpy(args[4],thisDetector->detectorIP); - sprintf(args[5],"%x",thisDetector->receiverUDPPort2); + sls::strcpy_safe(args[0],thisDetector->receiverUDPIP); + sls::strcpy_safe(args[1],thisDetector->receiverUDPMAC); + snprintf(args[2],array_size, "%x",thisDetector->receiverUDPPort); + sls::strcpy_safe(args[3],thisDetector->detectorMAC); + sls::strcpy_safe(args[4],thisDetector->detectorIP); + snprintf(args[5], array_size, "%x",thisDetector->receiverUDPPort2); // 2d positions to detector to put into udp header { int pos[3] = {0, 0, 0}; @@ -2081,60 +2081,47 @@ int slsDetector::configureMAC() { // pos[2] (z is reserved) FILE_LOG(logDEBUG1) << "Detector [" << detId << "] - (" << pos[0] << "," << pos[1] << ")"; - sprintf(args[6], "%x", pos[0]); - sprintf(args[7], "%x", pos[1]); - sprintf(args[8], "%x", pos[2]); + snprintf(args[6], array_size, "%x", pos[0]); + snprintf(args[7], array_size, "%x", pos[1]); + snprintf(args[8], array_size, "%x", pos[2]); } { //converting IPaddress to std::hex - std::stringstream ss(args[0]); - char cword[50]=""; - bzero(cword, 50); - std::string s; - while (getline(ss, s, '.')) { - sprintf(cword,"%s%02x",cword,atoi(s.c_str())); - } - bzero(args[0], 50); - strcpy(args[0],cword); + const std::string cword = sls::stringIpToHex(args[0]); + sls::strcpy_safe(args[0],cword.c_str()); FILE_LOG(logDEBUG1) << "receiver udp ip:" << args[0] << "-"; } { //converting MACaddress to std::hex std::stringstream ss(args[1]); - char cword[50]=""; - bzero(cword, 50); + char cword[array_size] = {}; std::string s; while (getline(ss, s, ':')) { sprintf(cword,"%s%s",cword,s.c_str()); } - bzero(args[1], 50); - strcpy(args[1],cword); + sls::strcpy_safe(args[1],cword); FILE_LOG(logDEBUG1) << "receiver udp mac:" << args[1] << "-"; } FILE_LOG(logDEBUG1) << "receiver udp port:" << args[2] << "-"; { std::stringstream ss(args[3]); - char cword[50]=""; - bzero(cword, 50); + char cword[array_size] = {}; std::string s; while (getline(ss, s, ':')) { sprintf(cword,"%s%s",cword,s.c_str()); } - bzero(args[3], 50); - strcpy(args[3],cword); - FILE_LOG(logDEBUG1) << "detecotor udp mac:" << args[3] << "-"; + sls::strcpy_safe(args[3],cword); + FILE_LOG(logDEBUG1) << "detector udp mac:" << args[3] << "-"; } { //converting IPaddress to std::hex std::stringstream ss(args[4]); - char cword[50]=""; - bzero(cword, 50); + char cword[50] = {}; std::string s; while (getline(ss, s, '.')) { sprintf(cword,"%s%02x",cword,atoi(s.c_str())); } - bzero(args[4], 50); - strcpy(args[4],cword); + sls::strcpy_safe(args[4],cword); FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[4] << "-"; } FILE_LOG(logDEBUG1) << "receiver udp port2:" << args[5] << "-"; @@ -2171,14 +2158,14 @@ int slsDetector::configureMAC() { (idetectorip)&0xff); // update if different if (strcasecmp(retvals[0],thisDetector->detectorMAC)) { - memset(thisDetector->detectorMAC, 0, MAX_STR_LENGTH); - strcpy(thisDetector->detectorMAC, retvals[0]); + // memset(thisDetector->detectorMAC, 0, MAX_STR_LENGTH); + sls::strcpy_safe(thisDetector->detectorMAC, retvals[0]); FILE_LOG(logINFO) << detId << ": Detector MAC updated to " << thisDetector->detectorMAC; } if (strcasecmp(retvals[1],thisDetector->detectorIP)) { - memset(thisDetector->detectorIP, 0, MAX_STR_LENGTH); - strcpy(thisDetector->detectorIP, retvals[1]); + // memset(thisDetector->detectorIP, 0, MAX_STR_LENGTH); + sls::strcpy_safe(thisDetector->detectorIP, retvals[1]); FILE_LOG(logINFO) << detId << ": Detector IP updated to " << thisDetector->detectorIP; } diff --git a/slsDetectorSoftware/slsDetector/slsDetector.h b/slsDetectorSoftware/slsDetector/slsDetector.h index cbacbc1cf..05b5232b4 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.h +++ b/slsDetectorSoftware/slsDetector/slsDetector.h @@ -395,7 +395,7 @@ public: * @param type string of detector type * @returns detector type in receiver */ - int setDetectorType(std::string stype); + int setDetectorType(const std::string& detector_type); /** * Get Detector type from shared memory variable diff --git a/slsSupportLib/include/string_utils.h b/slsSupportLib/include/string_utils.h index 37ddc588d..fd35f1253 100644 --- a/slsSupportLib/include/string_utils.h +++ b/slsSupportLib/include/string_utils.h @@ -35,4 +35,9 @@ Concatenate strings using + if the strings are different */ std::string concatenateIfDifferent(std::vector container); +/* +Convert an ip address string to a string in hex format. (removing dots) +*/ +std::string stringIpToHex(const std::string& ip); + }; // namespace sls diff --git a/slsSupportLib/src/string_utils.cpp b/slsSupportLib/src/string_utils.cpp index 341c1cf45..ae6b24c2d 100644 --- a/slsSupportLib/src/string_utils.cpp +++ b/slsSupportLib/src/string_utils.cpp @@ -2,6 +2,7 @@ #include "string_utils.h" #include "container_utils.h" #include +#include namespace sls{ @@ -37,5 +38,17 @@ std::string concatenateIfDifferent(std::vector container) } } +std::string stringIpToHex(const std::string& ip) +{ + std::istringstream iss(ip); + std::ostringstream oss; + std::string item; + while (std::getline(iss, item, '.')) + { + oss << std::setw(2) << std::setfill('0') << std::hex << std::stoi(item); + } + return oss.str(); +} + }; // namespace sls \ No newline at end of file diff --git a/tests/src/test-string_utils.cpp b/tests/src/test-string_utils.cpp index 8f27294dc..1437a81fb 100644 --- a/tests/src/test-string_utils.cpp +++ b/tests/src/test-string_utils.cpp @@ -87,4 +87,10 @@ TEST_CASE("concatenate non empty strings with one element"){ REQUIRE(vec.size()==5); auto ret = sls::concatenateNonEmptyStrings(vec); REQUIRE(ret=="hej+"); +} + +TEST_CASE("Convert ip address"){ + std::string address = "101.255.103.1"; + REQUIRE(sls::stringIpToHex(address) == "65ff6701"); + } \ No newline at end of file From 991567ba1ca7031e061104579c8d4be76efe72cc Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Mon, 14 Jan 2019 15:33:35 +0100 Subject: [PATCH 2/3] strcpy and cleanup of configuremac --- .../slsDetector/slsDetector.cpp | 152 ++++++++---------- slsSupportLib/include/string_utils.h | 20 ++- slsSupportLib/src/string_utils.cpp | 4 +- tests/src/test-string_utils.cpp | 18 +++ 4 files changed, 102 insertions(+), 92 deletions(-) diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 0cfbe88d7..8f4245128 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -359,8 +359,7 @@ void slsDetector::initializeDetectorStructure(detectorType type) { thisDetector->shmversion = SLS_SHMVERSION; thisDetector->onlineFlag = OFFLINE_FLAG; thisDetector->stoppedFlag = 0; - strncpy(thisDetector->hostname, DEFAULT_HOSTNAME, MAX_STR_LENGTH-1); - thisDetector->hostname[MAX_STR_LENGTH-1] = 0; + sls::strcpy_safe(thisDetector->hostname, DEFAULT_HOSTNAME); thisDetector->myDetectorType = type; thisDetector->offset[X] = 0; thisDetector->offset[Y] = 0; @@ -368,8 +367,7 @@ void slsDetector::initializeDetectorStructure(detectorType type) { thisDetector->multiSize[Y] = 0; thisDetector->controlPort = DEFAULT_PORTNO; thisDetector->stopPort = DEFAULT_PORTNO + 1; - strncpy(thisDetector->settingsDir, getenv("HOME"), MAX_STR_LENGTH-1); - thisDetector->settingsDir[MAX_STR_LENGTH-1] = 0; + sls::strcpy_safe(thisDetector->settingsDir, getenv("HOME")); thisDetector->nTrimEn = 0; for(int & trimEnergie : thisDetector->trimEnergies) trimEnergie = 0; @@ -377,7 +375,7 @@ void slsDetector::initializeDetectorStructure(detectorType type) { thisDetector->nROI = 0; memset(thisDetector->roiLimits, 0, MAX_ROIS * sizeof(ROI)); thisDetector->roFlags = NORMAL_READOUT; - strcpy(thisDetector->settingsFile, "none"); + sls::strcpy_safe(thisDetector->settingsFile, "none"); thisDetector->currentSettings = UNINITIALIZED; thisDetector->currentThresholdEV = -1; thisDetector->timerValue[FRAME_NUMBER] = 1; @@ -396,16 +394,14 @@ void slsDetector::initializeDetectorStructure(detectorType type) { thisDetector->timerValue[SUBFRAME_ACQUISITION_TIME] = 0; thisDetector->timerValue[STORAGE_CELL_NUMBER] = 0; thisDetector->timerValue[SUBFRAME_DEADTIME] = 0; - strcpy(thisDetector->receiver_hostname, "none"); + sls::strcpy_safe(thisDetector->receiver_hostname, "none"); thisDetector->receiverTCPPort = DEFAULT_PORTNO+2; thisDetector->receiverUDPPort = DEFAULT_UDP_PORTNO; thisDetector->receiverUDPPort2 = DEFAULT_UDP_PORTNO + 1; - strcpy(thisDetector->receiverUDPIP, "none"); - strcpy(thisDetector->receiverUDPMAC, "none"); - strncpy(thisDetector->detectorMAC, DEFAULT_DET_MAC, MAX_STR_LENGTH-1); - thisDetector->detectorMAC[MAX_STR_LENGTH-1] = 0; - strncpy(thisDetector->detectorIP, DEFAULT_DET_IP, MAX_STR_LENGTH-1); - thisDetector->detectorIP[MAX_STR_LENGTH-1] = 0; + sls::strcpy_safe(thisDetector->receiverUDPIP, "none"); + sls::strcpy_safe(thisDetector->receiverUDPMAC, "none"); + sls::strcpy_safe(thisDetector->detectorMAC, DEFAULT_DET_MAC); + sls::strcpy_safe(thisDetector->detectorIP, DEFAULT_DET_IP); thisDetector->receiverOnlineFlag = OFFLINE_FLAG; thisDetector->tenGigaEnable = 0; thisDetector->flippedData[X] = 0; @@ -428,8 +424,8 @@ void slsDetector::initializeDetectorStructure(detectorType type) { thisDetector->activated = true; thisDetector->receiver_deactivatedPaddingEnable = true; thisDetector->receiver_silentMode = false; - strcpy(thisDetector->receiver_filePath, "/"); - strcpy(thisDetector->receiver_fileName, "run"); + sls::strcpy_safe(thisDetector->receiver_filePath, "/"); + sls::strcpy_safe(thisDetector->receiver_fileName, "run"); thisDetector->receiver_fileIndex = 0; thisDetector->receiver_fileFormatType = BINARY; switch(thisDetector->myDetectorType) { @@ -2085,45 +2081,23 @@ int slsDetector::configureMAC() { snprintf(args[7], array_size, "%x", pos[1]); snprintf(args[8], array_size, "%x", pos[2]); } - { - //converting IPaddress to std::hex - const std::string cword = sls::stringIpToHex(args[0]); - sls::strcpy_safe(args[0],cword.c_str()); - FILE_LOG(logDEBUG1) << "receiver udp ip:" << args[0] << "-"; - } - { - //converting MACaddress to std::hex - std::stringstream ss(args[1]); - char cword[array_size] = {}; - std::string s; - while (getline(ss, s, ':')) { - sprintf(cword,"%s%s",cword,s.c_str()); - } - sls::strcpy_safe(args[1],cword); - FILE_LOG(logDEBUG1) << "receiver udp mac:" << args[1] << "-"; - } + + //converting receiverUDPIP to string hex + sls::strcpy_safe(args[0],sls::stringIpToHex(args[0]).c_str()); + FILE_LOG(logDEBUG1) << "receiver udp ip:" << args[0] << "-"; + + //MAC already in hex removing : + sls::removeChar(args[1], ':'); + FILE_LOG(logDEBUG1) << "receiver udp mac:" << args[1] << "-"; FILE_LOG(logDEBUG1) << "receiver udp port:" << args[2] << "-"; - { - std::stringstream ss(args[3]); - char cword[array_size] = {}; - std::string s; - while (getline(ss, s, ':')) { - sprintf(cword,"%s%s",cword,s.c_str()); - } - sls::strcpy_safe(args[3],cword); - FILE_LOG(logDEBUG1) << "detector udp mac:" << args[3] << "-"; - } - { - //converting IPaddress to std::hex - std::stringstream ss(args[4]); - char cword[50] = {}; - std::string s; - while (getline(ss, s, '.')) { - sprintf(cword,"%s%02x",cword,atoi(s.c_str())); - } - sls::strcpy_safe(args[4],cword); - FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[4] << "-"; - } + + // MAC already in hex removing : + sls::removeChar(args[3], ':'); + FILE_LOG(logDEBUG1) << "detector udp mac:" << args[3] << "-"; + //converting detectorIP to string hex + sls::strcpy_safe(args[4],sls::stringIpToHex(args[4]).c_str()); + FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[4] << "-"; + FILE_LOG(logDEBUG1) << "receiver udp port2:" << args[5] << "-"; FILE_LOG(logDEBUG1) << "row:" << args[6] << "-"; FILE_LOG(logDEBUG1) << "col:" << args[7] << "-"; @@ -2144,14 +2118,14 @@ int slsDetector::configureMAC() { uint32_t idetectorip = 0; sscanf(retvals[0], "%lx", &idetectormac); sscanf(retvals[1], "%x", &idetectorip); - sprintf(retvals[0],"%02x:%02x:%02x:%02x:%02x:%02x", + snprintf(retvals[0], sizeof(retvals[0]), "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned int)((idetectormac>>40)&0xFF), (unsigned int)((idetectormac>>32)&0xFF), (unsigned int)((idetectormac>>24)&0xFF), (unsigned int)((idetectormac>>16)&0xFF), (unsigned int)((idetectormac>>8)&0xFF), (unsigned int)((idetectormac>>0)&0xFF)); - sprintf(retvals[1],"%d.%d.%d.%d", + snprintf(retvals[1],sizeof(retvals[1]), "%d.%d.%d.%d", (idetectorip>>24)&0xff, (idetectorip>>16)&0xff, (idetectorip>>8)&0xff, @@ -2838,7 +2812,7 @@ std::string slsDetector::setDetectorMAC(const std::string& detectorMAC) { } // valid format else { - strcpy(thisDetector->detectorMAC, detectorMAC.c_str()); + sls::strcpy_safe(thisDetector->detectorMAC, detectorMAC.c_str()); if (!strcmp(thisDetector->receiver_hostname, "none")) { FILE_LOG(logDEBUG1) << "Receiver hostname not set yet"; } else if (setUDPConnection() == FAIL) { @@ -2861,7 +2835,7 @@ std::string slsDetector::setDetectorIP(const std::string& detectorIP) { } // valid format else { - strcpy(thisDetector->detectorIP, detectorIP.c_str()); + sls::strcpy_safe(thisDetector->detectorIP, detectorIP.c_str()); if (!strcmp(thisDetector->receiver_hostname, "none")) { FILE_LOG(logDEBUG1) << "Receiver hostname not set yet"; } else if (setUDPConnection() == FAIL) { @@ -2891,7 +2865,7 @@ std::string slsDetector::setReceiver(const std::string& receiverIP) { updateDetector(); // start updating - strcpy(thisDetector->receiver_hostname, receiverIP.c_str()); + sls::strcpy_safe(thisDetector->receiver_hostname, receiverIP.c_str()); if (setReceiverOnline(ONLINE_FLAG)==ONLINE_FLAG) { FILE_LOG(logDEBUG1) << @@ -3008,12 +2982,12 @@ std::string slsDetector::setReceiverUDPMAC(const std::string& udpmac) { } // valid format else { - strcpy(thisDetector->receiverUDPMAC, udpmac.c_str()); + sls::strcpy_safe(thisDetector->receiverUDPMAC, udpmac.c_str()); if (!strcmp(thisDetector->receiver_hostname, "none")) { FILE_LOG(logDEBUG1) << "Receiver hostname not set yet"; } // not doing setUDPConnection as rx_udpmac will get replaced,(must use configuremac) - strcpy(thisDetector->receiverUDPMAC,udpmac.c_str()); + sls::strcpy_safe(thisDetector->receiverUDPMAC,udpmac.c_str()); } return std::string(thisDetector->receiverUDPMAC); } @@ -3124,10 +3098,10 @@ std::string slsDetector::setReceiverStreamingIP(std::string sourceIP) { // set it anyway, else it is lost if rx_hostname is not set yet memset(thisDetector->receiver_zmqip, 0, MAX_STR_LENGTH); - strcpy(thisDetector->receiver_zmqip, args); + sls::strcpy_safe(thisDetector->receiver_zmqip, args); // if zmqip is empty, update it if (! strlen(thisDetector->zmqip)) - strcpy(thisDetector->zmqip, args); + sls::strcpy_safe(thisDetector->zmqip, args); FILE_LOG(logDEBUG1) << "Sending receiver streaming IP to receiver: " << args; // send to receiver @@ -3141,7 +3115,7 @@ std::string slsDetector::setReceiverStreamingIP(std::string sourceIP) { } else { FILE_LOG(logDEBUG1) << "Receiver streaming port: " << retvals; memset(thisDetector->receiver_zmqip, 0, MAX_STR_LENGTH); - strcpy(thisDetector->receiver_zmqip, retvals); + sls::strcpy_safe(thisDetector->receiver_zmqip, retvals); if (ret == FORCE_UPDATE) ret = updateReceiver(); } @@ -3168,7 +3142,7 @@ std::string slsDetector::setAdditionalJsonHeader(const std::string& jsonheader) } else { FILE_LOG(logDEBUG1) << "Additional json header: " << retvals; memset(thisDetector->receiver_additionalJsonHeader, 0, MAX_STR_LENGTH); - strcpy(thisDetector->receiver_additionalJsonHeader, retvals); + sls::strcpy_safe(thisDetector->receiver_additionalJsonHeader, retvals); if (ret == FORCE_UPDATE) ret = updateReceiver(); } @@ -3229,7 +3203,7 @@ int slsDetector::setUDPConnection() { int fnum = F_SETUP_RECEIVER_UDP; int ret = FAIL; char args[3][MAX_STR_LENGTH] = {{""}, {""}, {""}};; - char retvals[MAX_STR_LENGTH] = {""}; + char retvals[MAX_STR_LENGTH] = {}; FILE_LOG(logDEBUG1) << "Setting UDP Connection"; // called before set up @@ -3241,7 +3215,7 @@ int slsDetector::setUDPConnection() { if (!strcmp(thisDetector->receiverUDPIP, "none")) { // hostname is an ip address if (strchr(thisDetector->receiver_hostname,'.') != nullptr) - strcpy(thisDetector->receiverUDPIP, thisDetector->receiver_hostname); + sls::strcpy_safe(thisDetector->receiverUDPIP, thisDetector->receiver_hostname); // if hostname not ip, convert it to ip else { struct addrinfo *result; @@ -3252,15 +3226,15 @@ int slsDetector::setUDPConnection() { // on failure, back to none if (dataSocket->ConvertInternetAddresstoIpString(result, thisDetector->receiverUDPIP, MAX_STR_LENGTH)) { - strcpy(thisDetector->receiverUDPIP, "none"); + sls::strcpy_safe(thisDetector->receiverUDPIP, "none"); } } } } //copy arguments to args[][] - strcpy(args[0],thisDetector->receiverUDPIP); - sprintf(args[1],"%d",thisDetector->receiverUDPPort); - sprintf(args[2],"%d",thisDetector->receiverUDPPort2); + sls::strcpy_safe(args[0],thisDetector->receiverUDPIP); + snprintf(args[1], sizeof(args[2]), "%d",thisDetector->receiverUDPPort); + snprintf(args[2], sizeof(args[2]), "%d",thisDetector->receiverUDPPort2); FILE_LOG(logDEBUG1) << "Receiver udp ip address: " << thisDetector->receiverUDPIP; FILE_LOG(logDEBUG1) << "Receiver udp port: " << thisDetector->receiverUDPPort; FILE_LOG(logDEBUG1) << "Receiver udp port2: " << thisDetector->receiverUDPPort2; @@ -3275,7 +3249,7 @@ int slsDetector::setUDPConnection() { } else { FILE_LOG(logDEBUG1) << "Receiver UDP MAC returned : " << retvals; memset(thisDetector->receiverUDPMAC, 0, MAX_STR_LENGTH); - strcpy(thisDetector->receiverUDPMAC, retvals); + sls::strcpy_safe(thisDetector->receiverUDPMAC, retvals); if (ret == FORCE_UPDATE) ret = updateReceiver(); @@ -4519,11 +4493,11 @@ int slsDetector::setReceiverTCPSocket(const std::string& name, int const receiv thisDetector->receiverOnlineFlag = OFFLINE_FLAG; return FAIL; } - strcpy(thisName,thisDetector->receiver_hostname); + sls::strcpy_safe(thisName,thisDetector->receiver_hostname); } else { FILE_LOG(logDEBUG1) << "Setting rx_hostname"; - strcpy(thisName, name.c_str()); - strcpy(thisDetector->receiver_hostname, thisName); + sls::strcpy_safe(thisName, name.c_str()); + sls::strcpy_safe(thisDetector->receiver_hostname, thisName); if (dataSocket) { delete dataSocket; dataSocket = nullptr; @@ -4640,7 +4614,7 @@ int slsDetector::execReceiverCommand(const std::string& cmd) { int ret = FAIL; char arg[MAX_STR_LENGTH] = {0}; char retval[MAX_STR_LENGTH] = {0}; - strcpy(arg, cmd.c_str()); + sls::strcpy_safe(arg, cmd.c_str()); FILE_LOG(logDEBUG1) << "Sending command to receiver: " << arg; if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) { @@ -4661,19 +4635,19 @@ int slsDetector::execReceiverCommand(const std::string& cmd) { int slsDetector::updateReceiverNoWait() { int n = 0, i32 = 0; - char cstring[MAX_STR_LENGTH] = {0}; - char lastClientIP[INET_ADDRSTRLEN] = {0}; + char cstring[MAX_STR_LENGTH] = {}; + char lastClientIP[INET_ADDRSTRLEN] = {}; n += dataSocket->ReceiveDataOnly(lastClientIP, sizeof(lastClientIP)); FILE_LOG(logDEBUG1) << "Updating receiver last modified by " << lastClientIP; // filepath n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring)); - strcpy(thisDetector->receiver_filePath, cstring); + sls::strcpy_safe(thisDetector->receiver_filePath, cstring); // filename n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring)); - strcpy(thisDetector->receiver_fileName, cstring); + sls::strcpy_safe(thisDetector->receiver_fileName, cstring); // index n += dataSocket->ReceiveDataOnly(&i32, sizeof(i32)); @@ -4717,11 +4691,11 @@ int slsDetector::updateReceiverNoWait() { // streaming source ip n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring)); - strcpy(thisDetector->receiver_zmqip, cstring); + sls::strcpy_safe(thisDetector->receiver_zmqip, cstring); // additional json header n += dataSocket->ReceiveDataOnly(cstring, sizeof(cstring)); - strcpy(thisDetector->receiver_additionalJsonHeader, cstring); + sls::strcpy_safe(thisDetector->receiver_additionalJsonHeader, cstring); // receiver streaming enable n += dataSocket->ReceiveDataOnly(&i32, sizeof(i32)); @@ -4816,9 +4790,9 @@ void slsDetector::setDetectorId() { void slsDetector::setDetectorHostname() { int fnum = F_SEND_RECEIVER_DETHOSTNAME; int ret = FAIL; - char args[MAX_STR_LENGTH] = {0}; - char retvals[MAX_STR_LENGTH] = {0}; - strcpy(args, thisDetector->hostname); + char args[MAX_STR_LENGTH] = {}; + char retvals[MAX_STR_LENGTH] = {}; + sls::strcpy_safe(args, thisDetector->hostname); FILE_LOG(logDEBUG1) << "Sending detector hostname to receiver: " << args; if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) { @@ -4846,9 +4820,9 @@ std::string slsDetector::setFilePath(const std::string& path) { if (!path.empty()) { int fnum = F_SET_RECEIVER_FILE_PATH; int ret = FAIL; - char args[MAX_STR_LENGTH] = {0}; - char retvals[MAX_STR_LENGTH] = {0}; - strcpy(args, path.c_str()); + char args[MAX_STR_LENGTH] = {}; + char retvals[MAX_STR_LENGTH] = {}; + sls::strcpy_safe(args, path.c_str()); FILE_LOG(logDEBUG1) << "Sending file path to receiver: " << args; if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) { @@ -4864,7 +4838,7 @@ std::string slsDetector::setFilePath(const std::string& path) { setErrorMask((getErrorMask())|(RECEIVER_PARAMETER_NOT_SET)); } else { FILE_LOG(logDEBUG1) << "Receiver file path: " << retvals; - strcpy(thisDetector->receiver_filePath, retvals); + sls::strcpy_safe(thisDetector->receiver_filePath, retvals); if (ret == FORCE_UPDATE) ret = updateReceiver(); } @@ -4885,7 +4859,7 @@ std::string slsDetector::setFileName(const std::string& fname) { int ret = FAIL; char args[MAX_STR_LENGTH] = {""}; char retvals[MAX_STR_LENGTH] = {""}; - strcpy(args, fname.c_str()); + sls::strcpy_safe(args, fname.c_str()); FILE_LOG(logDEBUG1) << "Sending file name to receiver: " << args; if (thisDetector->receiverOnlineFlag == ONLINE_FLAG && connectData() == OK) { @@ -5766,7 +5740,7 @@ slsDetectorDefs::sls_detector_module* slsDetector::readSettingsFile(const std::s } infile.close(); - strcpy(thisDetector->settingsFile, fname.c_str()); + sls::strcpy_safe(thisDetector->settingsFile, fname.c_str()); FILE_LOG(logINFO) << "Settings file loaded: " << thisDetector->settingsFile; return myMod; } diff --git a/slsSupportLib/include/string_utils.h b/slsSupportLib/include/string_utils.h index fd35f1253..f01af0fd9 100644 --- a/slsSupportLib/include/string_utils.h +++ b/slsSupportLib/include/string_utils.h @@ -18,6 +18,22 @@ void strcpy_safe(char (&destination)[array_size], const char *source) { destination[array_size - 1] = '\0'; } +/* +Removes all occurrences of the specified char from a c string +Templated on array size to ensure no access after buffer limits. +*/ +template +void removeChar(char (&str)[array_size], char ch) { + int count = 0; + for (int i = 0; str[i]; i++) { + if (str[i] != ch) + str[count++] = str[i]; + if (i == array_size - 1) + break; + } + str[count] = '\0'; +} + /* Split a string using the specified delimeter and return a vector of strings. TODO! Look into switching to absl or a string_view based implementation. Current @@ -33,11 +49,11 @@ std::string concatenateNonEmptyStrings(const std::vector &vec); /* Concatenate strings using + if the strings are different */ -std::string concatenateIfDifferent(std::vector container); +std::string concatenateIfDifferent(const std::vector &container); /* Convert an ip address string to a string in hex format. (removing dots) */ -std::string stringIpToHex(const std::string& ip); +std::string stringIpToHex(const std::string &ip); }; // namespace sls diff --git a/slsSupportLib/src/string_utils.cpp b/slsSupportLib/src/string_utils.cpp index ae6b24c2d..b8f4b2be0 100644 --- a/slsSupportLib/src/string_utils.cpp +++ b/slsSupportLib/src/string_utils.cpp @@ -3,6 +3,7 @@ #include "container_utils.h" #include #include +#include namespace sls{ @@ -26,7 +27,7 @@ std::string concatenateNonEmptyStrings(const std::vector& vec){ return ret; } -std::string concatenateIfDifferent(std::vector container) +std::string concatenateIfDifferent(const std::vector& container) { if (allEqual(container)) { return container.front(); @@ -51,4 +52,5 @@ std::string stringIpToHex(const std::string& ip) } + }; // namespace sls \ No newline at end of file diff --git a/tests/src/test-string_utils.cpp b/tests/src/test-string_utils.cpp index 1437a81fb..cb03f1717 100644 --- a/tests/src/test-string_utils.cpp +++ b/tests/src/test-string_utils.cpp @@ -93,4 +93,22 @@ TEST_CASE("Convert ip address"){ std::string address = "101.255.103.1"; REQUIRE(sls::stringIpToHex(address) == "65ff6701"); +} + +TEST_CASE("Remove char from string"){ + char str[] = "sometest"; + sls::removeChar(str, 'e'); + REQUIRE(std::string(str) == "somtst"); +} + +TEST_CASE("Remove char from empty string"){ + char str[50] = {}; + sls::removeChar(str, 'e'); + REQUIRE(std::string(str) == ""); +} + +TEST_CASE("Many characters in a row"){ + char str[] = "someeequitellll::ongstring"; + sls::removeChar(str, 'l'); + REQUIRE(std::string(str) == "someeequite::ongstring"); } \ No newline at end of file From bc9d5718c9d0e9a4116f02f8415f76159d7b68ff Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Mon, 14 Jan 2019 15:38:48 +0100 Subject: [PATCH 3/3] typo --- slsSupportLib/include/container_utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slsSupportLib/include/container_utils.h b/slsSupportLib/include/container_utils.h index 163e8f196..6998a395c 100644 --- a/slsSupportLib/include/container_utils.h +++ b/slsSupportLib/include/container_utils.h @@ -12,7 +12,7 @@ namespace sls { -// C++11 make_unique implementation for exeption safety +// C++11 make_unique implementation for exception safety // already available as std::make_unique in C++14 template std::unique_ptr make_unique(Args &&... args) {