Skip to content

Commit 1ac1746

Browse files
committed
Merge pull request #346
2 parents c7a418f + 66822bf commit 1ac1746

File tree

59 files changed

+1033
-155
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1033
-155
lines changed

phongo_compat.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,6 @@
8585
# define ARG_UNUSED
8686
#endif
8787

88-
#ifdef HAVE_ATOLL
89-
# define STRTOLL(s) atoll(s)
90-
#else
91-
# if defined(PHP_WIN32)
92-
# define STRTOLL(s) _atoi64(s)
93-
# else
94-
# define STRTOLL(s) strtoll(s, NULL, 10)
95-
# endif
96-
#endif
97-
9888
#ifndef php_ignore_value
9989
# if defined(__GNUC__) && __GNUC__ >= 4
10090
# define php_ignore_value(x) (({ __typeof__ (x) __x = (x); (void) __x; }))
@@ -116,7 +106,6 @@
116106
# error Unsupported architecture (integers are neither 32-bit nor 64-bit)
117107
#endif
118108
# define SIZEOF_PHONGO_LONG SIZEOF_ZEND_LONG
119-
# define phongo_str(str) (str)->val
120109
# define phongo_create_object_retval zend_object*
121110
# define PHONGO_ALLOC_OBJECT_T(_obj_t, _class_type) (_obj_t *)ecalloc(1, sizeof(_obj_t)+zend_object_properties_size(_class_type))
122111
# define PHONGO_TSRMLS_FETCH_FROM_CTX(user_data)
@@ -147,7 +136,7 @@
147136
# define phongo_long long
148137
# define PHONGO_LONG_FORMAT "ld"
149138
# define SIZEOF_PHONGO_LONG SIZEOF_LONG
150-
# define phongo_str(str) str
139+
# define ZSTR_VAL(str) str
151140
# define phongo_create_object_retval zend_object_value
152141
# define PHONGO_ALLOC_OBJECT_T(_obj_t, _class_type) (_obj_t *)ecalloc(1, sizeof(_obj_t))
153142
# define PHONGO_TSRMLS_FETCH_FROM_CTX(user_data) TSRMLS_FETCH_FROM_CTX(user_data)

php_phongo.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ static void php_phongo_log(mongoc_log_level_t log_level, const char *log_domain,
224224
time(&t);
225225
dt = php_format_date((char *)"Y-m-d\\TH:i:sP", strlen("Y-m-d\\TH:i:sP"), t, 0 TSRMLS_CC);
226226

227-
fprintf(MONGODB_G(debug_fd), "[%s] %10s: %-8s> %s\n", phongo_str(dt), log_domain, mongoc_log_level_str(log_level), message);
227+
fprintf(MONGODB_G(debug_fd), "[%s] %10s: %-8s> %s\n", ZSTR_VAL(dt), log_domain, mongoc_log_level_str(log_level), message);
228228
fflush(MONGODB_G(debug_fd));
229229
efree(dt);
230230
} break;
@@ -1236,7 +1236,7 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
12361236
zend_restore_error_handling(&error_handling TSRMLS_CC);
12371237

12381238
if (!stream) {
1239-
bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_CONNECT, "Failed connecting to '%s:%d': %s", host->host, host->port, phongo_str(errmsg));
1239+
bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_CONNECT, "Failed connecting to '%s:%d': %s", host->host, host->port, ZSTR_VAL(errmsg));
12401240
efree(dsn);
12411241
efree(uniqid);
12421242
if (errmsg) {
@@ -2342,11 +2342,11 @@ ZEND_INI_MH(OnUpdateDebug)
23422342
MONGODB_G(debug_fd) = NULL;
23432343
}
23442344

