Skip to content

Commit

Permalink
Credentials: Use QsortCompare instead of strcmp for type comparison (g…
Browse files Browse the repository at this point in the history
…rpc#28973)

* Credentials: Use QsortCompare instead of strcmp for type comparison

* Reviewer comments
  • Loading branch information
yashykt authored Mar 3, 2022
1 parent 55ba777 commit cc44f7c
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/core/lib/security/credentials/credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ void grpc_override_well_known_credentials_path_getter(
struct grpc_channel_credentials
: grpc_core::RefCounted<grpc_channel_credentials> {
public:
// The pointer value \a type is used to uniquely identify a creds
// implementation for down-casting purposes. Every creds implementation should
// use a unique string instance, which should be returned by all instances of
// that creds implementation.
explicit grpc_channel_credentials(const char* type) : type_(type) {}
~grpc_channel_credentials() override = default;

Expand Down Expand Up @@ -143,7 +147,9 @@ struct grpc_channel_credentials
// as equal (assuming other channel args match).
int cmp(const grpc_channel_credentials* other) const {
GPR_ASSERT(other != nullptr);
int r = strcmp(type(), other->type());
// Intentionally uses grpc_core::QsortCompare instead of strcmp as a safety
// against different grpc_channel_credentials types using the same name.
int r = grpc_core::QsortCompare(type(), other->type());
if (r != 0) return r;
return cmp_impl(other);
}
Expand Down Expand Up @@ -198,6 +204,10 @@ struct grpc_call_credentials
grpc_core::RefCountedPtr<grpc_auth_context> auth_context;
};

// The pointer value \a type is used to uniquely identify a creds
// implementation for down-casting purposes. Every creds implementation should
// use a unique string instance, which should be returned by all instances of
// that creds implementation.
explicit grpc_call_credentials(
const char* type,
grpc_security_level min_security_level = GRPC_PRIVACY_AND_INTEGRITY)
Expand All @@ -219,7 +229,9 @@ struct grpc_call_credentials
// credentials as effectively the same..
int cmp(const grpc_call_credentials* other) const {
GPR_ASSERT(other != nullptr);
int r = strcmp(type(), other->type());
// Intentionally uses grpc_core::QsortCompare instead of strcmp as a safety
// against different grpc_call_credentials types using the same name.
int r = grpc_core::QsortCompare(type(), other->type());
if (r != 0) return r;
return cmp_impl(other);
}
Expand Down

0 comments on commit cc44f7c

Please sign in to comment.