From ac491d8c1f901fd44c613301d247cd1cd6b82d72 Mon Sep 17 00:00:00 2001
From: Rafael Sales <rafaelcds@gmail.com>
Date: Wed, 2 Mar 2016 02:30:29 -0300
Subject: [PATCH] Raise on unexpected metadata values

The existing implementation was causing segmentation fault because
src/ruby/ext/grpc/rb_call.c:358 was trying to convert any value type other than
Array to String. The Array type is handled in first `if`. This change will cause
the Ruby code that sends non-string values to fail with a better message:
`ArgumentError: Header values must be of type string or array`
---
 src/ruby/ext/grpc/rb_call.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/ruby/ext/grpc/rb_call.c b/src/ruby/ext/grpc/rb_call.c
index dc80d18b459..f5fdbb2ffd7 100644
--- a/src/ruby/ext/grpc/rb_call.c
+++ b/src/ruby/ext/grpc/rb_call.c
@@ -359,7 +359,7 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) {
       md_ary->metadata[md_ary->count].value_length = value_len;
       md_ary->count += 1;
     }
-  } else {
+  } else if (TYPE(val) == T_STRING) {
     value_str = RSTRING_PTR(val);
     value_len = RSTRING_LEN(val);
     if (!grpc_is_binary_header(key_str, key_len) &&
@@ -373,6 +373,10 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) {
     md_ary->metadata[md_ary->count].value = value_str;
     md_ary->metadata[md_ary->count].value_length = value_len;
     md_ary->count += 1;
+  } else {
+    rb_raise(rb_eArgError,
+               "Header values must be of type string or array");
+    return ST_STOP;
   }
 
   return ST_CONTINUE;