Skip to content

Commit ad1f3f0

Browse files
committed
PHPC-939: Don't modify std props in BSON get_properties handlers
1 parent bac8cb3 commit ad1f3f0

File tree

10 files changed

+306
-40
lines changed

10 files changed

+306
-40
lines changed

php_phongo.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ zend_bool phongo_writeconcernerror_init(zval *return_value, bson_t *bson TSRMLS_
167167
ce->unserialize = zend_class_unserialize_deny; \
168168
} while(0);
169169

170+
#define PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, size) do { \
171+
if (is_debug) { \
172+
ALLOC_HASHTABLE(props); \
173+
zend_hash_init((props), (size), NULL, ZVAL_PTR_DTOR, 0); \
174+
} else if ((intern)->properties) { \
175+
zend_hash_clean((intern)->properties); \
176+
(props) = (intern)->properties; \
177+
} else { \
178+
ALLOC_HASHTABLE(props); \
179+
zend_hash_init((props), (size), NULL, ZVAL_PTR_DTOR, 0); \
180+
(intern)->properties = (props); \
181+
} \
182+
} while(0);
183+
170184
#endif /* PHONGO_H */
171185

172186

php_phongo_structs.h

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,27 @@ typedef struct {
139139

140140
typedef struct {
141141
PHONGO_ZEND_OBJECT_PRE
142-
char *data;
143-
int data_len;
144-
uint8_t type;
142+
char *data;
143+
int data_len;
144+
uint8_t type;
145+
HashTable *properties;
145146
PHONGO_ZEND_OBJECT_POST
146147
} php_phongo_binary_t;
147148

148149
typedef struct {
149150
PHONGO_ZEND_OBJECT_PRE
150-
bool initialized;
151-
bson_decimal128_t decimal;
151+
bool initialized;
152+
bson_decimal128_t decimal;
153+
HashTable *properties;
152154
PHONGO_ZEND_OBJECT_POST
153155
} php_phongo_decimal128_t;
154156

155157
typedef struct {
156158
PHONGO_ZEND_OBJECT_PRE
157-
char *code;
158-
size_t code_len;
159-
bson_t *scope;
159+
char *code;
160+
size_t code_len;
161+
bson_t *scope;
162+
HashTable *properties;
160163
PHONGO_ZEND_OBJECT_POST
161164
} php_phongo_javascript_t;
162165

@@ -172,32 +175,36 @@ typedef struct {
172175

173176
typedef struct {
174177
PHONGO_ZEND_OBJECT_PRE
175-
bool initialized;
176-
char oid[25];
178+
bool initialized;
179+
char oid[25];
180+
HashTable *properties;
177181
PHONGO_ZEND_OBJECT_POST
178182
} php_phongo_objectid_t;
179183

180184
typedef struct {
181185
PHONGO_ZEND_OBJECT_PRE
182-
char *pattern;
183-
int pattern_len;
184-
char *flags;
185-
int flags_len;
186+
char *pattern;
187+
int pattern_len;
188+
char *flags;
189+
int flags_len;
190+
HashTable *properties;
186191
PHONGO_ZEND_OBJECT_POST
187192
} php_phongo_regex_t;
188193

189194
typedef struct {
190195
PHONGO_ZEND_OBJECT_PRE
191-
bool initialized;
192-
uint32_t increment;
193-
uint32_t timestamp;
196+
bool initialized;
197+
uint32_t increment;
198+
uint32_t timestamp;
199+
HashTable *properties;
194200
PHONGO_ZEND_OBJECT_POST
195201
} php_phongo_timestamp_t;
196202

197203
typedef struct {
198204
PHONGO_ZEND_OBJECT_PRE
199-
bool initialized;
200-
int64_t milliseconds;
205+
bool initialized;
206+
int64_t milliseconds;
207+
HashTable *properties;
201208
PHONGO_ZEND_OBJECT_POST
202209
} php_phongo_utcdatetime_t;
203210

src/BSON/Binary.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ static void php_phongo_binary_free_object(phongo_free_object_arg *object TSRMLS_
336336
efree(intern->data);
337337
}
338338

339+
if (intern->properties) {
340+
zend_hash_destroy(intern->properties);
341+
FREE_HASHTABLE(intern->properties);
342+
}
343+
339344
#if PHP_VERSION_ID < 70000
340345
efree(intern);
341346
#endif
@@ -390,16 +395,17 @@ static HashTable *php_phongo_binary_get_gc(zval *object, phongo_get_gc_table tab
390395
*table = NULL;
391396
*n = 0;
392397

393-
return zend_std_get_properties(object TSRMLS_CC);
398+
return Z_BINARY_OBJ_P(object)->properties;
394399
} /* }}} */
395400

396-
static HashTable *php_phongo_binary_get_properties(zval *object TSRMLS_DC) /* {{{ */
401+
static HashTable *php_phongo_binary_get_properties_hash(zval *object, bool is_debug TSRMLS_DC) /* {{{ */
397402
{
398403
php_phongo_binary_t *intern;
399404
HashTable *props;
400405

401406
intern = Z_BINARY_OBJ_P(object);
402-
props = zend_std_get_properties(object TSRMLS_CC);
407+
408+
PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 2);
403409

404410
if (!intern->data) {
405411
return props;
@@ -431,6 +437,17 @@ static HashTable *php_phongo_binary_get_properties(zval *object TSRMLS_DC) /* {{
431437

432438
return props;
433439
} /* }}} */
440+
441+
static HashTable *php_phongo_binary_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
442+
{
443+
*is_temp = 1;
444+
return php_phongo_binary_get_properties_hash(object, true TSRMLS_CC);
445+
} /* }}} */
446+
447+
static HashTable *php_phongo_binary_get_properties(zval *object TSRMLS_DC) /* {{{ */
448+
{
449+
return php_phongo_binary_get_properties_hash(object, false TSRMLS_CC);
450+
} /* }}} */
434451
/* }}} */
435452

436453
void php_phongo_binary_init_ce(INIT_FUNC_ARGS) /* {{{ */
@@ -448,6 +465,7 @@ void php_phongo_binary_init_ce(INIT_FUNC_ARGS) /* {{{ */
448465

449466
memcpy(&php_phongo_handler_binary, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
450467
php_phongo_handler_binary.compare_objects = php_phongo_binary_compare_objects;
468+
php_phongo_handler_binary.get_debug_info = php_phongo_binary_get_debug_info;
451469
php_phongo_handler_binary.get_gc = php_phongo_binary_get_gc;
452470
php_phongo_handler_binary.get_properties = php_phongo_binary_get_properties;
453471
#if PHP_VERSION_ID >= 70000

src/BSON/Decimal128.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ static void php_phongo_decimal128_free_object(phongo_free_object_arg *object TSR
269269

270270
zend_object_std_dtor(&intern->std TSRMLS_CC);
271271

272+
if (intern->properties) {
273+
zend_hash_destroy(intern->properties);
274+
FREE_HASHTABLE(intern->properties);
275+
}
276+
272277
#if PHP_VERSION_ID < 70000
273278
efree(intern);
274279
#endif
@@ -303,17 +308,18 @@ static HashTable *php_phongo_decimal128_get_gc(zval *object, phongo_get_gc_table
303308
*table = NULL;
304309
*n = 0;
305310

306-
return zend_std_get_properties(object TSRMLS_CC);
311+
return Z_DECIMAL128_OBJ_P(object)->properties;
307312
} /* }}} */
308313

309-
static HashTable *php_phongo_decimal128_get_properties(zval *object TSRMLS_DC) /* {{{ */
314+
static HashTable *php_phongo_decimal128_get_properties_hash(zval *object, bool is_debug TSRMLS_DC) /* {{{ */
310315
{
311316
php_phongo_decimal128_t *intern;
312317
HashTable *props;
313318
char outbuf[BSON_DECIMAL128_STRING] = "";
314319

315320
intern = Z_DECIMAL128_OBJ_P(object);
316-
props = zend_std_get_properties(object TSRMLS_CC);
321+
322+
PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 1);
317323

318324
if (!intern->initialized) {
319325
return props;
@@ -340,6 +346,17 @@ static HashTable *php_phongo_decimal128_get_properties(zval *object TSRMLS_DC) /
340346

341347
return props;
342348
} /* }}} */
349+
350+
static HashTable *php_phongo_decimal128_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
351+
{
352+
*is_temp = 1;
353+
return php_phongo_decimal128_get_properties_hash(object, true TSRMLS_CC);
354+
} /* }}} */
355+
356+
static HashTable *php_phongo_decimal128_get_properties(zval *object TSRMLS_DC) /* {{{ */
357+
{
358+
return php_phongo_decimal128_get_properties_hash(object, false TSRMLS_CC);
359+
} /* }}} */
343360
/* }}} */
344361

345362
void php_phongo_decimal128_init_ce(INIT_FUNC_ARGS) /* {{{ */
@@ -356,6 +373,7 @@ void php_phongo_decimal128_init_ce(INIT_FUNC_ARGS) /* {{{ */
356373
zend_class_implements(php_phongo_decimal128_ce TSRMLS_CC, 1, zend_ce_serializable);
357374

358375
memcpy(&php_phongo_handler_decimal128, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
376+
php_phongo_handler_decimal128.get_debug_info = php_phongo_decimal128_get_debug_info;
359377
php_phongo_handler_decimal128.get_gc = php_phongo_decimal128_get_gc;
360378
php_phongo_handler_decimal128.get_properties = php_phongo_decimal128_get_properties;
361379
#if PHP_VERSION_ID >= 70000

src/BSON/Javascript.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ static void php_phongo_javascript_free_object(phongo_free_object_arg *object TSR
382382
intern->scope = NULL;
383383
}
384384

385+
if (intern->properties) {
386+
zend_hash_destroy(intern->properties);
387+
FREE_HASHTABLE(intern->properties);
388+
}
389+
385390
#if PHP_VERSION_ID < 70000
386391
efree(intern);
387392
#endif
@@ -426,16 +431,17 @@ static HashTable *php_phongo_javascript_get_gc(zval *object, phongo_get_gc_table
426431
*table = NULL;
427432
*n = 0;
428433

429-
return zend_std_get_properties(object TSRMLS_CC);
434+
return Z_JAVASCRIPT_OBJ_P(object)->properties;
430435
} /* }}} */
431436

432-
HashTable *php_phongo_javascript_get_properties(zval *object TSRMLS_DC) /* {{{ */
437+
HashTable *php_phongo_javascript_get_properties_hash(zval *object, bool is_debug TSRMLS_DC) /* {{{ */
433438
{
434439
php_phongo_javascript_t *intern;
435440
HashTable *props;
436441

437442
intern = Z_JAVASCRIPT_OBJ_P(object);
438-
props = zend_std_get_properties(object TSRMLS_CC);
443+
444+
PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 2);
439445

440446
if (!intern->code) {
441447
return props;
@@ -504,6 +510,17 @@ HashTable *php_phongo_javascript_get_properties(zval *object TSRMLS_DC) /* {{{ *
504510

505511
return props;
506512
} /* }}} */
513+
514+
static HashTable *php_phongo_javascript_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
515+
{
516+
*is_temp = 1;
517+
return php_phongo_javascript_get_properties_hash(object, true TSRMLS_CC);
518+
} /* }}} */
519+
520+
static HashTable *php_phongo_javascript_get_properties(zval *object TSRMLS_DC) /* {{{ */
521+
{
522+
return php_phongo_javascript_get_properties_hash(object, false TSRMLS_CC);
523+
} /* }}} */
507524
/* }}} */
508525

509526
void php_phongo_javascript_init_ce(INIT_FUNC_ARGS) /* {{{ */
@@ -521,6 +538,7 @@ void php_phongo_javascript_init_ce(INIT_FUNC_ARGS) /* {{{ */
521538

522539
memcpy(&php_phongo_handler_javascript, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
523540
php_phongo_handler_javascript.compare_objects = php_phongo_javascript_compare_objects;
541+
php_phongo_handler_javascript.get_debug_info = php_phongo_javascript_get_debug_info;
524542
php_phongo_handler_javascript.get_gc = php_phongo_javascript_get_gc;
525543
php_phongo_handler_javascript.get_properties = php_phongo_javascript_get_properties;
526544
#if PHP_VERSION_ID >= 70000

src/BSON/ObjectID.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ static void php_phongo_objectid_free_object(phongo_free_object_arg *object TSRML
307307

308308
zend_object_std_dtor(&intern->std TSRMLS_CC);
309309

310+
if (intern->properties) {
311+
zend_hash_destroy(intern->properties);
312+
FREE_HASHTABLE(intern->properties);
313+
}
314+
310315
#if PHP_VERSION_ID < 70000
311316
efree(intern);
312317
#endif
@@ -352,16 +357,17 @@ static HashTable *php_phongo_objectid_get_gc(zval *object, phongo_get_gc_table t
352357
*table = NULL;
353358
*n = 0;
354359

355-
return zend_std_get_properties(object TSRMLS_CC);
360+
return Z_OBJECTID_OBJ_P(object)->properties;
356361
} /* }}} */
357362

358-
static HashTable *php_phongo_objectid_get_properties(zval *object TSRMLS_DC) /* {{{ */
363+
static HashTable *php_phongo_objectid_get_properties_hash(zval *object, bool is_debug TSRMLS_DC) /* {{{ */
359364
{
360365
php_phongo_objectid_t *intern;
361366
HashTable *props;
362367

363368
intern = Z_OBJECTID_OBJ_P(object);
364-
props = zend_std_get_properties(object TSRMLS_CC);
369+
370+
PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 1);
365371

366372
if (!intern->oid) {
367373
return props;
@@ -386,6 +392,17 @@ static HashTable *php_phongo_objectid_get_properties(zval *object TSRMLS_DC) /*
386392

387393
return props;
388394
} /* }}} */
395+
396+
static HashTable *php_phongo_objectid_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
397+
{
398+
*is_temp = 1;
399+
return php_phongo_objectid_get_properties_hash(object, true TSRMLS_CC);
400+
} /* }}} */
401+
402+
static HashTable *php_phongo_objectid_get_properties(zval *object TSRMLS_DC) /* {{{ */
403+
{
404+
return php_phongo_objectid_get_properties_hash(object, false TSRMLS_CC);
405+
} /* }}} */
389406
/* }}} */
390407

391408
void php_phongo_objectid_init_ce(INIT_FUNC_ARGS) /* {{{ */
@@ -403,6 +420,7 @@ void php_phongo_objectid_init_ce(INIT_FUNC_ARGS) /* {{{ */
403420

404421
memcpy(&php_phongo_handler_objectid, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
405422
php_phongo_handler_objectid.compare_objects = php_phongo_objectid_compare_objects;
423+
php_phongo_handler_objectid.get_debug_info = php_phongo_objectid_get_debug_info;
406424
php_phongo_handler_objectid.get_gc = php_phongo_objectid_get_gc;
407425
php_phongo_handler_objectid.get_properties = php_phongo_objectid_get_properties;
408426
#if PHP_VERSION_ID >= 70000

0 commit comments

Comments
 (0)