From 82e659779531a969155420f4f816b2458de3cfd5 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 13 Jun 2019 14:41:17 +0200 Subject: Use strnlen for field copies Signed-off-by: Szczepan Zalega --- libnitrokey/misc.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/libnitrokey/misc.h b/libnitrokey/misc.h index d10c8df..a9c4672 100644 --- a/libnitrokey/misc.h +++ b/libnitrokey/misc.h @@ -67,7 +67,8 @@ private: oss << std::hex << std::setw(sizeof(value)*2) << std::setfill('0') << value; return oss.str(); } - + +#define FIELD_WIDTH_MAX (100) /** * Copies string from pointer to fixed size C-style array. Src needs to be a valid C-string - eg. ended with '\0'. * Throws when source is bigger than destination. @@ -82,12 +83,13 @@ private: // throw EmptySourceStringException(slot_number); return; const size_t s_dest = sizeof dest; - LOG(std::string("strcpyT sizes dest src ") - +std::to_string(s_dest)+ " " - +std::to_string(strlen(src))+ " " - ,nitrokey::log::Loglevel::DEBUG_L2); - if (strlen(src) > s_dest){ - throw TooLongStringException(strlen(src), s_dest, src); + const size_t src_strlen = strnlen(src, FIELD_WIDTH_MAX); + LOG(std::string("strcpyT sizes dest src ") + + std::to_string(s_dest) + " " + + std::to_string(src_strlen) + " " + , nitrokey::log::Loglevel::DEBUG_L2); + if (src_strlen > s_dest){ + throw TooLongStringException(src_strlen, s_dest, src); } strncpy((char*) &dest, src, s_dest); } -- cgit v1.2.1