Handle NULL result of ERR_reason_error_string() in some apps

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13920)
This commit is contained in:
Dr. David von Oheimb 2021-01-21 12:36:58 +01:00 committed by Dr. David von Oheimb
parent 4718326a46
commit 7f90026b3f
6 changed files with 31 additions and 36 deletions

View File

@ -258,15 +258,8 @@ int pkey_main(int argc, char **argv)
* Note: at least for RSA keys if this function returns
* -1, there will be no error reasons.
*/
unsigned long err;
BIO_printf(out, "Key is invalid\n");
while ((err = ERR_peek_error()) != 0) {
BIO_printf(out, "Detailed error: %s\n",
ERR_reason_error_string(err));
ERR_get_error(); /* remove err from error stack */
}
BIO_printf(bio_err, "Key is invalid\n");
ERR_print_errors(bio_err);
goto end;
}
}

View File

@ -52,7 +52,6 @@ int pkeyparam_main(int argc, char **argv)
int text = 0, noout = 0, ret = EXIT_FAILURE, check = 0, r;
OPTION_CHOICE o;
char *infile = NULL, *outfile = NULL, *prog;
unsigned long err;
prog = opt_init(argc, argv, pkeyparam_options);
while ((o = opt_next()) != OPT_EOF) {
@ -125,13 +124,8 @@ int pkeyparam_main(int argc, char **argv)
* Note: at least for RSA keys if this function returns
* -1, there will be no error reasons.
*/
BIO_printf(out, "Parameters are invalid\n");
while ((err = ERR_peek_error()) != 0) {
BIO_printf(out, "Detailed error: %s\n",
ERR_reason_error_string(err));
ERR_get_error(); /* remove err from error stack */
}
BIO_printf(bio_err, "Parameters are invalid\n");
ERR_print_errors(bio_err);
goto end;
}
}

View File

@ -259,7 +259,7 @@ int rsa_main(int argc, char **argv)
pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
if (pctx == NULL) {
BIO_printf(out, "RSA unable to create PKEY context\n");
BIO_printf(bio_err, "RSA unable to create PKEY context\n");
ERR_print_errors(bio_err);
goto end;
}
@ -269,15 +269,8 @@ int rsa_main(int argc, char **argv)
if (r == 1) {
BIO_printf(out, "RSA key ok\n");
} else if (r == 0) {
unsigned long err;
while ((err = ERR_peek_error()) != 0 &&
ERR_GET_LIB(err) == ERR_LIB_RSA &&
ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
BIO_printf(out, "RSA key error: %s\n",
ERR_reason_error_string(err));
ERR_get_error(); /* remove err from error stack */
}
BIO_printf(bio_err, "RSA key not ok\n");
ERR_print_errors(bio_err);
} else if (r == -1) {
ERR_print_errors(bio_err);
goto end;

View File

@ -175,7 +175,7 @@ int BIO_bind(int sock, const BIO_ADDR *addr, int options)
# endif
if (bind(sock, BIO_ADDR_sockaddr(addr), BIO_ADDR_sockaddr_size(addr)) != 0) {
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error() /* may be 0 */,
"calling bind()");
ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_BIND_SOCKET);
return 0;

View File

@ -155,12 +155,27 @@ void OSSL_CMP_print_errors_cb(OSSL_CMP_log_cb_t log_fn)
while ((err = ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) {
const char *component =
improve_location_name(func, ERR_lib_error_string(err));
unsigned long reason = ERR_GET_REASON(err);
const char *rs = NULL;
char rsbuf[256];
#ifndef OPENSSL_NO_ERR
if (ERR_SYSTEM_ERROR(err)) {
if (openssl_strerror_r(reason, rsbuf, sizeof(rsbuf)))
rs = rsbuf;
} else {
rs = ERR_reason_error_string(err);
}
#endif
if (rs == NULL) {
BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", reason);
rs = rsbuf;
}
if (data != NULL && (flags & ERR_TXT_STRING) != 0)
BIO_snprintf(msg, sizeof(msg), "%s:%s", rs, data);
else
BIO_snprintf(msg, sizeof(msg), "%s", rs);
if (!(flags & ERR_TXT_STRING))
data = NULL;
BIO_snprintf(msg, sizeof(msg), "%s%s%s", ERR_reason_error_string(err),
data == NULL || *data == '\0' ? "" : " : ",
data == NULL ? "" : data);
if (log_fn == NULL) {
#ifndef OPENSSL_NO_STDIO
BIO *bio = BIO_new_fp(stderr, BIO_NOCLOSE);

View File

@ -158,8 +158,8 @@ static int execute_CTX_print_errors_test(OSSL_CMP_CTX_TEST_FIXTURE *fixture)
ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
base_err_msg_size += strlen("NULL_ARGUMENT");
expected_size = base_err_msg_size;
ossl_cmp_add_error_data("data1"); /* should prepend separator " : " */
expected_size += strlen(" : " "data1");
ossl_cmp_add_error_data("data1"); /* should prepend separator ":" */
expected_size += strlen(":" "data1");
ossl_cmp_add_error_data("data2"); /* should prepend separator " : " */
expected_size += strlen(" : " "data2");
ossl_cmp_add_error_line("new line"); /* should prepend separator "\n" */
@ -169,7 +169,7 @@ static int execute_CTX_print_errors_test(OSSL_CMP_CTX_TEST_FIXTURE *fixture)
res = 0;
ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
base_err_msg_size = strlen("INVALID_ARGS") + strlen(" : ");
base_err_msg_size = strlen("INVALID_ARGS") + strlen(":");
expected_size = base_err_msg_size;
while (expected_size < 4096) { /* force split */
ERR_add_error_txt(STR_SEP, max_str_literal);