BIO_write-ex(): Improve behavior in corner cases and documentation

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15608)
This commit is contained in:
Dr. David von Oheimb 2021-06-03 12:56:11 +02:00 committed by Pauli
parent f41fd10d90
commit 5d43bfa7d5
2 changed files with 22 additions and 11 deletions

View 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)

View 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.