Skip to content

Commit 4d58b2c

Browse files
committed
Merge pull request #1033
2 parents f6d1469 + fafe503 commit 4d58b2c

14 files changed

+88
-73
lines changed

php_bson.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,16 @@ typedef struct {
8383
php_phongo_field_path* field_path;
8484
} php_phongo_bson_state;
8585

86-
#if PHP_VERSION_ID >= 70000
87-
#define PHONGO_BSON_STATE_INITIALIZER \
88-
{ \
89-
{ { 0 } }, { PHONGO_TYPEMAP_NONE, NULL, PHONGO_TYPEMAP_NONE, NULL, PHONGO_TYPEMAP_NONE, NULL }, NULL, NULL \
90-
}
91-
#else
92-
#define PHONGO_BSON_STATE_INITIALIZER \
93-
{ \
94-
NULL, { PHONGO_TYPEMAP_NONE, NULL, PHONGO_TYPEMAP_NONE, NULL, PHONGO_TYPEMAP_NONE, NULL }, NULL, NULL \
95-
}
96-
#endif
86+
#define PHONGO_BSON_INIT_STATE(s) \
87+
do { \
88+
memset(&(s), 0, sizeof(php_phongo_bson_state)); \
89+
} while (0)
90+
#define PHONGO_BSON_INIT_DEBUG_STATE(s) \
91+
do { \
92+
memset(&(s), 0, sizeof(php_phongo_bson_state)); \
93+
s.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY; \
94+
s.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY; \
95+
} while (0)
9796

9897
void php_phongo_zval_to_bson(zval* data, php_phongo_bson_flags_t flags, bson_t* bson, bson_t** bson_out TSRMLS_DC);
9998
bool php_phongo_bson_to_zval_ex(const unsigned char* data, int data_len, php_phongo_bson_state* state);

php_phongo.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,12 +1182,9 @@ void php_phongo_server_to_zval(zval* retval, mongoc_server_description_t* sd) /*
11821182
if (bson_iter_init_find(&iter, is_master, "tags") && BSON_ITER_HOLDS_DOCUMENT(&iter)) {
11831183
const uint8_t* bytes;
11841184
uint32_t len;
1185-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
1186-
1187-
/* Use native arrays for debugging output */
1188-
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
1189-
state.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
1185+
php_phongo_bson_state state;
11901186

1187+
PHONGO_BSON_INIT_DEBUG_STATE(state);
11911188
bson_iter_document(&iter, &len, &bytes);
11921189
php_phongo_bson_to_zval_ex(bytes, len, &state);
11931190

@@ -1199,11 +1196,9 @@ void php_phongo_server_to_zval(zval* retval, mongoc_server_description_t* sd) /*
11991196
}
12001197

12011198
{
1202-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
1203-
/* Use native arrays for debugging output */
1204-
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
1205-
state.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
1199+
php_phongo_bson_state state;
12061200

1201+
PHONGO_BSON_INIT_DEBUG_STATE(state);
12071202
php_phongo_bson_to_zval_ex(bson_get_data(is_master), is_master->len, &state);
12081203

12091204
#if PHP_VERSION_ID >= 70000

src/BSON/Javascript.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ static PHP_METHOD(Javascript, getScope)
177177
}
178178

179179
if (intern->scope->len) {
180-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
180+
php_phongo_bson_state state;
181181

182+
PHONGO_BSON_INIT_STATE(state);
182183
php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state);
183184
#if PHP_VERSION_ID >= 70000
184185
RETURN_ZVAL(&state.zchild, 0, 1);
@@ -206,8 +207,9 @@ static PHP_METHOD(Javascript, jsonSerialize)
206207
ADD_ASSOC_STRINGL(return_value, "$code", intern->code, intern->code_len);
207208