2345-
if (!new_value || (new_value && !phongo_str(new_value)[0])
2346-
|| strcasecmp("0", phongo_str(new_value)) == 0
2347-
|| strcasecmp("off", phongo_str(new_value)) == 0
2348-
|| strcasecmp("no", phongo_str(new_value)) == 0
2349-
|| strcasecmp("false", phongo_str(new_value)) == 0
2345+
if (!new_value || (new_value && !ZSTR_VAL(new_value)[0])
2346+
|| strcasecmp("0", ZSTR_VAL(new_value)) == 0
2347+
|| strcasecmp("off", ZSTR_VAL(new_value)) == 0
2348+
|| strcasecmp("no", ZSTR_VAL(new_value)) == 0
2349+
|| strcasecmp("false", ZSTR_VAL(new_value)) == 0
23502350
) {
23512351
mongoc_log_trace_disable();
23522352
mongoc_log_set_handler(NULL, NULL);
@@ -2359,19 +2359,19 @@ ZEND_INI_MH(OnUpdateDebug)
23592359
}
23602360

23612361

2362-
if (strcasecmp(phongo_str(new_value), "stderr") == 0) {
2362+
if (strcasecmp(ZSTR_VAL(new_value), "stderr") == 0) {
23632363
MONGODB_G(debug_fd) = stderr;
2364-
} else if (strcasecmp(phongo_str(new_value), "stdout") == 0) {
2364+
} else if (strcasecmp(ZSTR_VAL(new_value), "stdout") == 0) {
23652365
MONGODB_G(debug_fd) = stdout;
23662366
} else if (
2367-
strcasecmp("1", phongo_str(new_value)) == 0
2368-
|| strcasecmp("on", phongo_str(new_value)) == 0
2369-
|| strcasecmp("yes", phongo_str(new_value)) == 0
2370-
|| strcasecmp("true", phongo_str(new_value)) == 0
2367+
strcasecmp("1", ZSTR_VAL(new_value)) == 0
2368+
|| strcasecmp("on", ZSTR_VAL(new_value)) == 0
2369+
|| strcasecmp("yes", ZSTR_VAL(new_value)) == 0
2370+
|| strcasecmp("true", ZSTR_VAL(new_value)) == 0
23712371
) {
23722372
tmp_dir = NULL;
23732373
} else {
2374-
tmp_dir = phongo_str(new_value);
2374+
tmp_dir = ZSTR_VAL(new_value);
23752375
}
23762376

23772377
if (!MONGODB_G(debug_fd)) {
@@ -2386,7 +2386,7 @@ ZEND_INI_MH(OnUpdateDebug)
23862386

23872387
fd = php_open_temporary_fd(tmp_dir, prefix, &filename TSRMLS_CC);
23882388
if (fd != -1) {
2389-
const char *path = phongo_str(filename);
2389+
const char *path = ZSTR_VAL(filename);
23902390
MONGODB_G(debug_fd) = VCWD_FOPEN(path, "a");
23912391
}
23922392
efree(filename);

src/BSON/Binary.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ PHONGO_API zend_class_entry *php_phongo_binary_ce;
4646

4747
zend_object_handlers php_phongo_handler_binary;
4848

49-
/* Initialize the object from a string and return whether it was successful. */
50-
static bool php_phongo_binary_init(php_phongo_binary_t *intern, const char *data, phongo_zpp_char_len data_len, phongo_long type)
49+
/* Initialize the object and return whether it was successful. An exception will
50+
* be thrown on error. */
51+
static bool php_phongo_binary_init(php_phongo_binary_t *intern, const char *data, phongo_zpp_char_len data_len, phongo_long type TSRMLS_DC)
5152
{
5253
if (type < 0 || type > UINT8_MAX) {
54+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected type to be an unsigned 8-bit integer, %" PHONGO_LONG_FORMAT " given", type);
5355
return false;
5456
}
5557

@@ -60,24 +62,27 @@ static bool php_phongo_binary_init(php_phongo_binary_t *intern, const char *data
6062
return true;
6163
}
6264

63-
/* Initialize the object from a HashTable and return whether it was successful. */
64-
static bool php_phongo_binary_init_from_hash(php_phongo_binary_t *intern, HashTable *props)
65+
/* Initialize the object from a HashTable and return whether it was successful.
66+
* An exception will be thrown on error. */
67+
static bool php_phongo_binary_init_from_hash(php_phongo_binary_t *intern, HashTable *props TSRMLS_DC)
6568
{
6669
#if PHP_VERSION_ID >= 70000
6770
zval *data, *type;
6871

6972
if ((data = zend_hash_str_find(props, "data", sizeof("data")-1)) && Z_TYPE_P(data) == IS_STRING &&
7073
(type = zend_hash_str_find(props, "type", sizeof("type")-1)) && Z_TYPE_P(type) == IS_LONG) {
71-
return php_phongo_binary_init(intern, Z_STRVAL_P(data), Z_STRLEN_P(data), Z_LVAL_P(type));
74+
return php_phongo_binary_init(intern, Z_STRVAL_P(data), Z_STRLEN_P(data), Z_LVAL_P(type) TSRMLS_CC);
7275
}
7376
#else
7477
zval **data, **type;
7578

7679
if (zend_hash_find(props, "data", sizeof("data"), (void**) &data) == SUCCESS && Z_TYPE_PP(data) == IS_STRING &&
7780
zend_hash_find(props, "type", sizeof("type"), (void**) &type) == SUCCESS && Z_TYPE_PP(type) == IS_LONG) {
78-
return php_phongo_binary_init(intern, Z_STRVAL_PP(data), Z_STRLEN_PP(data), Z_LVAL_PP(type));
81+
return php_phongo_binary_init(intern, Z_STRVAL_PP(data), Z_STRLEN_PP(data), Z_LVAL_PP(type) TSRMLS_CC);
7982
}
8083
#endif
84+
85+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s initialization requires \"data\" string and \"type\" integer fields", ZSTR_VAL(php_phongo_binary_ce->name));
8186
return false;
8287
}
8388

@@ -101,9 +106,7 @@ PHP_METHOD(Binary, __construct)
101106
}
102107
zend_restore_error_handling(&error_handling TSRMLS_CC);
103108

104-
if (!php_phongo_binary_init(intern, data, data_len, type)) {
105-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected type to be an unsigned 8-bit integer, %" PHONGO_LONG_FORMAT " given", type);
106-
}
109+
php_phongo_binary_init(intern, data, data_len, type TSRMLS_CC);
107110
}
108111
/* }}} */
109112

@@ -124,9 +127,7 @@ PHP_METHOD(Binary, __set_state)
124127
intern = Z_BINARY_OBJ_P(return_value);
125128
props = Z_ARRVAL_P(array);
126129

127-
if (!php_phongo_binary_init_from_hash(intern, props)) {
128-
php_error(E_ERROR, "Invalid serialization data for Binary object");
129-
}
130+
php_phongo_binary_init_from_hash(intern, props TSRMLS_CC);
130131
}
131132
/* }}} */
132133

