Addendum to commit #16654.
[openssl.git] / crypto / bio / bss_mem.c
index 4fa2cf15a4845d785cc2e430feb84dc41da2b988..5e89537557f914cd763880a5553b97ffa1b95eff 100644 (file)
@@ -94,16 +94,18 @@ BIO *BIO_new_mem_buf(void *buf, int len)
 {
        BIO *ret;
        BUF_MEM *b;
+       size_t sz;
+
        if (!buf) {
                BIOerr(BIO_F_BIO_NEW_MEM_BUF,BIO_R_NULL_PARAMETER);
                return NULL;
        }
-       if(len == -1) len = strlen(buf);
+       sz = (len<0) ? strlen(buf) : len;
        if(!(ret = BIO_new(BIO_s_mem())) ) return NULL;
        b = (BUF_MEM *)ret->ptr;
        b->data = buf;
-       b->length = len;
-       b->max = len;
+       b->length = sz;
+       b->max = sz;
        ret->flags |= BIO_FLAGS_MEM_RDONLY;
        /* Since this is static data retrying wont help */
        ret->num = 0;
@@ -144,30 +146,22 @@ static int mem_read(BIO *b, char *out, int outl)
        {
        int ret= -1;
        BUF_MEM *bm;
-       int i;
-       char *from,*to;
 
        bm=(BUF_MEM *)b->ptr;
        BIO_clear_retry_flags(b);
-       ret=(outl > bm->length)?bm->length:outl;
+       ret=(outl >=0 && (size_t)outl > bm->length)?bm->length:outl;
        if ((out != NULL) && (ret > 0)) {
                memcpy(out,bm->data,ret);
                bm->length-=ret;
-               /* memmove(&(bm->data[0]),&(bm->data[ret]), bm->length); */
                if(b->flags & BIO_FLAGS_MEM_RDONLY) bm->data += ret;
                else {
-                       from=(char *)&(bm->data[ret]);
-                       to=(char *)&(bm->data[0]);
-                       for (i=0; i<bm->length; i++)
-                               to[i]=from[i];
+                       memmove(&(bm->data[0]),&(bm->data[ret]),bm->length);
                }
        } else if (bm->length == 0)
                {
-               if (b->num != 0)
+               ret = b->num;
+               if (ret != 0)
                        BIO_set_retry_read(b);
-               ret= b->num;
-               if(ret < 0)
-                   BIOerr(BIO_F_MEM_READ,BIO_R_EOF_ON_MEMORY_BIO);
                }
        return(ret);
        }
@@ -192,7 +186,7 @@ static int mem_write(BIO *b, const char *in, int inl)
 
        BIO_clear_retry_flags(b);
        blen=bm->length;
-       if (BUF_MEM_grow(bm,blen+inl) != (blen+inl))
+       if (BUF_MEM_grow_clean(bm,blen+inl) != (blen+inl))
                goto end;
        memcpy(&(bm->data[blen]),in,inl);
        ret=inl;
@@ -210,16 +204,20 @@ static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
        switch (cmd)
                {
        case BIO_CTRL_RESET:
-               if (bm->data != NULL) {
+               if (bm->data != NULL)
+                       {
                        /* For read only case reset to the start again */
                        if(b->flags & BIO_FLAGS_MEM_RDONLY) 
-                                       bm->data -= bm->max - bm->length;
-                                       bm->length = bm->max;
-                       else {
+                               {
+                               bm->data -= bm->max - bm->length;
+                               bm->length = bm->max;
+                               }
+                       else
+                               {
                                memset(bm->data,0,bm->max);
                                bm->length=0;
+                               }
                        }
-               }
                break;
        case BIO_CTRL_EOF:
                ret=(long)(bm->length == 0);
@@ -282,7 +280,11 @@ static int mem_gets(BIO *bp, char *buf, int size)
 
        BIO_clear_retry_flags(bp);
        j=bm->length;
-       if (j <= 0) return(0);
+       if (j <= 0)
+               {
+               *buf='\0';
+               return 0;
+               }
        p=bm->data;
        for (i=0; i<j; i++)
                {