From: Dr. David von Oheimb Date: Thu, 3 Jun 2021 10:56:11 +0000 (+0200) Subject: BIO_write-ex(): Improve behavior in corner cases and documentation X-Git-Tag: openssl-3.0.0-beta1~172 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5d43bfa7d58c6af5e40d6615edc83c709df2852b;p=thirdparty%2Fopenssl.git BIO_write-ex(): Improve behavior in corner cases and documentation Reviewed-by: Tomas Mraz Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/15608) --- diff --git a/crypto/bio/bio_lib.c b/crypto/bio/bio_lib.c index 80b81db5c40..cdce1227960 100644 --- a/crypto/bio/bio_lib.c +++ b/crypto/bio/bio_lib.c @@ -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) diff --git a/doc/man3/BIO_read.pod b/doc/man3/BIO_read.pod index d9201ef3b79..08104b1b92d 100644 --- a/doc/man3/BIO_read.pod +++ b/doc/man3/BIO_read.pod @@ -25,8 +25,9 @@ BIO_read_ex() attempts to read I bytes from BIO I and places the data in I. If any bytes were successfully read then the number of bytes read is stored in I<*readbytes>. -BIO_write_ex() attempts to write I bytes from I to BIO I. If -successful then the number of bytes written is stored in I<*written>. +BIO_write_ex() attempts to write I bytes from I to BIO I. +If successful then the number of bytes written is stored in I<*written> +unless I is NULL. No data is written if I is NULL. BIO_read() attempts to read I bytes from BIO I and places the data in I. @@ -55,10 +56,15 @@ BIO_puts() attempts to write a NUL-terminated string I to BIO I. =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 is NULL. +BIO_write_ex() returns 1 if no error was encountered writing data, 0 otherwise. +Write to NULL B 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 is NULL or I. BIO_gets() returns -2 if the "gets" operation is not implemented by the BIO or -1 on other errors.