@@ -144,9 +145,7 @@ PHP_METHOD(Binary, __wakeup)
144145
intern = Z_BINARY_OBJ_P(getThis());
145146
props = zend_std_get_properties(getThis() TSRMLS_CC);
146147

147-
if (!php_phongo_binary_init_from_hash(intern, props)) {
148-
php_error(E_ERROR, "Invalid serialization data for Binary object");
149-
}
148+
php_phongo_binary_init_from_hash(intern, props TSRMLS_CC);
150149
}
151150
/* }}} */
152151

src/BSON/Decimal128.c

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,39 @@ PHONGO_API zend_class_entry *php_phongo_decimal128_ce;
4646

4747
zend_object_handlers php_phongo_handler_decimal128;
4848

49-
/* Initialize the object from a string and return whether it was successful. */
50-
static bool php_phongo_decimal128_init(php_phongo_decimal128_t *intern, const char *value)
49+
/* Initialize the object and return whether it was successful. An exception will
50+
* be thrown on error. */
51+
static bool php_phongo_decimal128_init(php_phongo_decimal128_t *intern, const char *value TSRMLS_DC)
5152
{
52-
if (bson_decimal128_from_string(value, &intern->decimal)) {
53-
intern->initialized = true;
54-
55-
return true;
53+
if (!bson_decimal128_from_string(value, &intern->decimal)) {
54+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Error parsing decimal string: %s", value);
55+
return false;
5656
}
5757

58-
return false;
58+
intern->initialized = true;
59+
60+
return true;
5961
}
6062

61-
/* Initialize the object from a HashTable and return whether it was successful. */
62-
static bool php_phongo_decimal128_init_from_hash(php_phongo_decimal128_t *intern, HashTable *props)
63+
/* Initialize the object from a HashTable and return whether it was successful.
64+
* An exception will be thrown on error. */
65+
static bool php_phongo_decimal128_init_from_hash(php_phongo_decimal128_t *intern, HashTable *props TSRMLS_DC)
6366
{
6467
#if PHP_VERSION_ID >= 70000
6568
zval *dec;
6669

6770
if ((dec = zend_hash_str_find(props, "dec", sizeof("dec")-1)) && Z_TYPE_P(dec) == IS_STRING) {
68-
return php_phongo_decimal128_init(intern, Z_STRVAL_P(dec));
71+
return php_phongo_decimal128_init(intern, Z_STRVAL_P(dec) TSRMLS_CC);
6972
}
7073
#else
7174
zval **dec;
7275

7376
if (zend_hash_find(props, "dec", sizeof("dec"), (void**) &dec) == SUCCESS && Z_TYPE_PP(dec) == IS_STRING) {
74-
return php_phongo_decimal128_init(intern, Z_STRVAL_PP(dec));
77+
return php_phongo_decimal128_init(intern, Z_STRVAL_PP(dec) TSRMLS_CC);
7578
}
7679
#endif
80+
81+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s initialization requires \"dec\" string field", ZSTR_VAL(php_phongo_decimal128_ce->name));
7782
return false;
7883
}
7984

