Make sure to NUL-terminate the string on end-of-file (and error)
[openssl.git] / crypto / bio / bf_buff.c
index c65dc14..c1fd75a 100644 (file)
@@ -60,7 +60,6 @@
 #include <errno.h>
 #include "cryptlib.h"
 #include <openssl/bio.h>
-#include <openssl/evp.h>
 
 static int buffer_write(BIO *h, const char *buf,int num);
 static int buffer_read(BIO *h, char *buf, int size);
@@ -70,7 +69,7 @@ static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
 static int buffer_new(BIO *h);
 static int buffer_free(BIO *data);
 static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
-#define DEFAULT_BUFFER_SIZE    1024
+#define DEFAULT_BUFFER_SIZE    4096
 
 static BIO_METHOD methods_buffer=
        {
@@ -483,7 +482,7 @@ static int buffer_gets(BIO *b, char *buf, int size)
                        size-=i;
                        ctx->ibuf_len-=i;
                        ctx->ibuf_off+=i;
-                       if ((flag) || (i == size))
+                       if (flag || size == 0)
                                {
                                *buf='\0';
                                return(num);
@@ -495,6 +494,7 @@ static int buffer_gets(BIO *b, char *buf, int size)
                        if (i <= 0)
                                {
                                BIO_copy_next_retry(b);
+                               *buf='\0';
                                if (i < 0) return((num > 0)?num:i);
                                if (i == 0) return(num);
                                }
@@ -506,6 +506,6 @@ static int buffer_gets(BIO *b, char *buf, int size)
 
 static int buffer_puts(BIO *b, const char *str)
        {
-       return(BIO_write(b,str,strlen(str)));
+       return(buffer_write(b,str,strlen(str)));
        }