added string_utils and strcpy_safe

This commit is contained in:
Erik Frojdh 2018-12-19 15:44:21 +01:00
parent 89d9204e1c
commit 1ca8bf204a
7 changed files with 74 additions and 10 deletions

View File

@ -6,6 +6,7 @@ set(SOURCES
slsDetector/slsDetector.cpp slsDetector/slsDetector.cpp
../slsSupportLib/include/ClientInterface.cpp ../slsSupportLib/include/ClientInterface.cpp
../slsSupportLib/include/utilities.cpp ../slsSupportLib/include/utilities.cpp
../slsSupportLib/src/string_utils.cpp
) )
set(HEADERS set(HEADERS
@ -46,6 +47,7 @@ set(PUBLICHEADERS
${PROJECT_SOURCE_DIR}/slsSupportLib/include/sls_detector_exceptions.h ${PROJECT_SOURCE_DIR}/slsSupportLib/include/sls_detector_exceptions.h
${PROJECT_SOURCE_DIR}/slsSupportLib/include/utilities.h ${PROJECT_SOURCE_DIR}/slsSupportLib/include/utilities.h
${PROJECT_SOURCE_DIR}/slsSupportLib/include/container_utils.h ${PROJECT_SOURCE_DIR}/slsSupportLib/include/container_utils.h
${PROJECT_SOURCE_DIR}/slsSupportLib/include/string_utils.h
sharedMemory/SharedMemory.h sharedMemory/SharedMemory.h
slsDetector/slsDetector.h slsDetector/slsDetector.h
slsDetector/slsDetectorUsers.h slsDetector/slsDetectorUsers.h

View File

@ -9,11 +9,13 @@
#include "sls_detector_exceptions.h" #include "sls_detector_exceptions.h"
#include "utilities.h" #include "utilities.h"
#include "string_utils.h"
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <rapidjson/document.h> //json header in zmq stream #include <rapidjson/document.h> //json header in zmq stream
#include <sstream> #include <sstream>
#include <string.h> #include <cstring>
#include <sys/ipc.h> #include <sys/ipc.h>
#include <sys/shm.h> #include <sys/shm.h>
#include <sys/types.h> #include <sys/types.h>
@ -391,15 +393,11 @@ void multiSlsDetector::updateUserdetails() {
memset(thisMultiDetector->lastUser, 0, SHORT_STRING_LENGTH); memset(thisMultiDetector->lastUser, 0, SHORT_STRING_LENGTH);
memset(thisMultiDetector->lastDate, 0, SHORT_STRING_LENGTH); memset(thisMultiDetector->lastDate, 0, SHORT_STRING_LENGTH);
try { try {
strncpy(thisMultiDetector->lastUser, exec("whoami").c_str(), sls::strcpy_safe(thisMultiDetector->lastUser, exec("whoami").c_str());
SHORT_STRING_LENGTH - 1); sls::strcpy_safe(thisMultiDetector->lastDate, exec("date").c_str());
thisMultiDetector->lastUser[SHORT_STRING_LENGTH - 1] = 0;
strncpy(thisMultiDetector->lastDate, exec("date").c_str(),
DATE_LENGTH - 1);
thisMultiDetector->lastDate[DATE_LENGTH - 1] = 0;
} catch (...) { } catch (...) {
strcpy(thisMultiDetector->lastUser, "errorreading"); sls::strcpy_safe(thisMultiDetector->lastUser, "errorreading");
strcpy(thisMultiDetector->lastDate, "errorreading"); sls::strcpy_safe(thisMultiDetector->lastDate, "errorreading");
} }
} }
@ -3116,7 +3114,7 @@ int multiSlsDetector::setCTBPattern(std::string fname, int detPos) {
int addr = 0; int addr = 0;
FILE *fd = fopen(fname.c_str(), "r"); FILE *fd = fopen(fname.c_str(), "r");
if (fd <= 0) { if (fd == nullptr) {
FILE_LOG(logERROR) << "Could not open file"; FILE_LOG(logERROR) << "Could not open file";
setErrorMask(getErrorMask() | MULTI_OTHER_ERROR); setErrorMask(getErrorMask() | MULTI_OTHER_ERROR);
return -1; return -1;

View File

@ -98,6 +98,8 @@ T minusOneIfDifferent(const std::vector<T>& container)
} }
} }
//TODO!(Erik)Should try to move away from using this in the slsDetectorPackage //TODO!(Erik)Should try to move away from using this in the slsDetectorPackage
inline inline
std::string concatenateIfDifferent(std::vector<std::string> container) std::string concatenateIfDifferent(std::vector<std::string> container)

View File

@ -0,0 +1,17 @@
namespace sls {
/* Implementation of a safe string copy function for setting fields in
for example the multi sls detector. It tries to copy the size of the
destination from the source, stopping on '\0'.
Warning this would truncate the source string and should be used with care.
Still this is better than strcpy...
*/
template <size_t array_size>
void strcpy_safe(char (&destination)[array_size], const char *source) {
strncpy(destination, source, array_size);
destination[array_size - 1] = '\0';
}
}; // namespace sls

View File

@ -0,0 +1,9 @@
// #include <cstring>
// namespace sls{
// void strcpy_safe(char *dst, const char *src, size_t size){
// strncpy(dst, src, size);
// }
// };

View File

@ -16,7 +16,9 @@ if(USE_TESTS)
set(LOCAL_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) set(LOCAL_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(TEST_SOURCES set(TEST_SOURCES
${LOCAL_TEST_DIR}/test-container_utils.cpp ${LOCAL_TEST_DIR}/test-container_utils.cpp
${LOCAL_TEST_DIR}/test-string_utils.cpp
${LOCAL_TEST_DIR}/test-MySocketTCP.cpp ${LOCAL_TEST_DIR}/test-MySocketTCP.cpp
#${LOCAL_TEST_DIR}/test-multiDetector.cpp #${LOCAL_TEST_DIR}/test-multiDetector.cpp
${LOCAL_TEST_DIR}/test.cpp ${LOCAL_TEST_DIR}/test.cpp
# PARENT_SCOPE # PARENT_SCOPE

View File

@ -0,0 +1,34 @@
#include "MySocketTCP.h"
#include "catch.hpp"
// #include "multiSlsDetector.h"
#include "logger.h"
#include <iostream>
#include <vector>
#include "string_utils.h"
#define VERBOSE
TEST_CASE("copy a string") {
char src[10] = "hej";
REQUIRE(src[3]=='\0');
char dst[20];
sls::strcpy_safe(dst, src);
REQUIRE(dst[0]=='h');
REQUIRE(dst[1]=='e');
REQUIRE(dst[2]=='j');
REQUIRE(dst[3]=='\0');
}
TEST_CASE("copy a long string"){
auto src = "some very very long sting that does not fit";
char dst[3];
sls::strcpy_safe(dst, src);
REQUIRE(dst[0]=='s');
REQUIRE(dst[1]=='o');
REQUIRE(dst[3]=='\0');
}