summaryrefslogtreecommitdiffstats
path: root/crypto/buffer
diff options
context:
space:
mode:
authorBen Laurie <ben@openssl.org>2002-11-13 15:43:43 +0000
committerBen Laurie <ben@openssl.org>2002-11-13 15:43:43 +0000
commit54a656ef081f72a740c550ebd8099b40b8b5cde0 (patch)
tree9b3638b56848c7f0648b84cfa7ad056116b37a1b /crypto/buffer
parent8f797f14b8ff7d3d5cb04443284259a0c94860b3 (diff)
Security fixes brought forward from 0.9.7.
Diffstat (limited to 'crypto/buffer')
-rw-r--r--crypto/buffer/buffer.c57
-rw-r--r--crypto/buffer/buffer.h8
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.