@@ -96,9 +101,7 @@ PHP_METHOD(Decimal128, __construct)
96101
}
97102
zend_restore_error_handling(&error_handling TSRMLS_CC);
98103

99-
if (!php_phongo_decimal128_init(intern, value)) {
100-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Error parsing decimal string: %s", value);
101-
}
104+
php_phongo_decimal128_init(intern, value TSRMLS_CC);
102105
}
103106
/* }}} */
104107

@@ -119,9 +122,7 @@ PHP_METHOD(Decimal128, __set_state)
119122
intern = Z_DECIMAL128_OBJ_P(return_value);
120123
props = Z_ARRVAL_P(array);
121124

122-
if (!php_phongo_decimal128_init_from_hash(intern, props)) {
123-
php_error(E_ERROR, "Invalid serialization data for Decimal128 object");
124-
}
125+
php_phongo_decimal128_init_from_hash(intern, props TSRMLS_CC);
125126
}
126127
/* }}} */
127128

@@ -158,9 +159,7 @@ PHP_METHOD(Decimal128, __wakeup)
158159
intern = Z_DECIMAL128_OBJ_P(getThis());
159160
props = zend_std_get_properties(getThis() TSRMLS_CC);
160161

161-
if (!php_phongo_decimal128_init_from_hash(intern, props)) {
162-
php_error(E_ERROR, "Invalid serialization data for Decimal128 object");
163-
}
162+
php_phongo_decimal128_init_from_hash(intern, props TSRMLS_CC);
164163
}
165164
/* }}} */
166165

src/BSON/Javascript.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,17 @@ PHONGO_API zend_class_entry *php_phongo_javascript_ce;
4646

4747
zend_object_handlers php_phongo_handler_javascript;
4848

49-
/* Initialize the object from a string and return whether it was successful. */
49+
/* Initialize the object and return whether it was successful. An exception will
50+
* be thrown on error. */
5051
static bool php_phongo_javascript_init(php_phongo_javascript_t *intern, const char *code, phongo_zpp_char_len code_len, zval *scope TSRMLS_DC)
5152
{
5253
if (scope && Z_TYPE_P(scope) != IS_OBJECT && Z_TYPE_P(scope) != IS_ARRAY && Z_TYPE_P(scope) != IS_NULL) {
54+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected scope to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(scope)));
55+
return false;
56+
}
57+
58+
if (strlen(code) != code_len) {
59+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Code cannot contain null bytes");
5360
return false;
5461
}
5562

@@ -66,7 +73,8 @@ static bool php_phongo_javascript_init(php_phongo_javascript_t *intern, const ch
6673
return true;
6774
}
6875

69-
/* Initialize the object from a HashTable and return whether it was successful. */
76+
/* Initialize the object from a HashTable and return whether it was successful.
77+
* An exception will be thrown on error. */
7078
static bool php_phongo_javascript_init_from_hash(php_phongo_javascript_t *intern, HashTable *props TSRMLS_DC)
7179
{
7280
#if PHP_VERSION_ID >= 70000
@@ -86,6 +94,8 @@ static bool php_phongo_javascript_init_from_hash(php_phongo_javascript_t *intern
8694
return php_phongo_javascript_init(intern, Z_STRVAL_PP(code), Z_STRLEN_PP(code), tmp TSRMLS_CC);
8795
}
8896
#endif
97+
98+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s initialization requires \"code\" string field", ZSTR_VAL(php_phongo_javascript_ce->name));
8999
return false;
90100
}
91101

@@ -131,9 +141,7 @@ PHP_METHOD(Javascript, __set_state)
131141
intern = Z_JAVASCRIPT_OBJ_P(return_value);
132142
props = Z_ARRVAL_P(array);
133143

134-
if (!php_phongo_javascript_init_from_hash(intern, props TSRMLS_CC)) {
135-
php_error(E_ERROR, "Invalid serialization data for Javascript object");
136-
}
144+
php_phongo_javascript_init_from_hash(intern, props TSRMLS_CC);
137145
}
138146
/* }}} */
139147

@@ -151,10 +159,7 @@ PHP_METHOD(Javascript, __wakeup)
151159
intern = Z_JAVASCRIPT_OBJ_P(getThis());
152160
props = zend_std_get_properties(getThis() TSRMLS_CC);
153161

154-
if (!php_phongo_javascript_init_from_hash(intern, props TSRMLS_CC)) {
155-
php_error(E_ERROR, "Invalid serialization data for Javascript object");
156-
} else {
157-
}
162+
php_phongo_javascript_init_from_hash(intern, props TSRMLS_CC);
158163
}
159164
/* }}} */
160165

0 commit comments

Comments
 (0)