Stanley Cheung 8 лет назад
Родитель
Сommit
262284c7fb

+ 1 - 1
src/php/ext/grpc/call_credentials.c

@@ -194,7 +194,7 @@ void plugin_get_metadata(void *ptr, grpc_auth_metadata_context context,
   grpc_metadata_array metadata;
   grpc_metadata_array metadata;
   bool cleanup = true;
   bool cleanup = true;
 
 
-  if (Z_TYPE_P(retval) != IS_ARRAY) {
+  if (retval == NULL || Z_TYPE_P(retval) != IS_ARRAY) {
     cleanup = false;
     cleanup = false;
     code = GRPC_STATUS_INVALID_ARGUMENT;
     code = GRPC_STATUS_INVALID_ARGUMENT;
   } else if (!create_metadata_array(retval, &metadata)) {
   } else if (!create_metadata_array(retval, &metadata)) {

+ 6 - 1
src/php/ext/grpc/channel.c

@@ -81,6 +81,7 @@ PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_channel)
       if (!(PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), p->wrapper->key,
       if (!(PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), p->wrapper->key,
                                           key_len, rsrc))) {
                                           key_len, rsrc))) {
         grpc_channel_destroy(p->wrapper->wrapped);
         grpc_channel_destroy(p->wrapper->wrapped);
+        free(p->wrapper->target);
       }
       }
       gpr_mu_unlock(&global_persistent_list_mu);
       gpr_mu_unlock(&global_persistent_list_mu);
     }
     }
@@ -288,7 +289,9 @@ PHP_METHOD(Channel, __construct) {
   }
   }
   channel->wrapper = malloc(sizeof(grpc_channel_wrapper));
   channel->wrapper = malloc(sizeof(grpc_channel_wrapper));
   channel->wrapper->key = key;
   channel->wrapper->key = key;
-  channel->wrapper->target = target;
+  char *target_cpy = malloc(target_length + 1);
+  strcpy(target_cpy, target);
+  channel->wrapper->target = target_cpy;
   channel->wrapper->args_hashstr = sha1str;
   channel->wrapper->args_hashstr = sha1str;
   if (creds != NULL && creds->hashstr != NULL) {
   if (creds != NULL && creds->hashstr != NULL) {
     channel->wrapper->creds_hashstr = creds->hashstr;
     channel->wrapper->creds_hashstr = creds->hashstr;
@@ -431,6 +434,7 @@ PHP_METHOD(Channel, close) {
   gpr_mu_lock(&channel->wrapper->mu);
   gpr_mu_lock(&channel->wrapper->mu);
   if (channel->wrapper->wrapped != NULL) {
   if (channel->wrapper->wrapped != NULL) {
     grpc_channel_destroy(channel->wrapper->wrapped);
     grpc_channel_destroy(channel->wrapper->wrapped);
+    free(channel->wrapper->target);
     channel->wrapper->wrapped = NULL;
     channel->wrapper->wrapped = NULL;
   }
   }
 
 
@@ -464,6 +468,7 @@ static void php_grpc_channel_plink_dtor(php_grpc_zend_resource *rsrc
     gpr_mu_lock(&le->channel->mu);
     gpr_mu_lock(&le->channel->mu);
     if (le->channel->wrapped != NULL) {
     if (le->channel->wrapped != NULL) {
       grpc_channel_destroy(le->channel->wrapped);
       grpc_channel_destroy(le->channel->wrapped);
+      free(le->channel->target);
       free(le->channel->key);
       free(le->channel->key);
       free(le->channel);
       free(le->channel);
     }
     }

+ 7 - 2
src/php/ext/grpc/channel_credentials.c

@@ -116,8 +116,13 @@ PHP_METHOD(ChannelCredentials, setDefaultRootsPem) {
                          "setDefaultRootsPem expects 1 string", 1 TSRMLS_CC);
                          "setDefaultRootsPem expects 1 string", 1 TSRMLS_CC);
     return;
     return;
   }
   }
-  default_pem_root_certs = gpr_malloc((pem_roots_length + 1) * sizeof(char));
-  memcpy(default_pem_root_certs, pem_roots, pem_roots_length + 1);
+  char *default_certs = php_grpc_get_default_pem_root_certs(TSRMLS_C);
+  if (default_certs == NULL) {
+    default_certs = gpr_malloc((pem_roots_length + 1) * sizeof(char));
+    memcpy(default_certs, pem_roots, pem_roots_length + 1);
+    php_grpc_update_default_pem_root_certs(default_certs TSRMLS_CC);
+  }
+  default_pem_root_certs = default_certs;
 }
 }
 
 
 /**
 /**

+ 9 - 1
src/php/ext/grpc/php_grpc.c

@@ -49,7 +49,6 @@
 #include <ext/standard/info.h>
 #include <ext/standard/info.h>
 #include "php_grpc.h"
 #include "php_grpc.h"
 
 
-ZEND_DECLARE_MODULE_GLOBALS(grpc)
 static PHP_GINIT_FUNCTION(grpc);
 static PHP_GINIT_FUNCTION(grpc);
 
 
 /* {{{ grpc_functions[]
 /* {{{ grpc_functions[]
@@ -297,9 +296,18 @@ PHP_RINIT_FUNCTION(grpc) {
  */
  */
 static PHP_GINIT_FUNCTION(grpc) {
 static PHP_GINIT_FUNCTION(grpc) {
   grpc_globals->initialized = 0;
   grpc_globals->initialized = 0;
+  grpc_globals->pem_root_certs = NULL;
 }
 }
 /* }}} */
 /* }}} */
 
 
