diff --git a/src/crypto/CHIPCryptoPALOpenSSL.cpp b/src/crypto/CHIPCryptoPALOpenSSL.cpp index 699eb7682efac6..c695b3dd5ba15a 100644 --- a/src/crypto/CHIPCryptoPALOpenSSL.cpp +++ b/src/crypto/CHIPCryptoPALOpenSSL.cpp @@ -104,7 +104,8 @@ static void _logSSLError() const char * err_str_reason = ERR_reason_error_string(ssl_err_code); if (err_str_lib) { - ChipLogError(Crypto, " ssl err %s %s %s\n", err_str_lib, err_str_routine, err_str_reason); + ChipLogError(Crypto, " ssl err %s %s %s\n", StringOrNullMarker(err_str_lib), StringOrNullMarker(err_str_routine), + StringOrNullMarker(err_str_reason)); } #endif // CHIP_ERROR_LOGGING ssl_err_code = ERR_get_error(); diff --git a/src/lib/support/CodeUtils.h b/src/lib/support/CodeUtils.h index f896da6c7484d1..a3b57980084bfd 100644 --- a/src/lib/support/CodeUtils.h +++ b/src/lib/support/CodeUtils.h @@ -682,6 +682,29 @@ inline void chipDie(void) */ #define ArraySize(a) (sizeof(a) / sizeof((a)[0])) +/** + * @brief Ensures that if `str` is NULL, a non-null `default_str_value` is provided + * + * @param str - null-terminated string pointer or nullptr + * @param default_str_value - replacement value if `str` is nullptr + * @return `str` if not null, otherwise `default_str_value` + */ +inline const char * DefaultStringWhenNull(const char * str, const char * default_str_value) +{ + return (str != nullptr) ? str : default_str_value; +} + +/** + * @brief Ensure that a string for a %s specifier is shown as "(null)" if null + * + * @param str - null-terminated string pointer or nullptr + * @return `str` if not null, otherwise literal "(null)" + */ +inline const char * StringOrNullMarker(const char * str) +{ + return DefaultStringWhenNull(str, "(null)"); +} + namespace chip { /**