From 05e3eefcf853c870b5273021ca79adeadbb1683a Mon Sep 17 00:00:00 2001 From: Yang Gao Date: Thu, 12 Mar 2015 23:41:46 -0700 Subject: [PATCH] Early return when duration is non-positive --- src/cpp/client/secure_credentials.cc | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/cpp/client/secure_credentials.cc b/src/cpp/client/secure_credentials.cc index 795d7731c93..fa5ce1e5d48 100644 --- a/src/cpp/client/secure_credentials.cc +++ b/src/cpp/client/secure_credentials.cc @@ -98,8 +98,10 @@ std::unique_ptr ComputeEngineCredentials() { std::unique_ptr ServiceAccountCredentials( const grpc::string& json_key, const grpc::string& scope, std::chrono::seconds token_lifetime) { - gpr_timespec lifetime = gpr_time_from_seconds( - token_lifetime.count() > 0 ? token_lifetime.count() : 0); + if (token_lifetime.count() <= 0) { + return WrapCredentials(nullptr); + } + gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime.count()); return WrapCredentials(grpc_service_account_credentials_create( json_key.c_str(), scope.c_str(), lifetime)); } @@ -107,8 +109,10 @@ std::unique_ptr ServiceAccountCredentials( // Builds JWT credentials. std::unique_ptr JWTCredentials( const grpc::string &json_key, std::chrono::seconds token_lifetime) { - gpr_timespec lifetime = gpr_time_from_seconds( - token_lifetime.count() > 0 ? token_lifetime.count() : 0); + if (token_lifetime.count() <= 0) { + return WrapCredentials(nullptr); + } + gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime.count()); return WrapCredentials( grpc_jwt_credentials_create(json_key.c_str(), lifetime)); }