From 33437566730bc225826e7f173517fb2f429b0e63 Mon Sep 17 00:00:00 2001 From: erm-g <110920239+erm-g@users.noreply.github.com> Date: Fri, 9 Feb 2024 12:09:18 -0800 Subject: [PATCH] [ssl] Fix for ALPN comparision logic (#35863) fix for b/316952851 Closes #35863 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/35863 from erm-g:apln-calc 1b445bf14a98fe35f65df315262c0f1753cc09e4 PiperOrigin-RevId: 605697428 --- src/core/ext/transport/chttp2/alpn/alpn.cc | 5 ++++- test/core/transport/chttp2/alpn_test.cc | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/ext/transport/chttp2/alpn/alpn.cc b/src/core/ext/transport/chttp2/alpn/alpn.cc index 432f2c06076..be9c617a14f 100644 --- a/src/core/ext/transport/chttp2/alpn/alpn.cc +++ b/src/core/ext/transport/chttp2/alpn/alpn.cc @@ -30,7 +30,10 @@ static const char* const supported_versions[] = {"h2"}; int grpc_chttp2_is_alpn_version_supported(const char* version, size_t size) { size_t i; for (i = 0; i < GPR_ARRAY_SIZE(supported_versions); i++) { - if (!strncmp(version, supported_versions[i], size)) return 1; + if (size == strlen(supported_versions[i]) && + !strncmp(version, supported_versions[i], size)) { + return 1; + } } return 0; } diff --git a/test/core/transport/chttp2/alpn_test.cc b/test/core/transport/chttp2/alpn_test.cc index a21ed75514f..a2d33ae682f 100644 --- a/test/core/transport/chttp2/alpn_test.cc +++ b/test/core/transport/chttp2/alpn_test.cc @@ -32,6 +32,7 @@ TEST(AlpnTest, TestAlpnFailure) { ASSERT_FALSE(grpc_chttp2_is_alpn_version_supported("h2-155", 6)); ASSERT_FALSE(grpc_chttp2_is_alpn_version_supported("h1-15", 5)); ASSERT_FALSE(grpc_chttp2_is_alpn_version_supported("grpc-exp", 8)); + ASSERT_FALSE(grpc_chttp2_is_alpn_version_supported("h", 1)); } int main(int argc, char** argv) {