+void php_grpc_update_default_pem_root_certs(char *default_certs TSRMLS_DC) {
+  GRPC_G(pem_root_certs) = default_certs;
+}
+
+char *php_grpc_get_default_pem_root_certs(TSRMLS_D) {
+  return GRPC_G(pem_root_certs);
+}
+
 /* The previous line is meant for vim and emacs, so it can correctly fold and
 /* The previous line is meant for vim and emacs, so it can correctly fold and
    unfold functions in source code. See the corresponding marks just before
    unfold functions in source code. See the corresponding marks just before
    function definition, where the functions purpose is also documented. Please
    function definition, where the functions purpose is also documented. Please

+ 6 - 0
src/php/ext/grpc/php_grpc.h

@@ -73,6 +73,7 @@ PHP_RINIT_FUNCTION(grpc);
 */
 */
 ZEND_BEGIN_MODULE_GLOBALS(grpc)
 ZEND_BEGIN_MODULE_GLOBALS(grpc)
   zend_bool initialized;
   zend_bool initialized;
+  char *pem_root_certs;
 ZEND_END_MODULE_GLOBALS(grpc)
 ZEND_END_MODULE_GLOBALS(grpc)
 
 
 /* In every utility function you add that needs to use variables
 /* In every utility function you add that needs to use variables
@@ -91,8 +92,13 @@ ZEND_END_MODULE_GLOBALS(grpc)
 #define GRPC_G(v) (grpc_globals.v)
 #define GRPC_G(v) (grpc_globals.v)
 #endif
 #endif
 
 
+ZEND_DECLARE_MODULE_GLOBALS(grpc)
+
 #define GRPC_STARTUP_FUNCTION(module)  ZEND_MINIT_FUNCTION(grpc_##module)
 #define GRPC_STARTUP_FUNCTION(module)  ZEND_MINIT_FUNCTION(grpc_##module)
 #define GRPC_STARTUP(module)           \
 #define GRPC_STARTUP(module)           \
   ZEND_MODULE_STARTUP_N(grpc_##module)(INIT_FUNC_ARGS_PASSTHRU)
   ZEND_MODULE_STARTUP_N(grpc_##module)(INIT_FUNC_ARGS_PASSTHRU)
 
 
+void php_grpc_update_default_pem_root_certs(char *default_certs TSRMLS_DC);
+char *php_grpc_get_default_pem_root_certs(TSRMLS_D);
+
 #endif /* PHP_GRPC_H */
 #endif /* PHP_GRPC_H */