Skip to content

Commit 2ef0ff2

Browse files
authored
PHPC-2457: Fix using array items by reference (#1697)
* Remove conditional definitions for zend_engine 2 * Inline compatibility macros for zend_engine 2 * Simplify switch and fix cast in php_array_api * Add deref variant to php_array_api * Correctly handle zval references when using php_array_fetch * Adapt formatting to match rest of file * Flip test names
1 parent 5ddc557 commit 2ef0ff2

File tree

11 files changed

+225
-198
lines changed

11 files changed

+225
-198
lines changed

src/MongoDB/BulkWrite.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static inline bool php_phongo_bulkwrite_bson_array_has_valid_keys(bson_t* array)
149149
* success; otherwise, false is returned and an exception is thrown. */
150150
static bool php_phongo_bulkwrite_opts_append_array(bson_t* opts, const char* key, zval* zarr)
151151
{
152-
zval* value = php_array_fetch(zarr, key);
152+
zval* value = php_array_fetch_deref(zarr, key);
153153
bson_t b = BSON_INITIALIZER;
154154

155155
if (Z_TYPE_P(value) != IS_OBJECT && Z_TYPE_P(value) != IS_ARRAY) {
@@ -185,7 +185,7 @@ static bool php_phongo_bulkwrite_opts_append_array(bson_t* opts, const char* key
185185
* success; otherwise, false is returned and an exception is thrown. */
186186
static bool php_phongo_bulkwrite_opts_append_document(bson_t* opts, const char* key, zval* zarr)
187187
{
188-
zval* value = php_array_fetch(zarr, key);
188+
zval* value = php_array_fetch_deref(zarr, key);
189189
bson_t b = BSON_INITIALIZER;
190190

191191
if (Z_TYPE_P(value) != IS_OBJECT && Z_TYPE_P(value) != IS_ARRAY) {
@@ -246,10 +246,10 @@ static bool php_phongo_bulkwrite_opt_hint(bson_t* boptions, zval* zoptions)
246246
/* The "hint" option (or "$hint" modifier) must be a string or document.
247247
* Check for both types and merge into BSON options accordingly. */
248248
if (zoptions && php_array_existsc(zoptions, "hint")) {
249-
zend_uchar type = Z_TYPE_P(php_array_fetchc(zoptions, "hint"));
249+
zend_uchar type = Z_TYPE_P(php_array_fetchc_deref(zoptions, "hint"));
250250

251251
if (type == IS_STRING) {
252-
zval* value = php_array_fetchc(zoptions, "hint");
252+
zval* value = php_array_fetchc_deref(zoptions, "hint");
253253

254254
if (!bson_append_utf8(boptions, "hint", 4, Z_STRVAL_P(value), Z_STRLEN_P(value))) {
255255
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Error appending \"hint\" option");
@@ -343,7 +343,7 @@ static PHP_METHOD(MongoDB_Driver_BulkWrite, __construct)
343343
}
344344

345345
if (options && php_array_existsc(options, "let")) {
346-
zval* value = php_array_fetch(options, "let");
346+
zval* value = php_array_fetchc_deref(options, "let");
347347

348348
if (Z_TYPE_P(value) != IS_OBJECT && Z_TYPE_P(value) != IS_ARRAY) {
349349
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"let\" option to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(value)));
@@ -361,7 +361,7 @@ static PHP_METHOD(MongoDB_Driver_BulkWrite, __construct)
361361
}
362362

363363
if (options && php_array_existsc(options, "comment")) {
364-
zval* value = php_array_fetch(options, "comment");
364+
zval* value = php_array_fetchc_deref(options, "comment");
365365

366366
intern->comment = ecalloc(1, sizeof(bson_value_t));
367367
phongo_zval_to_bson_value(value, intern->comment);

src/MongoDB/ClientEncryption.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ static PHP_METHOD(MongoDB_Driver_ClientEncryption, rewrapManyDataKey)
426426
}
427427

428428
if (options && php_array_existsc(options, "masterKey")) {
429-
zval* zmasterkey = php_array_fetchc(options, "masterKey");
429+
zval* zmasterkey = php_array_fetchc_deref(options, "masterKey");
430430

431431
if (Z_TYPE_P(zmasterkey) != IS_OBJECT && Z_TYPE_P(zmasterkey) != IS_ARRAY) {
432432
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"masterKey\" option to be array or object, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(zmasterkey));
@@ -550,7 +550,7 @@ static mongoc_client_encryption_opts_t* phongo_clientencryption_opts_from_zval(z
550550
}
551551

552552
if (php_array_existsc(options, "keyVaultClient")) {
553-
zval* key_vault_client = php_array_fetch(options, "keyVaultClient");
553+
zval* key_vault_client = php_array_fetchc_deref(options, "keyVaultClient");
554554

555555
if (Z_TYPE_P(key_vault_client) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(key_vault_client), php_phongo_manager_ce)) {
556556
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"keyVaultClient\" option to be %s, %s given", ZSTR_VAL(php_phongo_manager_ce->name), PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(key_vault_client));
@@ -598,7 +598,7 @@ static mongoc_client_encryption_opts_t* phongo_clientencryption_opts_from_zval(z
598598
}
599599

600600
if (php_array_existsc(options, "kmsProviders")) {
601-
zval* kms_providers = php_array_fetchc(options, "kmsProviders");
601+
zval* kms_providers = php_array_fetchc_deref(options, "kmsProviders");
602602
bson_t bson_providers = BSON_INITIALIZER;
603603

604604
if (Z_TYPE_P(kms_providers) != IS_ARRAY && Z_TYPE_P(kms_providers) != IS_OBJECT) {
@@ -616,7 +616,7 @@ static mongoc_client_encryption_opts_t* phongo_clientencryption_opts_from_zval(z
616616
}
617617

618618
if (php_array_existsc(options, "tlsOptions")) {
619-
zval* tls_options = php_array_fetchc(options, "tlsOptions");
619+
zval* tls_options = php_array_fetchc_deref(options, "tlsOptions");
620620
bson_t bson_options = BSON_INITIALIZER;
621621

622622
if (Z_TYPE_P(tls_options) != IS_ARRAY && Z_TYPE_P(tls_options) != IS_OBJECT) {
@@ -707,7 +707,7 @@ static mongoc_client_encryption_datakey_opts_t* phongo_clientencryption_datakey_
707707
}
708708

709709
if (php_array_existsc(options, "keyAltNames")) {
710-
zval* zkeyaltnames = php_array_fetchc(options, "keyAltNames");
710+
zval* zkeyaltnames = php_array_fetchc_deref(options, "keyAltNames");
711711
HashTable* ht_data;
712712
uint32_t keyaltnames_count;
713713
char** keyaltnames;
@@ -769,7 +769,7 @@ static mongoc_client_encryption_datakey_opts_t* phongo_clientencryption_datakey_
769769
}
770770

771771
if (php_array_existsc(options, "keyMaterial")) {
772-
zval* keyMaterial = php_array_fetchc(options, "keyMaterial");
772+
zval* keyMaterial = php_array_fetchc_deref(options, "keyMaterial");
773773

774774
if (Z_TYPE_P(keyMaterial) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(keyMaterial), php_phongo_binary_ce)) {
775775
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"keyMaterial\" option to be %s, %s given", ZSTR_VAL(php_phongo_binary_ce->name), PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(keyMaterial));
@@ -780,7 +780,7 @@ static mongoc_client_encryption_datakey_opts_t* phongo_clientencryption_datakey_
780780
}
781781

782782
if (php_array_existsc(options, "masterKey")) {
783-
zval* zmasterkey = php_array_fetchc(options, "masterKey");
783+
zval* zmasterkey = php_array_fetchc_deref(options, "masterKey");
784784
bson_t masterkey = BSON_INITIALIZER;
785785

786786
if (Z_TYPE_P(zmasterkey) != IS_OBJECT && Z_TYPE_P(zmasterkey) != IS_ARRAY) {
@@ -932,7 +932,7 @@ static mongoc_client_encryption_encrypt_opts_t* phongo_clientencryption_encrypt_
932932
}
933933

934934
if (php_array_existsc(options, "contentionFactor")) {
935-
mongoc_client_encryption_encrypt_opts_set_contention_factor(opts, php_array_fetch_long(options, "contentionFactor"));
935+
mongoc_client_encryption_encrypt_opts_set_contention_factor(opts, php_array_fetchc_long(options, "contentionFactor"));
936936
}
937937

938938
if (php_array_existsc(options, "keyId")) {
@@ -954,7 +954,7 @@ static mongoc_client_encryption_encrypt_opts_t* phongo_clientencryption_encrypt_
954954
int plen;
955955
zend_bool pfree;
956956

957-
keyaltname = php_array_fetch_string(options, "keyAltName", &plen, &pfree);
957+
keyaltname = php_array_fetchc_string(options, "keyAltName", &plen, &pfree);
958958
mongoc_client_encryption_encrypt_opts_set_keyaltname(opts, keyaltname);
959959

960960
if (pfree) {
@@ -967,7 +967,7 @@ static mongoc_client_encryption_encrypt_opts_t* phongo_clientencryption_encrypt_
967967
int plen;
968968
zend_bool pfree;
969969

970-
algorithm = php_array_fetch_string(options, "algorithm", &plen, &pfree);
970+
algorithm = php_array_fetchc_string(options, "algorithm", &plen, &pfree);
971971
mongoc_client_encryption_encrypt_opts_set_algorithm(opts, algorithm);
972972

973973
if (pfree) {
@@ -980,7 +980,7 @@ static mongoc_client_encryption_encrypt_opts_t* phongo_clientencryption_encrypt_
980980
int plen;
981981
zend_bool pfree;
982982

983-
querytype = php_array_fetch_string(options, "queryType", &plen, &pfree);
983+
querytype = php_array_fetchc_string(options, "queryType", &plen, &pfree);
984984
mongoc_client_encryption_encrypt_opts_set_query_type(opts, querytype);
985985

986986
if (pfree) {
@@ -991,7 +991,7 @@ static mongoc_client_encryption_encrypt_opts_t* phongo_clientencryption_encrypt_
991991
if (php_array_existsc(options, "rangeOpts")) {
992992
mongoc_client_encryption_encrypt_range_opts_t* range_opts;
993993

994-
range_opts = phongo_clientencryption_encrypt_range_opts_from_zval(php_array_fetchc(options, "rangeOpts"));
994+
range_opts = phongo_clientencryption_encrypt_range_opts_from_zval(php_array_fetchc_deref(options, "rangeOpts"));
995995

996996
if (!range_opts) {
997997
/* Exception already thrown */

src/MongoDB/Manager.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static bool php_phongo_manager_merge_context_options(zval* zdriverOptions)
7070
return true;
7171
}
7272

73-
zcontext = php_array_fetchc(zdriverOptions, "context");
73+
zcontext = php_array_fetchc_deref(zdriverOptions, "context");
7474
context = php_stream_context_from_zval(zcontext, 1);
7575

7676
if (!context) {
@@ -734,7 +734,7 @@ static PHP_METHOD(MongoDB_Driver_Manager, startSession)
734734
}
735735

736736
if (options && php_array_existsc(options, "defaultTransactionOptions")) {
737-
zval* txn_options = php_array_fetchc(options, "defaultTransactionOptions");
737+
zval* txn_options = php_array_fetchc_deref(options, "defaultTransactionOptions");
738738

739739
/* Thrown exception and return if the defaultTransactionOptions is not an array */
740740
if (Z_TYPE_P(txn_options) != IS_ARRAY) {

src/MongoDB/Query.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ zend_class_entry* php_phongo_query_ce;
3636
* otherwise, false is returned and an exception is thrown. */
3737
static bool php_phongo_query_opts_append_string(bson_t* opts, const char* opts_key, zval* zarr, const char* zarr_key)
3838
{
39-
zval* value = php_array_fetch(zarr, zarr_key);
39+
zval* value = php_array_fetch_deref(zarr, zarr_key);
4040

4141
if (Z_TYPE_P(value) != IS_STRING) {
4242
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"%s\" %s to be string, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(value));
@@ -55,7 +55,7 @@ static bool php_phongo_query_opts_append_string(bson_t* opts, const char* opts_k
5555
* success; otherwise, false is returned and an exception is thrown. */
5656
static bool php_phongo_query_opts_append_document(bson_t* opts, const char* opts_key, zval* zarr, const char* zarr_key)
5757
{
58-
zval* value = php_array_fetch(zarr, zarr_key);
58+
zval* value = php_array_fetch_deref(zarr, zarr_key);
5959
bson_t b = BSON_INITIALIZER;
6060

6161
if (Z_TYPE_P(value) != IS_OBJECT && Z_TYPE_P(value) != IS_ARRAY) {
@@ -172,7 +172,7 @@ static bool php_phongo_query_init_hint(php_phongo_query_t* intern, zval* options
172172
/* The "hint" option (or "$hint" modifier) must be a string or document.
173173
* Check for both types and merge into BSON options accordingly. */
174174
if (php_array_existsc(options, "hint")) {
175-
zend_uchar type = Z_TYPE_P(php_array_fetchc(options, "hint"));
175+
zend_uchar type = Z_TYPE_P(php_array_fetchc_deref(options, "hint"));
176176

177177
if (type == IS_STRING) {
178178
PHONGO_QUERY_OPT_STRING("hint", options, "hint");
@@ -183,7 +183,7 @@ static bool php_phongo_query_init_hint(php_phongo_query_t* intern, zval* options
183183
return false;
184184
}
185185
} else if (modifiers && php_array_existsc(modifiers, "$hint")) {
186-
zend_uchar type = Z_TYPE_P(php_array_fetchc(modifiers, "$hint"));
186+
zend_uchar type = Z_TYPE_P(php_array_fetchc_deref(modifiers, "$hint"));
187187

188188
if (type == IS_STRING) {
189189
PHONGO_QUERY_OPT_STRING("hint", modifiers, "$hint");
@@ -244,7 +244,7 @@ static bool php_phongo_query_init_readconcern(php_phongo_query_t* intern, zval*
244244
return true;
245245
}
246246

247-
read_concern = php_array_fetchc(options, "readConcern");
247+
read_concern = php_array_fetchc_deref(options, "readConcern");
248248

249249
if (Z_TYPE_P(read_concern) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(read_concern), php_phongo_readconcern_ce)) {
250250
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"readConcern\" option to be %s, %s given", ZSTR_VAL(php_phongo_readconcern_ce->name), PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(read_concern));
@@ -331,7 +331,7 @@ bool phongo_query_init(zval* return_value, zval* filter, zval* options)
331331
}
332332

333333
if (php_array_existsc(options, "modifiers")) {
334-
modifiers = php_array_fetchc(options, "modifiers");
334+
modifiers = php_array_fetchc_deref(options, "modifiers");
335335

336336
if (Z_TYPE_P(modifiers) != IS_ARRAY) {
337337
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"modifiers\" option to be array, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(modifiers));

src/MongoDB/ReadPreference.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, __construct)
273273
}
274274

275275
if (options && php_array_exists(options, "hedge")) {
276-
zval* hedge = php_array_fetchc(options, "hedge");
276+
zval* hedge = php_array_fetchc_deref(options, "hedge");
277277

278278
if (Z_TYPE_P(hedge) == IS_ARRAY || Z_TYPE_P(hedge) == IS_OBJECT) {
279279
bson_t* hedge_doc = bson_new();

src/MongoDB/Session.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ mongoc_transaction_opt_t* php_mongodb_session_parse_transaction_options(zval* op
378378
}
379379

380380
if (php_array_existsc(options, "readConcern")) {
381-
zval* read_concern = php_array_fetchc(options, "readConcern");
381+
zval* read_concern = php_array_fetchc_deref(options, "readConcern");
382382

383383
if (Z_TYPE_P(read_concern) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(read_concern), php_phongo_readconcern_ce)) {
384384
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"readConcern\" option to be %s, %s given", ZSTR_VAL(php_phongo_readconcern_ce->name), PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(read_concern));
@@ -396,7 +396,7 @@ mongoc_transaction_opt_t* php_mongodb_session_parse_transaction_options(zval* op
396396
}
397397

398398
if (php_array_existsc(options, "readPreference")) {
399-
zval* read_preference = php_array_fetchc(options, "readPreference");
399+
zval* read_preference = php_array_fetchc_deref(options, "readPreference");
400400

401401
if (Z_TYPE_P(read_preference) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(read_preference), php_phongo_readpreference_ce)) {
402402
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"readPreference\" option to be %s, %s given", ZSTR_VAL(php_phongo_readpreference_ce->name), PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(read_preference));
@@ -414,7 +414,7 @@ mongoc_transaction_opt_t* php_mongodb_session_parse_transaction_options(zval* op
414414
}
415415

416416
if (php_array_existsc(options, "writeConcern")) {
417-
zval* write_concern = php_array_fetchc(options, "writeConcern");
417+
zval* write_concern = php_array_fetchc_deref(options, "writeConcern");
418418

419419
if (Z_TYPE_P(write_concern) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(write_concern), php_phongo_writeconcern_ce)) {
420420
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"writeConcern\" option to be %s, %s given", ZSTR_VAL(php_phongo_writeconcern_ce->name), PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(write_concern));

0 commit comments

Comments
 (0)