Skip to content

Commit a19fdb0

Browse files
committed
PHPC-1500: Replace manager instance with client hash for hashing
1 parent b65ee36 commit a19fdb0

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

phongo_compat.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@
169169
Z_ARRVAL_P(__z) = __arr; \
170170
Z_TYPE_P(__z) = IS_ARRAY; \
171171
} while (0);
172+
#define ZVAL_DUP(z, v) \
173+
do { \
174+
zval* _z = (z); \
175+
const zval* _v = (v); \
176+
*_z = *_v; \
177+
INIT_PZVAL(_z); \
178+
zval_copy_ctor(_z); \
179+
} while (0);
172180
#define phongo_free_object_arg void
173181
#define phongo_zpp_char_len int
174182
#define ZEND_HASH_APPLY_PROTECTION(ht) 1

php_phongo.c

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,66 @@ int php_phongo_set_monitoring_callbacks(mongoc_client_t* client)
25112511
return retval;
25122512
}
25132513

2514+
static zval* php_phongo_manager_prepare_manager_for_hash(zval* driverOptions, bool* free TSRMLS_DC)
2515+
{
2516+
php_phongo_manager_t* manager;
2517+
zval* autoEncryptionOpts = NULL;
2518+
zval* keyVaultClient = NULL;
2519+
zval* driverOptionsClone = NULL;
2520+
zval* autoEncryptionOptsClone = NULL;
2521+
#if PHP_VERSION_ID >= 70000
2522+
zval stackAutoEncryptionOptsClone;
2523+
#endif
2524+
2525+
*free = false;
2526+
2527+
if (!driverOptions) {
2528+
return NULL;
2529+
}
2530+
2531+
if (!php_array_existsc(driverOptions, "autoEncryption")) {
2532+
goto ref;
2533+
}
2534+
2535+
autoEncryptionOpts = php_array_fetchc(driverOptions, "autoEncryption");
2536+
if (Z_TYPE_P(autoEncryptionOpts) != IS_ARRAY) {
2537+
goto ref;
2538+
}
2539+
2540+
if (!php_array_existsc(autoEncryptionOpts, "keyVaultClient")) {
2541+
goto ref;
2542+
}
2543+
2544+
keyVaultClient = php_array_fetchc(autoEncryptionOpts, "keyVaultClient");
2545+
if (Z_TYPE_P(keyVaultClient) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(keyVaultClient), php_phongo_manager_ce TSRMLS_CC)) {
2546+
goto ref;
2547+
}
2548+
2549+
*free = true;
2550+
2551+
manager = Z_MANAGER_OBJ_P(keyVaultClient);
2552+
2553+
#if PHP_VERSION_ID >= 70000
2554+
driverOptionsClone = ecalloc(sizeof(zval), 1);
2555+
autoEncryptionOptsClone = &stackAutoEncryptionOptsClone;
2556+
#else
2557+
ALLOC_INIT_ZVAL(driverOptionsClone);
2558+
MAKE_STD_ZVAL(autoEncryptionOptsClone);
2559+
#endif
2560+
2561+
ZVAL_DUP(autoEncryptionOptsClone, autoEncryptionOpts);
2562+
ADD_ASSOC_STRINGL(autoEncryptionOptsClone, "keyVaultClient", manager->client_hash, manager->client_hash_len);
2563+
2564+
ZVAL_DUP(driverOptionsClone, driverOptions);
2565+
ADD_ASSOC_ZVAL_EX(driverOptionsClone, "autoEncryption", autoEncryptionOptsClone);
2566+
2567+
return driverOptionsClone;
2568+
2569+
ref:
2570+
Z_ADDREF_P(driverOptions);
2571+
return driverOptions;
2572+
}
2573+
25142574
/* Creates a hash for a client by concatenating the URI string with serialized
25152575
* options arrays. On success, a persistent string is returned (i.e. pefree()
25162576
* should be used to free it) and hash_len will be set to the string's length.
@@ -2520,6 +2580,8 @@ static char* php_phongo_manager_make_client_hash(const char* uri_string, zval* o
25202580
char* hash = NULL;
25212581
smart_str var_buf = { 0 };
25222582
php_serialize_data_t var_hash;
2583+
zval* serializable_driver_options = NULL;
2584+
bool free_driver_options = false;
25232585

25242586
#if PHP_VERSION_ID >= 70000
25252587
zval args;
@@ -2536,8 +2598,8 @@ static char* php_phongo_manager_make_client_hash(const char* uri_string, zval* o
25362598
}
25372599

25382600
if (driverOptions) {
2539-
ADD_ASSOC_ZVAL_EX(&args, "driverOptions", driverOptions);
2540-
Z_ADDREF_P(driverOptions);
2601+
serializable_driver_options = php_phongo_manager_prepare_manager_for_hash(driverOptions, &free_driver_options TSRMLS_CC);
2602+
ADD_ASSOC_ZVAL_EX(&args, "driverOptions", serializable_driver_options);
25412603
} else {
25422604
ADD_ASSOC_NULL_EX(&args, "driverOptions");
25432605
}
@@ -2552,6 +2614,11 @@ static char* php_phongo_manager_make_client_hash(const char* uri_string, zval* o
25522614
}
25532615

25542616
zval_ptr_dtor(&args);
2617+
2618+
if (free_driver_options) {
2619+
efree(serializable_driver_options);
2620+
}
2621+
25552622
#else
25562623
zval* args;
25572624

@@ -2568,8 +2635,8 @@ static char* php_phongo_manager_make_client_hash(const char* uri_string, zval* o
25682635
}
25692636

25702637
if (driverOptions) {
2571-
ADD_ASSOC_ZVAL_EX(args, "driverOptions", driverOptions);
2572-
Z_ADDREF_P(driverOptions);
2638+
serializable_driver_options = php_phongo_manager_prepare_manager_for_hash(driverOptions, &free_driver_options TSRMLS_CC);
2639+
ADD_ASSOC_ZVAL_EX(args, "driverOptions", serializable_driver_options);
25732640
} else {
25742641
ADD_ASSOC_NULL_EX(args, "driverOptions");
25752642
}

0 commit comments

Comments
 (0)