Fix some feedback issues for BIO size_t-ify

Rename some parameters; add some error codes; fix a comment; etc

Reviewed-by: Richard Levitte <levitte@openssl.org>
This commit is contained in:
Matt Caswell 2016-10-25 13:19:59 +01:00
parent fbba62f6c9
commit 7bf79e33c9
3 changed files with 42 additions and 35 deletions

View file

@ -45,12 +45,14 @@ static ERR_STRING_DATA BIO_str_functs[] = {
{ERR_FUNC(BIO_F_BIO_PUTS), "BIO_puts"}, {ERR_FUNC(BIO_F_BIO_PUTS), "BIO_puts"},
{ERR_FUNC(BIO_F_BIO_READ), "BIO_read"}, {ERR_FUNC(BIO_F_BIO_READ), "BIO_read"},
{ERR_FUNC(BIO_F_BIO_READ_EX), "BIO_read_ex"}, {ERR_FUNC(BIO_F_BIO_READ_EX), "BIO_read_ex"},
{ERR_FUNC(BIO_F_BIO_READ_INTERN), "bio_read_intern"},
{ERR_FUNC(BIO_F_BIO_SOCKET), "BIO_socket"}, {ERR_FUNC(BIO_F_BIO_SOCKET), "BIO_socket"},
{ERR_FUNC(BIO_F_BIO_SOCKET_NBIO), "BIO_socket_nbio"}, {ERR_FUNC(BIO_F_BIO_SOCKET_NBIO), "BIO_socket_nbio"},
{ERR_FUNC(BIO_F_BIO_SOCK_INFO), "BIO_sock_info"}, {ERR_FUNC(BIO_F_BIO_SOCK_INFO), "BIO_sock_info"},
{ERR_FUNC(BIO_F_BIO_SOCK_INIT), "BIO_sock_init"}, {ERR_FUNC(BIO_F_BIO_SOCK_INIT), "BIO_sock_init"},
{ERR_FUNC(BIO_F_BIO_WRITE), "BIO_write"}, {ERR_FUNC(BIO_F_BIO_WRITE), "BIO_write"},
{ERR_FUNC(BIO_F_BIO_WRITE_EX), "BIO_write_ex"}, {ERR_FUNC(BIO_F_BIO_WRITE_EX), "BIO_write_ex"},
{ERR_FUNC(BIO_F_BIO_WRITE_INTERN), "bio_write_intern"},
{ERR_FUNC(BIO_F_BUFFER_CTRL), "buffer_ctrl"}, {ERR_FUNC(BIO_F_BUFFER_CTRL), "buffer_ctrl"},
{ERR_FUNC(BIO_F_CONN_CTRL), "conn_ctrl"}, {ERR_FUNC(BIO_F_CONN_CTRL), "conn_ctrl"},
{ERR_FUNC(BIO_F_CONN_STATE), "conn_state"}, {ERR_FUNC(BIO_F_CONN_STATE), "conn_state"},
@ -82,6 +84,7 @@ static ERR_STRING_DATA BIO_str_reasons[] = {
{ERR_REASON(BIO_R_INVALID_ARGUMENT), "invalid argument"}, {ERR_REASON(BIO_R_INVALID_ARGUMENT), "invalid argument"},
{ERR_REASON(BIO_R_INVALID_SOCKET), "invalid socket"}, {ERR_REASON(BIO_R_INVALID_SOCKET), "invalid socket"},
{ERR_REASON(BIO_R_IN_USE), "in use"}, {ERR_REASON(BIO_R_IN_USE), "in use"},
{ERR_REASON(BIO_R_LENGTH_TOO_LONG), "length too long"},
{ERR_REASON(BIO_R_LISTEN_V6_ONLY), "listen v6 only"}, {ERR_REASON(BIO_R_LISTEN_V6_ONLY), "listen v6 only"},
{ERR_REASON(BIO_R_LOOKUP_RETURNED_NOTHING), "lookup returned nothing"}, {ERR_REASON(BIO_R_LOOKUP_RETURNED_NOTHING), "lookup returned nothing"},
{ERR_REASON(BIO_R_MALFORMED_HOST_OR_SERVICE), {ERR_REASON(BIO_R_MALFORMED_HOST_OR_SERVICE),

View file

@ -61,9 +61,6 @@ static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len,
ret = b->callback(b, oper, argp, argi, argl, inret); ret = b->callback(b, oper, argp, argi, argl, inret);
if (ret > LONG_MAX || ret < LONG_MIN)
return -1;
if (ret >= 0 && (HAS_LEN_OPER(bareoper) || bareoper == BIO_CB_PUTS)) { if (ret >= 0 && (HAS_LEN_OPER(bareoper) || bareoper == BIO_CB_PUTS)) {
*processed = (size_t)ret; *processed = (size_t)ret;
ret = 1; ret = 1;
@ -246,54 +243,56 @@ int BIO_method_type(const BIO *b)
/* /*
* This is essentially the same as BIO_read_ex() except that it allows * This is essentially the same as BIO_read_ex() except that it allows
* 0 or a -ve value to indicate failure (retryable or not) in the return. This * 0 or a negative value to indicate failure (retryable or not) in the return.
* is for compatibility with the old style BIO_read(), where existing code may * This is for compatibility with the old style BIO_read(), where existing code
* make assumptions about the return value that it might get. * may make assumptions about the return value that it might get.
*/ */
static int bio_read_intern(BIO *b, void *data, size_t datal, size_t *read) static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *read)
{ {
int ret; int ret;
if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) { if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNSUPPORTED_METHOD); BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNSUPPORTED_METHOD);
return -2; return -2;
} }
if ((b->callback != NULL || b->callback_ex != NULL) && if ((b->callback != NULL || b->callback_ex != NULL) &&
((ret = (int)bio_call_callback(b, BIO_CB_READ, data, datal, 0, 0L, 1L, ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
read)) <= 0)) read)) <= 0))
return ret; return ret;
if (!b->init) { if (!b->init) {
BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNINITIALIZED); BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNINITIALIZED);
return -2; return -2;
} }
ret = b->method->bread(b, data, datal, read); ret = b->method->bread(b, data, dlen, read);
if (ret > 0) if (ret > 0)
b->num_read += (uint64_t)*read; b->num_read += (uint64_t)*read;
if (b->callback != NULL || b->callback_ex != NULL) if (b->callback != NULL || b->callback_ex != NULL)
ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data, ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
datal, 0, 0L, ret, read); dlen, 0, 0L, ret, read);
/* Shouldn't happen */ /* Shouldn't happen */
if (ret > 0 && *read > datal) if (ret > 0 && *read > dlen) {
BIOerr(BIO_F_BIO_READ_INTERN, ERR_R_INTERNAL_ERROR);
return -1; return -1;
}
return ret; return ret;
} }
int BIO_read(BIO *b, void *data, int datal) int BIO_read(BIO *b, void *data, int dlen)
{ {
size_t read; size_t read;
int ret; int ret;
if (datal < 0) if (dlen < 0)
return 0; return 0;
ret = bio_read_intern(b, data, (size_t)datal, &read); ret = bio_read_intern(b, data, (size_t)dlen, &read);
if (ret > 0) { if (ret > 0) {
/* *read should always be <= outl */ /* *read should always be <= outl */
@ -303,11 +302,11 @@ int BIO_read(BIO *b, void *data, int datal)
return ret; return ret;
} }
int BIO_read_ex(BIO *b, void *data, size_t datal, size_t *read) int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *read)
{ {
int ret; int ret;
ret = bio_read_intern(b, data, datal, read); ret = bio_read_intern(b, data, dlen, read);
if (ret > 0) if (ret > 0)
ret = 1; ret = 1;
@ -317,7 +316,7 @@ int BIO_read_ex(BIO *b, void *data, size_t datal, size_t *read)
return ret; return ret;
} }
static int bio_write_intern(BIO *b, const void *data, size_t datal, static int bio_write_intern(BIO *b, const void *data, size_t dlen,
size_t *written) size_t *written)
{ {
int ret; int ret;
@ -326,41 +325,41 @@ static int bio_write_intern(BIO *b, const void *data, size_t datal,
return 0; return 0;
if ((b->method == NULL) || (b->method->bwrite == NULL)) { if ((b->method == NULL) || (b->method->bwrite == NULL)) {
BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNSUPPORTED_METHOD); BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNSUPPORTED_METHOD);
return -2; return -2;
} }
if ((b->callback != NULL || b->callback_ex != NULL) && if ((b->callback != NULL || b->callback_ex != NULL) &&
((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, datal, 0, 0L, 1L, ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
written)) <= 0)) written)) <= 0))
return ret; return ret;
if (!b->init) { if (!b->init) {
BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNINITIALIZED); BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNINITIALIZED);
return -2; return -2;
} }
ret = b->method->bwrite(b, data, datal, written); ret = b->method->bwrite(b, data, dlen, written);
if (ret > 0) if (ret > 0)
b->num_write += (uint64_t)*written; b->num_write += (uint64_t)*written;
if (b->callback != NULL || b->callback_ex != NULL) if (b->callback != NULL || b->callback_ex != NULL)
ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data, ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
datal, 0, 0L, ret, written); dlen, 0, 0L, ret, written);
return ret; return ret;
} }
int BIO_write(BIO *b, const void *data, int datal) int BIO_write(BIO *b, const void *data, int dlen)
{ {
size_t written; size_t written;
int ret; int ret;
if (datal < 0) if (dlen < 0)
return 0; return 0;
ret = bio_write_intern(b, data, (size_t)datal, &written); ret = bio_write_intern(b, data, (size_t)dlen, &written);
if (ret > 0) { if (ret > 0) {
/* *written should always be <= inl */ /* *written should always be <= inl */
@ -370,11 +369,11 @@ int BIO_write(BIO *b, const void *data, int datal)
return ret; return ret;
} }
int BIO_write_ex(BIO *b, const void *data, size_t datal, size_t *written) int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
{ {
int ret; int ret;
ret = bio_write_intern(b, data, datal, written); ret = bio_write_intern(b, data, dlen, written);
if (ret > 0) if (ret > 0)
ret = 1; ret = 1;
@ -418,11 +417,13 @@ int BIO_puts(BIO *b, const char *in)
0L, ret, &written); 0L, ret, &written);
if (ret > 0) { if (ret > 0) {
if (written > INT_MAX) if (written > INT_MAX) {
BIOerr(BIO_F_BIO_PUTS, BIO_R_LENGTH_TOO_LONG);
ret = -1; ret = -1;
else } else {
ret = (int)written; ret = (int)written;
} }
}
return ret; return ret;
} }

