Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 563960d

Browse files
committed
Klocwork fix: removing strncpy call
This patch reworks the code to avoid calling the strncpy method, which is considered as unsafe on windows. Signed-off-by: Thomas Cahuzac <[email protected]>
1 parent b9e2fdb commit 563960d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

test/test-subsystem/TESTSubsystemString.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030
#include <string.h>
31+
#include <iterator>
32+
#include <algorithm>
33+
#include <stdexcept>
34+
#include <Iterator.hpp>
3135
#include "TESTSubsystemString.h"
3236

3337
#define base CTESTSubsystemObject
@@ -46,5 +50,15 @@ std::string CTESTSubsystemString::toString(const void *pvValue, size_t /*size*/)
4650

4751
void CTESTSubsystemString::fromString(const std::string &strValue, void *pvValue, size_t size)
4852
{
49-
strncpy((char *)pvValue, strValue.c_str(), size);
53+
std::size_t requiredBufferSize = strValue.length() + 1; /* Adding one for null character */
54+
if (size < requiredBufferSize) {
55+
throw std::logic_error("Buffer is to small: " + std::to_string(size) + " Minimum size: " +
56+
std::to_string(requiredBufferSize));
57+
}
58+
59+
auto destination = MAKE_ARRAY_ITERATOR(static_cast<char *>(pvValue), size);
60+
auto last = std::copy(begin(strValue), end(strValue), destination);
61+
62+
/* Adding null character */
63+
*last = 0;
5064
}

0 commit comments

Comments
 (0)