121925
From 30238f96081e47178237e58f5229850514858fd3 Mon Sep 17 00:00:00 2001
121925
From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
121925
Date: Tue, 15 Nov 2016 06:33:36 +0000
121925
Subject: [PATCH] merge revision(s) 53064: [Backport #11810]
121925
121925
	* ext/openssl/ossl_ssl.c (ssl_npn_select_cb_common): fix parsing
121925
	  protocol list.
121925
	  The protocol list from OpenSSL is not null-terminated.
121925
	  patched by Kazuki Yamaguchi [Bug #11810] [ruby-core:72082]
121925
121925
121925
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@56798 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
121925
---
121925
 ChangeLog              |  7 +++++++
121925
 ext/openssl/ossl_ssl.c | 17 +++++++----------
121925
 2 files changed, 14 insertions(+), 10 deletions(-)
121925
121925
diff --git a/ChangeLog b/ChangeLog
121925
index cae6e73..f8f303e 100644
121925
--- a/ChangeLog
121925
+++ b/ChangeLog
121925
@@ -1,3 +1,10 @@
121925
+Tue Nov 15 15:29:36 2016  NARUSE, Yui  <naruse@ruby-lang.org>
121925
+
121925
+	* ext/openssl/ossl_ssl.c (ssl_npn_select_cb_common): fix parsing
121925
+	  protocol list.
121925
+	  The protocol list from OpenSSL is not null-terminated.
121925
+	  patched by Kazuki Yamaguchi [Bug #11810] [ruby-core:72082]
121925
+
121925
 Thu Feb 25 19:49:31 2016  Nobuyoshi Nakada  <nobu@ruby-lang.org>
121925
 
121925
 	* ext/socket/socket.c (sock_gethostname): support unlimited size
121925
diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c
121925
index 0da1eb1..5b00cb7 100644
121925
--- a/ext/openssl/ossl_ssl.c
121925
+++ b/ext/openssl/ossl_ssl.c
121925
@@ -605,19 +605,16 @@ ssl_npn_select_cb_common(VALUE cb, const unsigned char **out, unsigned char *out
121925
 {
121925
     VALUE selected;
121925
     long len;
121925
-    unsigned char l;
121925
     VALUE protocols = rb_ary_new();
121925
+    unsigned char l;
121925
+    const unsigned char *in_end = in + inlen;
121925
 
121925
-    /* The format is len_1|proto_1|...|len_n|proto_n\0 */
121925
-    while (l = *in++) {
121925
-	VALUE protocol;
121925
-	if (l > inlen) {
121925
-	    ossl_raise(eSSLError, "Invalid protocol name list");
121925
-	}
121925
-	protocol = rb_str_new((const char *)in, l);
121925
-	rb_ary_push(protocols, protocol);
121925
+    /* assume OpenSSL verifies this format */
121925
+    /* The format is len_1|proto_1|...|len_n|proto_n */
121925
+    while (in < in_end) {
121925
+	l = *in++;
121925
+	rb_ary_push(protocols, rb_str_new((const char *)in, l));
121925
 	in += l;
121925
-	inlen -= l;
121925
     }
121925
 
121925
     selected = rb_funcall(cb, rb_intern("call"), 1, protocols);