Skip to content

Commit 689568f

Browse files
committed
reduce the number of format units
1 parent b8f0cce commit 689568f

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

Modules/_ssl.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -694,26 +694,26 @@ build_ssl_verify_error(_sslmodulestate *state, PySSLSocket *sslsock, int ssl_err
694694
/* verify code for cert validation error */
695695
long verify_code_value = SSL_get_verify_result(sslsock->ssl);
696696

697-
const char *what =
698-
verify_code_value == X509_V_ERR_HOSTNAME_MISMATCH
699-
? "Hostname"
700-
: verify_code_value == X509_V_ERR_IP_ADDRESS_MISMATCH
701-
? "IP address"
702-
: NULL;
703-
704-
if (what == NULL) {
705-
const char *s = X509_verify_cert_error_string(verify_code_value);
706-
verify = s == NULL ? Py_None : PyUnicode_FromString(s);
707-
}
708-
else {
709-
// The server's hostname is known to be an ASCII string.
710-
assert(PyUnicode_IS_ASCII(sslsock->server_hostname));
711-
const char *hostname = PyUnicode_AsUTF8(sslsock->server_hostname);
712-
verify = PyUnicode_FromFormat(
713-
"%s mismatch, certificate is not valid for '%s'.",
714-
what, hostname
715-
);
697+
switch (verify_code_value) {
698+
case X509_V_ERR_IP_ADDRESS_MISMATCH: _Py_FALLTHROUGH;
699+
case X509_V_ERR_HOSTNAME_MISMATCH: {
700+
// The server's hostname is known to be an ASCII string.
701+
assert(PyUnicode_IS_ASCII(sslsock->server_hostname));
702+
const char *hostname = PyUnicode_AsUTF8(sslsock->server_hostname);
703+
const char *fmt =
704+
verify_code_value == X509_V_ERR_IP_ADDRESS_MISMATCH
705+
? "IP address mismatch, certificate is not valid for '%s'."
706+
: "Hostname mismatch, certificate is not valid for '%s'.";
707+
verify = PyUnicode_FromFormat(fmt, hostname);
708+
break;
709+
}
710+
default: {
711+
const char *s = X509_verify_cert_error_string(verify_code_value);
712+
verify = s == NULL ? Py_None : PyUnicode_FromString(s);
713+
break;
714+
}
716715
}
716+
717717
if (verify == NULL) {
718718
goto fail;
719719
}

0 commit comments

Comments
 (0)