From f53456210d1daab6de1313baa29debc42ee6aaff Mon Sep 17 00:00:00 2001 From: Moiz Haidry Date: Thu, 27 Feb 2020 09:03:52 -0800 Subject: [PATCH] Addressed Mark's comments --- .../credentials/plugin/plugin_credentials.cc | 17 +++++------ test/core/security/credentials_test.cc | 30 +++++++++---------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/core/lib/security/credentials/plugin/plugin_credentials.cc b/src/core/lib/security/credentials/plugin/plugin_credentials.cc index 3ee05c697f3..ebe59212f82 100644 --- a/src/core/lib/security/credentials/plugin/plugin_credentials.cc +++ b/src/core/lib/security/credentials/plugin/plugin_credentials.cc @@ -43,17 +43,16 @@ grpc_plugin_credentials::~grpc_plugin_credentials() { } std::string grpc_plugin_credentials::debug_string() { - std::string debug_str; + char* debug_c_str = nullptr; if (plugin_.debug_string != nullptr) { - char* debug_c_str = plugin_.debug_string(plugin_.state); - if (strlen(debug_c_str) != 0) { - debug_str = debug_c_str; - } - gpr_free(debug_c_str); + debug_c_str = plugin_.debug_string(plugin_.state); } - return debug_str.empty() - ? "grpc_plugin_credentials did not provide a debug string" - : debug_str; + std::string debug_str( + debug_c_str != nullptr + ? debug_c_str + : "grpc_plugin_credentials did not provide a debug string"); + gpr_free(debug_c_str); + return debug_str; } void grpc_plugin_credentials::pending_request_remove_locked( diff --git a/test/core/security/credentials_test.cc b/test/core/security/credentials_test.cc index 3299259d1ee..fd0cf6cc403 100644 --- a/test/core/security/credentials_test.cc +++ b/test/core/security/credentials_test.cc @@ -1228,9 +1228,9 @@ static void test_jwt_creds_lifetime(void) { grpc_max_auth_token_lifetime()) == 0); /* Check security level. */ GPR_ASSERT(jwt_creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); - GPR_ASSERT(gpr_strincmp(expected_creds_debug_string_prefix, - jwt_creds->debug_string().c_str(), - strlen(expected_creds_debug_string_prefix)) == 0); + GPR_ASSERT(strncmp(expected_creds_debug_string_prefix, + jwt_creds->debug_string().c_str(), + strlen(expected_creds_debug_string_prefix)) == 0); grpc_call_credentials_release(jwt_creds); // Shorter lifetime. @@ -1240,9 +1240,9 @@ static void test_jwt_creds_lifetime(void) { json_key_string, token_lifetime, nullptr); GPR_ASSERT(gpr_time_cmp(creds_as_jwt(jwt_creds)->jwt_lifetime(), token_lifetime) == 0); - GPR_ASSERT(gpr_strincmp(expected_creds_debug_string_prefix, - jwt_creds->debug_string().c_str(), - strlen(expected_creds_debug_string_prefix)) == 0); + GPR_ASSERT(strncmp(expected_creds_debug_string_prefix, + jwt_creds->debug_string().c_str(), + strlen(expected_creds_debug_string_prefix)) == 0); grpc_call_credentials_release(jwt_creds); // Cropped lifetime. @@ -1252,9 +1252,9 @@ static void test_jwt_creds_lifetime(void) { json_key_string, token_lifetime, nullptr); GPR_ASSERT(gpr_time_cmp(creds_as_jwt(jwt_creds)->jwt_lifetime(), grpc_max_auth_token_lifetime()) == 0); - GPR_ASSERT(gpr_strincmp(expected_creds_debug_string_prefix, - jwt_creds->debug_string().c_str(), - strlen(expected_creds_debug_string_prefix)) == 0); + GPR_ASSERT(strncmp(expected_creds_debug_string_prefix, + jwt_creds->debug_string().c_str(), + strlen(expected_creds_debug_string_prefix)) == 0); grpc_call_credentials_release(jwt_creds); gpr_free(json_key_string); @@ -1298,9 +1298,9 @@ static void test_jwt_creds_success(void) { grpc_jwt_encode_and_sign_set_override(encode_and_sign_jwt_success); run_request_metadata_test(creds, auth_md_ctx, state); grpc_core::ExecCtx::Get()->Flush(); - GPR_ASSERT(gpr_strincmp(expected_creds_debug_string_prefix, - creds->debug_string().c_str(), - strlen(expected_creds_debug_string_prefix)) == 0); + GPR_ASSERT(strncmp(expected_creds_debug_string_prefix, + creds->debug_string().c_str(), + strlen(expected_creds_debug_string_prefix)) == 0); creds->Unref(); gpr_free(json_key_string); @@ -1326,9 +1326,9 @@ static void test_jwt_creds_signing_failure(void) { run_request_metadata_test(creds, auth_md_ctx, state); gpr_free(json_key_string); - GPR_ASSERT(gpr_strincmp(expected_creds_debug_string_prefix, - creds->debug_string().c_str(), - strlen(expected_creds_debug_string_prefix)) == 0); + GPR_ASSERT(strncmp(expected_creds_debug_string_prefix, + creds->debug_string().c_str(), + strlen(expected_creds_debug_string_prefix)) == 0); creds->Unref(); grpc_jwt_encode_and_sign_set_override(nullptr);