From ed693e43329383c0d68455d83778cdc9748a074d 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 84236c7671..07840b57c4 100644 --- a/crypto/buffer/buf_str.c +++ b/crypto/buffer/buf_str.c @@ -60,6 +60,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); @@ -73,7 +83,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 f8da32b485..881c2da170 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.40.0