Skip to content

Commit 60febd0

Browse files
alcaeusjmikola
andauthored
PHPC-1699: Ensure all argument parsing errors throw InvalidArgumentException (#1173)
Co-authored-by: Jeremy Mikola <jmikola@gmail.com>
1 parent c07159a commit 60febd0

Some content is hidden

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

56 files changed

+979
-215
lines changed

php_phongo.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3849,9 +3849,14 @@ PHP_FUNCTION(MongoDB_disabled___construct) /* {{{ */
38493849

38503850
PHP_FUNCTION(MongoDB_disabled___wakeup) /* {{{ */
38513851
{
3852+
zend_error_handling error_handling;
3853+
3854+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
38523855
if (zend_parse_parameters_none() == FAILURE) {
3856+
zend_restore_error_handling(&error_handling);
38533857
return;
38543858
}
3859+
zend_restore_error_handling(&error_handling);
38553860

38563861
phongo_throw_exception(PHONGO_ERROR_RUNTIME, "%s", "MongoDB\\Driver objects cannot be serialized");
38573862
} /* }}} */

src/BSON/Binary.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ static bool php_phongo_binary_init_from_hash(php_phongo_binary_t* intern, HashTa
7373
Construct a new BSON binary type */
7474
static PHP_METHOD(Binary, __construct)
7575
{
76-
php_phongo_binary_t* intern;
7776
zend_error_handling error_handling;
77+
php_phongo_binary_t* intern;
7878
char* data;
7979
size_t data_len;
8080
zend_long type;
8181

82-
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
8382
intern = Z_BINARY_OBJ_P(getThis());
8483

84+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
8585
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &data, &data_len, &type) == FAILURE) {
8686
zend_restore_error_handling(&error_handling);
8787
return;
@@ -95,13 +95,17 @@ static PHP_METHOD(Binary, __construct)
9595
*/
9696
static PHP_METHOD(Binary, __set_state)
9797
{
98+
zend_error_handling error_handling;
9899
php_phongo_binary_t* intern;
99100
HashTable* props;
100101
zval* array;
101102

103+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
102104
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
103-
RETURN_FALSE;
105+
zend_restore_error_handling(&error_handling);
106+
return;
104107
}
108+
zend_restore_error_handling(&error_handling);
105109

106110
object_init_ex(return_value, php_phongo_binary_ce);
107111

@@ -115,11 +119,15 @@ static PHP_METHOD(Binary, __set_state)
115119
Return the Binary's data string. */
116120
static PHP_METHOD(Binary, __toString)
117121
{
122+
zend_error_handling error_handling;
118123
php_phongo_binary_t* intern;
119124

125+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
120126
if (zend_parse_parameters_none() == FAILURE) {
127+
zend_restore_error_handling(&error_handling);
121128
return;
122129
}
130+
zend_restore_error_handling(&error_handling);
123131

124132
intern = Z_BINARY_OBJ_P(getThis());
125133

@@ -130,13 +138,17 @@ static PHP_METHOD(Binary, __toString)
130138
*/
131139
static PHP_METHOD(Binary, getData)
132140
{
141+
zend_error_handling error_handling;
133142
php_phongo_binary_t* intern;
134143

135144
intern = Z_BINARY_OBJ_P(getThis());
136145

146+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
137147
if (zend_parse_parameters_none() == FAILURE) {
148+
zend_restore_error_handling(&error_handling);
138149
return;
139150
}
151+
zend_restore_error_handling(&error_handling);
140152

141153
RETURN_STRINGL(intern->data, intern->data_len);
142154
} /* }}} */
@@ -145,13 +157,17 @@ static PHP_METHOD(Binary, getData)
145157
*/
146158
static PHP_METHOD(Binary, getType)
147159
{
160+
zend_error_handling error_handling;
148161
php_phongo_binary_t* intern;
149162

150163
intern = Z_BINARY_OBJ_P(getThis());
151164

165+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
152166
if (zend_parse_parameters_none() == FAILURE) {
167+
zend_restore_error_handling(&error_handling);
153168
return;
154169
}
170+
zend_restore_error_handling(&error_handling);
155171

156172
RETURN_LONG(intern->type);
157173
} /* }}} */
@@ -160,13 +176,17 @@ static PHP_METHOD(Binary, getType)
160176
*/
161177
static PHP_METHOD(Binary, jsonSerialize)
162178
{
179+
zend_error_handling error_handling;
163180
php_phongo_binary_t* intern;
164181
char type[3];
165182
int type_len;
166183

184+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
167185
if (zend_parse_parameters_none() == FAILURE) {
186+
zend_restore_error_handling(&error_handling);
168187
return;
169188
}
189+
zend_restore_error_handling(&error_handling);
170190

171191
intern = Z_BINARY_OBJ_P(getThis());
172192

@@ -186,16 +206,20 @@ static PHP_METHOD(Binary, jsonSerialize)
186206
*/
187207
static PHP_METHOD(Binary, serialize)
188208
{
209+
zend_error_handling error_handling;
189210
php_phongo_binary_t* intern;
190211
zval retval;
191212
php_serialize_data_t var_hash;
192213
smart_str buf = { 0 };
193214

194215
intern = Z_BINARY_OBJ_P(getThis());
195216

217+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
196218
if (zend_parse_parameters_none() == FAILURE) {
219+
zend_restore_error_handling(&error_handling);
197220
return;
198221
}
222+
zend_restore_error_handling(&error_handling);
199223

200224
array_init_size(&retval, 2);
201225
ADD_ASSOC_STRINGL(&retval, "data", intern->data, intern->data_len);
@@ -216,8 +240,8 @@ static PHP_METHOD(Binary, serialize)
216240
*/
217241
static PHP_METHOD(Binary, unserialize)
218242
{
219-
php_phongo_binary_t* intern;
220243
zend_error_handling error_handling;
244+
php_phongo_binary_t* intern;
221245
char* serialized;
222246
size_t serialized_len;
223247
zval props;
@@ -226,7 +250,6 @@ static PHP_METHOD(Binary, unserialize)
226250
intern = Z_BINARY_OBJ_P(getThis());
227251

228252
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
229-
230253
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized, &serialized_len) == FAILURE) {
231254
zend_restore_error_handling(&error_handling);
232255
return;

src/BSON/DBPointer.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,17 @@ static bool php_phongo_dbpointer_init_from_hash(php_phongo_dbpointer_t* intern,
7171
Return the DBPointer's namespace string and ObjectId. */
7272
static PHP_METHOD(DBPointer, __toString)
7373
{
74+
zend_error_handling error_handling;
7475
php_phongo_dbpointer_t* intern;
7576
char* retval;
7677
int retval_len;
7778

79+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
7880
if (zend_parse_parameters_none() == FAILURE) {
81+
zend_restore_error_handling(&error_handling);
7982
return;
8083
}
84+
zend_restore_error_handling(&error_handling);
8185

8286
intern = Z_DBPOINTER_OBJ_P(getThis());
8387

@@ -90,13 +94,17 @@ static PHP_METHOD(DBPointer, __toString)
9094
*/
9195
static PHP_METHOD(DBPointer, jsonSerialize)
9296
{
97+
zend_error_handling error_handling;
9398
php_phongo_dbpointer_t* intern;
9499
zval zdb_pointer;
95100
zval zoid;
96101

102+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
97103
if (zend_parse_parameters_none() == FAILURE) {
104+
zend_restore_error_handling(&error_handling);
98105
return;
99106
}
107+
zend_restore_error_handling(&error_handling);
100108

101109
intern = Z_DBPOINTER_OBJ_P(getThis());
102110

@@ -114,16 +122,20 @@ static PHP_METHOD(DBPointer, jsonSerialize)
114122
*/
115123
static PHP_METHOD(DBPointer, serialize)
116124
{
125+
zend_error_handling error_handling;
117126
php_phongo_dbpointer_t* intern;
118127
zval retval;
119128
php_serialize_data_t var_hash;
120129
smart_str buf = { 0 };
121130

122131
intern = Z_DBPOINTER_OBJ_P(getThis());
123132

133+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
124134
if (zend_parse_parameters_none() == FAILURE) {
135+
zend_restore_error_handling(&error_handling);
125136
return;
126137
}
138+
zend_restore_error_handling(&error_handling);
127139

128140
array_init_size(&retval, 2);
129141
ADD_ASSOC_STRINGL(&retval, "ref", intern->ref, intern->ref_len);
@@ -144,8 +156,8 @@ static PHP_METHOD(DBPointer, serialize)
144156
*/
145157
static PHP_METHOD(DBPointer, unserialize)
146158
{
147-
php_phongo_dbpointer_t* intern;
148159
zend_error_handling error_handling;
160+
php_phongo_dbpointer_t* intern;
149161
char* serialized;
150162
size_t serialized_len;
151163
zval props;
@@ -154,7 +166,6 @@ static PHP_METHOD(DBPointer, unserialize)
154166
intern = Z_DBPOINTER_OBJ_P(getThis());
155167

156168
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
157-
158169
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized, &serialized_len) == FAILURE) {
159170
zend_restore_error_handling(&error_handling);
160171
return;

src/BSON/Decimal128.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ static bool php_phongo_decimal128_init_from_hash(php_phongo_decimal128_t* intern
6060
Construct a new BSON Decimal128 type */
6161
static PHP_METHOD(Decimal128, __construct)
6262
{
63-
php_phongo_decimal128_t* intern;
6463
zend_error_handling error_handling;
64+
php_phongo_decimal128_t* intern;
6565
char* value;
6666
size_t value_len;
6767

68-
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
6968
intern = Z_DECIMAL128_OBJ_P(getThis());
7069

70+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
7171
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &value, &value_len) == FAILURE) {
7272
zend_restore_error_handling(&error_handling);
7373
return;
@@ -81,13 +81,17 @@ static PHP_METHOD(Decimal128, __construct)
8181
*/
8282
static PHP_METHOD(Decimal128, __set_state)
8383
{
84+
zend_error_handling error_handling;
8485
php_phongo_decimal128_t* intern;
8586
HashTable* props;
8687
zval* array;
8788

89+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
8890
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
89-
RETURN_FALSE;
91+
zend_restore_error_handling(&error_handling);
92+
return;
9093
}
94+
zend_restore_error_handling(&error_handling);
9195

9296
object_init_ex(return_value, php_phongo_decimal128_ce);
9397

@@ -101,14 +105,18 @@ static PHP_METHOD(Decimal128, __set_state)
101105
*/
102106
static PHP_METHOD(Decimal128, __toString)
103107
{
108+
zend_error_handling error_handling;
104109
php_phongo_decimal128_t* intern;
105110
char outbuf[BSON_DECIMAL128_STRING];
106111

107112
intern = Z_DECIMAL128_OBJ_P(getThis());
108113

114+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
109115
if (zend_parse_parameters_none() == FAILURE) {
116+
zend_restore_error_handling(&error_handling);
110117
return;
111118
}
119+
zend_restore_error_handling(&error_handling);
112120

113121
bson_decimal128_to_string(&intern->decimal, outbuf);
114122

@@ -119,12 +127,16 @@ static PHP_METHOD(Decimal128, __toString)
119127
*/
120128
static PHP_METHOD(Decimal128, jsonSerialize)
121129
{
130+
zend_error_handling error_handling;
122131
php_phongo_decimal128_t* intern;
123132
char outbuf[BSON_DECIMAL128_STRING] = "";
124133

134+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
125135
if (zend_parse_parameters_none() == FAILURE) {
136+
zend_restore_error_handling(&error_handling);
126137
return;
127138
}
139+
zend_restore_error_handling(&error_handling);
128140

129141
intern = Z_DECIMAL128_OBJ_P(getThis());
130142

@@ -137,6 +149,7 @@ static PHP_METHOD(Decimal128, jsonSerialize)
137149
*/
138150
static PHP_METHOD(Decimal128, serialize)
139151
{
152+
zend_error_handling error_handling;
140153
php_phongo_decimal128_t* intern;
141154
zval retval;
142155
php_serialize_data_t var_hash;
@@ -145,9 +158,12 @@ static PHP_METHOD(Decimal128, serialize)
145158

146159
intern = Z_DECIMAL128_OBJ_P(getThis());
147160

161+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
148162
if (zend_parse_parameters_none() == FAILURE) {
163+
zend_restore_error_handling(&error_handling);
149164
return;
150165
}
166+
zend_restore_error_handling(&error_handling);
151167

152168
bson_decimal128_to_string(&intern->decimal, outbuf);
153169
array_init_size(&retval, 1);
@@ -168,8 +184,8 @@ static PHP_METHOD(Decimal128, serialize)
168184
*/
169185
static PHP_METHOD(Decimal128, unserialize)
170186
{
171-
php_phongo_decimal128_t* intern;
172187
zend_error_handling error_handling;
188+
php_phongo_decimal128_t* intern;
173189
char* serialized;
174190
size_t serialized_len;
175191
zval props;
@@ -178,7 +194,6 @@ static PHP_METHOD(Decimal128, unserialize)
178194
intern = Z_DECIMAL128_OBJ_P(getThis());
179195

180196
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
181-
182197
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized, &serialized_len) == FAILURE) {
183198
zend_restore_error_handling(&error_handling);
184199
return;

0 commit comments

Comments
 (0)