208209
if (intern->scope && intern->scope->len) {
209-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
210+
php_phongo_bson_state state;
210211

212+
PHONGO_BSON_INIT_STATE(state);
211213
if (php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
212214
#if PHP_VERSION_ID >= 70000
213215
Z_ADDREF(state.zchild);
@@ -228,10 +230,12 @@ static PHP_METHOD(Javascript, serialize)
228230
{
229231
php_phongo_javascript_t* intern;
230232
ZVAL_RETVAL_TYPE retval;
231-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
233+
php_phongo_bson_state state;
232234
php_serialize_data_t var_hash;
233235
smart_str buf = { 0 };
234236

237+
PHONGO_BSON_INIT_STATE(state);
238+
235239
intern = Z_JAVASCRIPT_OBJ_P(getThis());
236240

237241
if (zend_parse_parameters_none() == FAILURE) {
@@ -451,8 +455,9 @@ HashTable* php_phongo_javascript_get_properties_hash(zval* object, bool is_debug
451455
zend_hash_str_update(props, "code", sizeof("code") - 1, &code);
452456

453457
if (intern->scope) {
454-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
458+
php_phongo_bson_state state;
455459

460+
PHONGO_BSON_INIT_STATE(state);
456461
if (php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
457462
Z_ADDREF(state.zchild);
458463
zend_hash_str_update(props, "scope", sizeof("scope") - 1, &state.zchild);
@@ -480,8 +485,9 @@ HashTable* php_phongo_javascript_get_properties_hash(zval* object, bool is_debug
480485
zend_hash_update(props, "code", sizeof("code"), &code, sizeof(code), NULL);
481486

482487
if (intern->scope) {
483-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
488+
php_phongo_bson_state state;
484489

490+
PHONGO_BSON_INIT_STATE(state);
485491
if (php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
486492
Z_ADDREF_P(state.zchild);
487493
zend_hash_update(props, "scope", sizeof("scope"), &state.zchild, sizeof(state.zchild), NULL);

src/BSON/functions.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ PHP_FUNCTION(MongoDB_BSON_toPHP)
5555
char* data;
5656
phongo_zpp_char_len data_len;
5757
zval* typemap = NULL;
58-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
58+
php_phongo_bson_state state;
59+
60+
PHONGO_BSON_INIT_STATE(state);
5961

6062
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a!", &data, &data_len, &typemap) == FAILURE) {
6163
return;

src/MongoDB/BulkWrite.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ zend_class_entry* php_phongo_bulkwrite_ce;
3434
/* Extracts the "_id" field of a BSON document into a return value. */
3535
static void php_phongo_bulkwrite_extract_id(bson_t* doc, zval** return_value) /* {{{ */
3636
{
37-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
38-
zval* id = NULL;
37+
zval* id = NULL;
38+
php_phongo_bson_state state;
3939

40+
PHONGO_BSON_INIT_STATE(state);
4041
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
4142

4243
if (!php_phongo_bson_to_zval_ex(bson_get_data(doc), doc->len, &state)) {

src/MongoDB/Cursor.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,12 @@ static zend_object_iterator* php_phongo_cursor_get_iterator(zend_class_entry* ce
222222
static PHP_METHOD(Cursor, setTypeMap)
223223
{
224224
php_phongo_cursor_t* intern;
225-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
225+
php_phongo_bson_state state;
226226
zval* typemap = NULL;
227227
bool restore_current_element = false;
228228

229+
PHONGO_BSON_INIT_STATE(state);
230+
229231
intern = Z_CURSOR_OBJ_P(getThis());
230232

231233
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!", &typemap) == FAILURE) {

src/MongoDB/Monitoring/CommandFailedEvent.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ PHP_METHOD(CommandFailedEvent, getOperationId)
9797
PHP_METHOD(CommandFailedEvent, getReply)
9898
{
9999
php_phongo_commandfailedevent_t* intern;
100-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
100+
php_phongo_bson_state state;
101+
102+
PHONGO_BSON_INIT_STATE(state);
101103

102104
intern = Z_COMMANDFAILEDEVENT_OBJ_P(getThis());
103105

@@ -226,7 +228,9 @@ static HashTable* php_phongo_commandfailedevent_get_debug_info(zval* object, int
226228
php_phongo_commandfailedevent_t* intern;
227229
zval retval = ZVAL_STATIC_INIT;
228230
char operation_id[20], request_id[20];
229-
php_phongo_bson_state reply_state = PHONGO_BSON_STATE_INITIALIZER;
231+
php_phongo_bson_state reply_state;
232+
233+
PHONGO_BSON_INIT_STATE(reply_state);
230234

231235
intern = Z_COMMANDFAILEDEVENT_OBJ_P(object);
232236
*is_temp = 1;

src/MongoDB/Monitoring/CommandStartedEvent.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ zend_class_entry* php_phongo_commandstartedevent_ce;
3131
PHP_METHOD(CommandStartedEvent, getCommand)
3232
{
3333
php_phongo_commandstartedevent_t* intern;
34-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
34+
php_phongo_bson_state state;
35+
36+
PHONGO_BSON_INIT_STATE(state);
3537

3638
intern = Z_COMMANDSTARTEDEVENT_OBJ_P(getThis());
3739

@@ -204,7 +206,9 @@ static HashTable* php_phongo_commandstartedevent_get_debug_info(zval* object, in
204206
php_phongo_commandstartedevent_t* intern;
205207
zval retval = ZVAL_STATIC_INIT;
206208
char operation_id[20], request_id[20];
207-
php_phongo_bson_state command_state = PHONGO_BSON_STATE_INITIALIZER;
209+
php_phongo_bson_state command_state;
210+
211+
PHONGO_BSON_INIT_STATE(command_state);
208212

209213
intern = Z_COMMANDSTARTEDEVENT_OBJ_P(object);
210214
*is_temp = 1;

src/MongoDB/Monitoring/CommandSucceededEvent.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ PHP_METHOD(CommandSucceededEvent, getOperationId)
7878
PHP_METHOD(CommandSucceededEvent, getReply)
7979
{
8080
php_phongo_commandsucceededevent_t* intern;
81-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
81+
php_phongo_bson_state state;
82+
83+
PHONGO_BSON_INIT_STATE(state);
8284

8385
intern = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(getThis());
8486

@@ -201,7 +203,9 @@ static HashTable* php_phongo_commandsucceededevent_get_debug_info(zval* object,
201203
php_phongo_commandsucceededevent_t* intern;
202204
zval retval = ZVAL_STATIC_INIT;
203205
char operation_id[20], request_id[20];
204-
php_phongo_bson_state reply_state = PHONGO_BSON_STATE_INITIALIZER;
206+
php_phongo_bson_state reply_state;
207+
208+
PHONGO_BSON_INIT_STATE(reply_state);
205209

206210
intern = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(object);
207211
*is_temp = 1;

src/MongoDB/ReadPreference.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,9 @@ static PHP_METHOD(ReadPreference, getTagSets)
359359
tags = mongoc_read_prefs_get_tags(intern->read_preference);
360360

361361
if (tags->len) {
362-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
363-
/* Use native arrays for debugging output */
364-
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
365-
state.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
362+
php_phongo_bson_state state;
366363

364+
PHONGO_BSON_INIT_DEBUG_STATE(state);
367365
php_phongo_bson_to_zval_ex(bson_get_data(tags), tags->len, &state);
368366
#if PHP_VERSION_ID >= 70000
369367
RETURN_ZVAL(&state.zchild, 0, 1);
@@ -431,10 +429,12 @@ static HashTable* php_phongo_readpreference_get_properties_hash(zval* object, bo
431429
}
432430

433431
if (!bson_empty0(tags)) {
432+
php_phongo_bson_state state;
433+
434434
/* Use PHONGO_TYPEMAP_NATIVE_ARRAY for the root type since tags is an
435435
* array; however, inner documents and arrays can use the default. */
436-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
437-
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
436+
PHONGO_BSON_INIT_STATE(state);
437+
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
438438

439439
php_phongo_bson_to_zval_ex(bson_get_data(tags), tags->len, &state);
440440
#if PHP_VERSION_ID >= 70000
@@ -522,11 +522,9 @@ static PHP_METHOD(ReadPreference, serialize)
522522
}
523523

524524
if (!bson_empty0(tags)) {
525-
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
526-
/* Use native arrays for debugging output */
527-
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
528-
state.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
525+
php_phongo_bson_state state;
529526

527+
PHONGO_BSON_INIT_DEBUG_STATE(state);
530528
php_phongo_bson_to_zval_ex(bson_get_data(tags), tags->len, &state);
531529
#if PHP_VERSION_ID >= 70000
532530
ADD_ASSOC_ZVAL_EX(&retval, "tags", &state.zchild);

0 commit comments

Comments
 (0)