diff options
author | Ben Laurie <ben@openssl.org> | 2002-11-12 13:23:40 +0000 |
---|---|---|
committer | Ben Laurie <ben@openssl.org> | 2002-11-12 13:23:40 +0000 |
commit | 9831d941ca3d46eab3e61e6983921220b9d5d09a (patch) | |
tree | ec628033e73b153616f300b3807921c9fe7de70f /crypto/buffer | |
parent | 707ceb29e04f7199f1ae619a6c0eab866fd15add (diff) |
Many security improvements (CHATS) and a warning fix.
Diffstat (limited to 'crypto/buffer')
-rw-r--r-- | crypto/buffer/buffer.c | 57 | ||||
-rw-r--r-- | crypto/buffer/buffer.h | 8 |
2 files changed, 65 insertions, 0 deletions
diff --git a/crypto/buffer/buffer.c b/crypto/buffer/buffer.c index 9299baba9e..d96487e7db 100644 --- a/crypto/buffer/buffer.c +++ b/crypto/buffer/buffer.c @@ -125,6 +125,43 @@ int BUF_MEM_grow(BUF_MEM *str, int len) return(len); } +int BUF_MEM_grow_clean(BUF_MEM *str, int len) + { + char *ret; + unsigned int n; + + if (str->length >= len) + { + memset(&str->data[len],0,str->length-len); + str->length=len; + return(len); + } + if (str->max >= len) + { + memset(&str->data[str->length],0,len-str->length); + str->length=len; + return(len); + } + n=(len+3)/3*4; + if (str->data == NULL) + ret=OPENSSL_malloc(n); + else + ret=OPENSSL_realloc_clean(str->data,str->max,n); + if (ret == NULL) + { + BUFerr(BUF_F_BUF_MEM_GROW,ERR_R_MALLOC_FAILURE); + len=0; + } + else + { + str->data=ret; + str->max=n; + memset(&str->data[str->length],0,len-str->length); + str->length=len; + } + return(len); + } + char *BUF_strdup(const char *str) { char *ret; @@ -143,3 +180,23 @@ char *BUF_strdup(const char *str) return(ret); } +size_t BUF_strlcpy(char *dst, const char *src, size_t size) + { + size_t l = 0; + for(; size > 1 && *src; size--) + { + *dst++ = *src++; + l++; + } + if (size) + *dst = '\0'; + return l + strlen(src); + } + +size_t BUF_strlcat(char *dst, const char *src, size_t size) + { + size_t l = 0; + for(; size > 0 && *dst; size--, dst++) + l++; + return l + BUF_strlcpy(dst, src, size); + } diff --git a/crypto/buffer/buffer.h b/crypto/buffer/buffer.h index 11e2d0359a..b6307f52ec 100644 --- a/crypto/buffer/buffer.h +++ b/crypto/buffer/buffer.h @@ -63,6 +63,8 @@ extern "C" { #endif +#include <sys/types.h> + typedef struct buf_mem_st { int length; /* current number of bytes */ @@ -73,8 +75,14 @@ typedef struct buf_mem_st BUF_MEM *BUF_MEM_new(void); void BUF_MEM_free(BUF_MEM *a); int BUF_MEM_grow(BUF_MEM *str, int len); +int BUF_MEM_grow_clean(BUF_MEM *str, int len); char * BUF_strdup(const char *str); +/* safe string functions */ +size_t BUF_strlcpy(char *dst,const char *src,size_t siz); +size_t BUF_strlcat(char *dst,const char *src,size_t siz); + + /* BEGIN ERROR CODES */ /* The following lines are auto generated by the script mkerr.pl. Any changes * made after this point may be overwritten when the script is next run. |