View file

@ -551,11 +551,11 @@ void BIO_set_shutdown(BIO *a, int shut);
int BIO_get_shutdown(BIO *a); int BIO_get_shutdown(BIO *a);
void BIO_vfree(BIO *a); void BIO_vfree(BIO *a);
int BIO_up_ref(BIO *a); int BIO_up_ref(BIO *a);
int BIO_read(BIO *b, void *data, int datal); int BIO_read(BIO *b, void *data, int dlen);
int BIO_read_ex(BIO *b, void *data, size_t datal, size_t *read); int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *read);
int BIO_gets(BIO *bp, char *buf, int size); int BIO_gets(BIO *bp, char *buf, int size);
int BIO_write(BIO *b, const void *data, int datal); int BIO_write(BIO *b, const void *data, int dlen);
int BIO_write_ex(BIO *b, const void *data, size_t datal, size_t *written); int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written);
int BIO_puts(BIO *bp, const char *buf); int BIO_puts(BIO *bp, const char *buf);
int BIO_indent(BIO *b, int indent, int max); int BIO_indent(BIO *b, int indent, int max);
long BIO_ctrl(BIO *bp, int cmd, long larg, void *parg); long BIO_ctrl(BIO *bp, int cmd, long larg, void *parg);
@ -811,12 +811,14 @@ int ERR_load_BIO_strings(void);
# define BIO_F_BIO_PUTS 110 # define BIO_F_BIO_PUTS 110
# define BIO_F_BIO_READ 111 # define BIO_F_BIO_READ 111
# define BIO_F_BIO_READ_EX 105 # define BIO_F_BIO_READ_EX 105
# define BIO_F_BIO_READ_INTERN 120
# define BIO_F_BIO_SOCKET 140 # define BIO_F_BIO_SOCKET 140
# define BIO_F_BIO_SOCKET_NBIO 142 # define BIO_F_BIO_SOCKET_NBIO 142
# define BIO_F_BIO_SOCK_INFO 141 # define BIO_F_BIO_SOCK_INFO 141
# define BIO_F_BIO_SOCK_INIT 112 # define BIO_F_BIO_SOCK_INIT 112
# define BIO_F_BIO_WRITE 113 # define BIO_F_BIO_WRITE 113
# define BIO_F_BIO_WRITE_EX 119 # define BIO_F_BIO_WRITE_EX 119
# define BIO_F_BIO_WRITE_INTERN 128
# define BIO_F_BUFFER_CTRL 114 # define BIO_F_BUFFER_CTRL 114
# define BIO_F_CONN_CTRL 127 # define BIO_F_CONN_CTRL 127
# define BIO_F_CONN_STATE 115 # define BIO_F_CONN_STATE 115
@ -842,6 +844,7 @@ int ERR_load_BIO_strings(void);
# define BIO_R_INVALID_ARGUMENT 125 # define BIO_R_INVALID_ARGUMENT 125
# define BIO_R_INVALID_SOCKET 135 # define BIO_R_INVALID_SOCKET 135
# define BIO_R_IN_USE 123 # define BIO_R_IN_USE 123
# define BIO_R_LENGTH_TOO_LONG 102
# define BIO_R_LISTEN_V6_ONLY 136 # define BIO_R_LISTEN_V6_ONLY 136
# define BIO_R_LOOKUP_RETURNED_NOTHING 142 # define BIO_R_LOOKUP_RETURNED_NOTHING 142
# define BIO_R_MALFORMED_HOST_OR_SERVICE 130 # define BIO_R_MALFORMED_HOST_OR_SERVICE 130