Fix from 1.0.0-stable branch.
[oweals/openssl.git] / crypto / bio / bss_mem.c
index c4e557822e561ade40d960232c0acc5c256521b1..e7ab9cb3a3f86ee12ce17f470b44ba9a19092c55 100644 (file)
@@ -163,11 +163,9 @@ static int mem_read(BIO *b, char *out, int outl)
                }
        } 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 +190,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,15 +208,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;
-                       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);
@@ -281,21 +284,27 @@ static int mem_gets(BIO *bp, char *buf, int size)
 
        BIO_clear_retry_flags(bp);
        j=bm->length;
-       if (j <= 0) return(0);
-       p=bm->data;
-       for (i=0; i<j; i++)
+       if ((size-1) < j) j=size-1;
+       if (j <= 0)
                {
-               if (p[i] == '\n') break;
+               *buf='\0';
+               return 0;
                }
-       if (i == j)
+       p=bm->data;
+       for (i=0; i<j; i++)
                {
-               BIO_set_retry_read(bp);
-               /* return(-1);  change the semantics 0.6.6a */ 
+               if (p[i] == '\n')
+                       {
+                       i++;
+                       break;
+                       }
                }
-       else
-               i++;
-       /* i is the max to copy */
-       if ((size-1) < i) i=size-1;
+
+       /*
+        * i is now the max num of bytes to copy, either j or up to
+        * and including the first newline
+        */ 
+
        i=mem_read(bp,buf,i);
        if (i > 0) buf[i]='\0';
        ret=i;