From dea4a18404d448e7bc58ae652f9f4355f1f1e404 Mon Sep 17 00:00:00 2001 From: Ben Laurie Date: Wed, 21 May 2014 11:54:54 +0100 Subject: [PATCH] Implement BUF_strnlen() and use it instead of strlen(). --- crypto/buffer/buf_str.c | 12 +++++++++++- crypto/buffer/buffer.h | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/crypto/buffer/buf_str.c b/crypto/buffer/buf_str.c index a464eb6e25..58028ebac7 100644 --- a/crypto/buffer/buf_str.c +++ b/crypto/buffer/buf_str.c @@ -62,6 +62,16 @@ #include "cryptlib.h" #include +size_t BUF_strnlen(const char *str, size_t maxlen) + { + const char *p; + + for (p = str; *p != '\0' && maxlen-- != 0; ++p) + ; + + return p - str; + } + char *BUF_strdup(const char *str) { if (str == NULL) return(NULL); @@ -75,7 +85,7 @@ char *BUF_strndup(const char *str, size_t siz) if (str == NULL) return(NULL); - len = strlen(str); + len = BUF_strnlen(str, siz); if (siz > len) siz = len; diff --git a/crypto/buffer/buffer.h b/crypto/buffer/buffer.h index 178e418282..4300d01b3f 100644 --- a/crypto/buffer/buffer.h +++ b/crypto/buffer/buffer.h @@ -85,6 +85,7 @@ BUF_MEM *BUF_MEM_new(void); void BUF_MEM_free(BUF_MEM *a); int BUF_MEM_grow(BUF_MEM *str, size_t len); int BUF_MEM_grow_clean(BUF_MEM *str, size_t len); +size_t BUF_strnlen(const char *str, size_t maxlen); char * BUF_strdup(const char *str); char * BUF_strndup(const char *str, size_t siz); void * BUF_memdup(const void *data, size_t siz); -- 2.25.1