diff options
author | Szczepan Zalega <szczepan@nitrokey.com> | 2019-06-13 14:41:17 +0200 |
---|---|---|
committer | Szczepan Zalega <szczepan@nitrokey.com> | 2019-06-13 15:01:03 +0200 |
commit | 82e659779531a969155420f4f816b2458de3cfd5 (patch) | |
tree | a06f2f7dad5fe320677828b9f95d4482f0c4f7d9 | |
parent | 0deddc2205b164cb775ff908265eebdf80943df2 (diff) | |
download | libnitrokey-82e659779531a969155420f4f816b2458de3cfd5.tar.gz libnitrokey-82e659779531a969155420f4f816b2458de3cfd5.tar.bz2 |
Use strnlen for field copies
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rw-r--r-- | libnitrokey/misc.h | 16 |
1 files 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); } |