From 1ca8bf204a8053222e326a8f1406a037c932d36c Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Wed, 19 Dec 2018 15:44:21 +0100 Subject: [PATCH] added string_utils and strcpy_safe --- slsDetectorSoftware/CMakeLists.txt | 2 ++ .../multiSlsDetector/multiSlsDetector.cpp | 18 +++++----- slsSupportLib/include/container_utils.h | 2 ++ slsSupportLib/include/string_utils.h | 17 ++++++++++ slsSupportLib/src/string_utils.cpp | 9 +++++ tests/CMakeLists.txt | 2 ++ tests/src/test-string_utils.cpp | 34 +++++++++++++++++++ 7 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 slsSupportLib/include/string_utils.h create mode 100644 slsSupportLib/src/string_utils.cpp create mode 100644 tests/src/test-string_utils.cpp diff --git a/slsDetectorSoftware/CMakeLists.txt b/slsDetectorSoftware/CMakeLists.txt index d3c2ca86e..f6f71d9a1 100644 --- a/slsDetectorSoftware/CMakeLists.txt +++ b/slsDetectorSoftware/CMakeLists.txt @@ -6,6 +6,7 @@ set(SOURCES slsDetector/slsDetector.cpp ../slsSupportLib/include/ClientInterface.cpp ../slsSupportLib/include/utilities.cpp + ../slsSupportLib/src/string_utils.cpp ) set(HEADERS @@ -46,6 +47,7 @@ set(PUBLICHEADERS ${PROJECT_SOURCE_DIR}/slsSupportLib/include/sls_detector_exceptions.h ${PROJECT_SOURCE_DIR}/slsSupportLib/include/utilities.h ${PROJECT_SOURCE_DIR}/slsSupportLib/include/container_utils.h + ${PROJECT_SOURCE_DIR}/slsSupportLib/include/string_utils.h sharedMemory/SharedMemory.h slsDetector/slsDetector.h slsDetector/slsDetectorUsers.h diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp index 9327af05a..ca53f5e1d 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp @@ -9,11 +9,13 @@ #include "sls_detector_exceptions.h" #include "utilities.h" +#include "string_utils.h" + #include #include #include //json header in zmq stream #include -#include +#include #include #include #include @@ -391,15 +393,11 @@ void multiSlsDetector::updateUserdetails() { memset(thisMultiDetector->lastUser, 0, SHORT_STRING_LENGTH); memset(thisMultiDetector->lastDate, 0, SHORT_STRING_LENGTH); try { - strncpy(thisMultiDetector->lastUser, exec("whoami").c_str(), - SHORT_STRING_LENGTH - 1); - thisMultiDetector->lastUser[SHORT_STRING_LENGTH - 1] = 0; - strncpy(thisMultiDetector->lastDate, exec("date").c_str(), - DATE_LENGTH - 1); - thisMultiDetector->lastDate[DATE_LENGTH - 1] = 0; + sls::strcpy_safe(thisMultiDetector->lastUser, exec("whoami").c_str()); + sls::strcpy_safe(thisMultiDetector->lastDate, exec("date").c_str()); } catch (...) { - strcpy(thisMultiDetector->lastUser, "errorreading"); - strcpy(thisMultiDetector->lastDate, "errorreading"); + sls::strcpy_safe(thisMultiDetector->lastUser, "errorreading"); + sls::strcpy_safe(thisMultiDetector->lastDate, "errorreading"); } } @@ -3116,7 +3114,7 @@ int multiSlsDetector::setCTBPattern(std::string fname, int detPos) { int addr = 0; FILE *fd = fopen(fname.c_str(), "r"); - if (fd <= 0) { + if (fd == nullptr) { FILE_LOG(logERROR) << "Could not open file"; setErrorMask(getErrorMask() | MULTI_OTHER_ERROR); return -1; diff --git a/slsSupportLib/include/container_utils.h b/slsSupportLib/include/container_utils.h index 5bdd50046..2b327b0f3 100644 --- a/slsSupportLib/include/container_utils.h +++ b/slsSupportLib/include/container_utils.h @@ -98,6 +98,8 @@ T minusOneIfDifferent(const std::vector& container) } } + + //TODO!(Erik)Should try to move away from using this in the slsDetectorPackage inline std::string concatenateIfDifferent(std::vector container) diff --git a/slsSupportLib/include/string_utils.h b/slsSupportLib/include/string_utils.h new file mode 100644 index 000000000..b5f8adc08 --- /dev/null +++ b/slsSupportLib/include/string_utils.h @@ -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 +void strcpy_safe(char (&destination)[array_size], const char *source) { + strncpy(destination, source, array_size); + destination[array_size - 1] = '\0'; +} + + +}; // namespace sls diff --git a/slsSupportLib/src/string_utils.cpp b/slsSupportLib/src/string_utils.cpp new file mode 100644 index 000000000..9429c84b9 --- /dev/null +++ b/slsSupportLib/src/string_utils.cpp @@ -0,0 +1,9 @@ + +// #include +// namespace sls{ + +// void strcpy_safe(char *dst, const char *src, size_t size){ +// strncpy(dst, src, size); +// } + +// }; \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 75559566f..4a4dcac4e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -16,7 +16,9 @@ if(USE_TESTS) set(LOCAL_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) set(TEST_SOURCES ${LOCAL_TEST_DIR}/test-container_utils.cpp + ${LOCAL_TEST_DIR}/test-string_utils.cpp ${LOCAL_TEST_DIR}/test-MySocketTCP.cpp + #${LOCAL_TEST_DIR}/test-multiDetector.cpp ${LOCAL_TEST_DIR}/test.cpp # PARENT_SCOPE diff --git a/tests/src/test-string_utils.cpp b/tests/src/test-string_utils.cpp new file mode 100644 index 000000000..86b2ad140 --- /dev/null +++ b/tests/src/test-string_utils.cpp @@ -0,0 +1,34 @@ +#include "MySocketTCP.h" +#include "catch.hpp" +// #include "multiSlsDetector.h" +#include "logger.h" +#include +#include + +#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'); +}