]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
BIO_write-ex(): Improve behavior in corner cases and documentation
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Thu, 3 Jun 2021 10:56:11 +0000 (12:56 +0200)
committerPauli <pauli@openssl.org>
Tue, 8 Jun 2021 05:17:11 +0000 (15:17 +1000)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15608)

crypto/bio/bio_lib.c
doc/man3/BIO_read.pod

index 80b81db5c400563280dd360cc43ad7b43b292ab7..cdce122796031fb4bfb8f5f6981003eaed4e28ee 100644 (file)
@@ -332,8 +332,11 @@ int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
 static int bio_write_intern(BIO *b, const void *data, size_t dlen,
                             size_t *written)
 {
+    size_t local_written;
     int ret;
 
+    if (written != NULL)
+        *written = 0;
     /*
      * b == NULL is not an error but just means that zero bytes are written.
      * Do not raise an error here.
@@ -356,15 +359,17 @@ static int bio_write_intern(BIO *b, const void *data, size_t dlen,
         return -1;
     }
 
-    ret = b->method->bwrite(b, data, dlen, written);
+    ret = b->method->bwrite(b, data, dlen, &local_written);
 
     if (ret > 0)
-        b->num_write += (uint64_t)*written;
+        b->num_write += (uint64_t)local_written;
 
     if (HAS_CALLBACK(b))
         ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
-                                     dlen, 0, 0L, ret, written);
+                                     dlen, 0, 0L, ret, &local_written);
 
+    if (written != NULL)
+        *written = local_written;
     return ret;
 }
 
@@ -373,13 +378,13 @@ int BIO_write(BIO *b, const void *data, int dlen)
     size_t written;
     int ret;
 
-    if (dlen < 0)
+    if (dlen <= 0)
         return 0;
 
     ret = bio_write_intern(b, data, (size_t)dlen, &written);
 
     if (ret > 0) {
-        /* *written should always be <= dlen */
+        /* written should always be <= dlen */
         ret = (int)written;
     }
 
@@ -388,7 +393,7 @@ int BIO_write(BIO *b, const void *data, int dlen)
 
 int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
 {
-    return bio_write_intern(b, data, dlen, written) > 0;
+    return bio_write_intern(b, data, dlen, written) >= 0;
 }
 
 int BIO_puts(BIO *b, const char *buf)
index d9201ef3b7926ec4de0b67c8adbe43c05b69c3b0..08104b1b92d161f9a64b528f388cb9a0689c1019 100644 (file)
@@ -25,8 +25,9 @@ BIO_read_ex() attempts to read I<dlen> bytes from BIO I<b> and places the data
 in I<data>. If any bytes were successfully read then the number of bytes read is
 stored in I<*readbytes>.
 
-BIO_write_ex() attempts to write I<dlen> bytes from I<data> to BIO I<b>. If
-successful then the number of bytes written is stored in I<*written>.
+BIO_write_ex() attempts to write I<dlen> bytes from I<data> to BIO I<b>.
+If successful then the number of bytes written is stored in I<*written>
+unless I<written> is NULL. No data is written if I<b> is NULL.
 
 BIO_read() attempts to read I<len> bytes from BIO I<b> and places
 the data in I<buf>.
@@ -55,10 +56,15 @@ BIO_puts() attempts to write a NUL-terminated string I<buf> to BIO I<b>.
 
 =head1 RETURN VALUES
 
-BIO_read_ex() and BIO_write_ex() return 1 if data was successfully read or
-written, and 0 otherwise.
+BIO_read_ex() returns 1 if data was successfully read, and 0 otherwise.
 
-BIO_write() and BIO_write_ex() return 0 if the BIO I<b> is NULL.
+BIO_write_ex() returns 1 if no error was encountered writing data, 0 otherwise.
+Write to NULL B<BIO> is not considered as an error.
+
+BIO_write() returns -2 if the "write" operation is not implemented by the BIO
+or -1 on other errors.
+Otherwise it returns the number of bytes written.
+This may be 0 if the BIO I<b> is NULL or I<dlen <= 0>.
 
 BIO_gets() returns -2 if the "gets" operation is not implemented by the BIO
 or -1 on other errors.