Skip to content

PHPC-1070 and PHPC-1071: Report class name for unexpected object values #705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ static bool process_read_concern(zval *option, bson_t *mongoc_opts TSRMLS_DC)
phongo_throw_exception(
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC,
"Expected 'readConcern' option to be 'MongoDB\\Driver\\ReadConcern', %s given",
zend_get_type_by_const(Z_TYPE_P(option))
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(option)
);
return false;
}
Expand Down Expand Up @@ -520,7 +520,7 @@ static bool process_read_preference(zval *option, bson_t *mongoc_opts, zval **zr
phongo_throw_exception(
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC,
"Expected 'readPreference' option to be 'MongoDB\\Driver\\ReadPreference', %s given",
zend_get_type_by_const(Z_TYPE_P(option))
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(option)
);
return false;
}
Expand All @@ -543,7 +543,7 @@ static bool process_write_concern(zval *option, bson_t *mongoc_opts, zval **zwri
phongo_throw_exception(
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC,
"Expected 'writeConcern' option to be 'MongoDB\\Driver\\WriteConcern', %s given",
zend_get_type_by_const(Z_TYPE_P(option))
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(option)
);
return false;
}
Expand Down
8 changes: 6 additions & 2 deletions php_phongo.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include "bson.h"
#include "mongoc.h"

#include "phongo_compat.h"
#include "php_phongo_classes.h"

#define phpext_mongodb_ptr &mongodb_module_entry
extern zend_module_entry mongodb_module_entry;

Expand Down Expand Up @@ -61,8 +64,6 @@ ZEND_TSRMLS_CACHE_EXTERN()

#define PHONGO_WRITE_CONCERN_W_MAJORITY "majority"

#include "php_phongo_classes.h"

/* This enum is necessary since mongoc_server_description_type_t is private and
* we need to translate strings returned by mongoc_server_description_type() to
* Server integer constants. */
Expand Down Expand Up @@ -194,6 +195,9 @@ zend_bool phongo_writeconcernerror_init(zval *return_value, bson_t *bson TSRMLS_
} \
} while(0);

#define PHONGO_ZVAL_CLASS_OR_TYPE_NAME(zv) (Z_TYPE(zv) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE(zv)->name) : zend_get_type_by_const(Z_TYPE(zv)))
#define PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(zvp) PHONGO_ZVAL_CLASS_OR_TYPE_NAME(*(zvp))

#endif /* PHONGO_H */


Expand Down
4 changes: 2 additions & 2 deletions src/BSON/Timestamp.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ static PHP_METHOD(Timestamp, __construct)
}

if (Z_TYPE_P(increment) != IS_STRING) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected increment to be an unsigned 32-bit integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(increment)));
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected increment to be an unsigned 32-bit integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(increment));
return;
}

Expand All @@ -156,7 +156,7 @@ static PHP_METHOD(Timestamp, __construct)
}

if (Z_TYPE_P(timestamp) != IS_STRING) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected timestamp to be an unsigned 32-bit integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(timestamp)));
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected timestamp to be an unsigned 32-bit integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(timestamp));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/BSON/UTCDateTime.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ static PHP_METHOD(UTCDateTime, __construct)
}

if (Z_TYPE_P(milliseconds) != IS_STRING) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(milliseconds)));
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds));
return;
}

Expand Down
8 changes: 4 additions & 4 deletions src/MongoDB/Query.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static bool php_phongo_query_opts_append_string(bson_t *opts, const char *opts_k
zval *value = php_array_fetch(zarr, zarr_key);

if (Z_TYPE_P(value) != IS_STRING) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"%s\" %s to be string, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", zend_get_type_by_const(Z_TYPE_P(value)));
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"%s\" %s to be string, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(value));
return false;
}

Expand All @@ -55,7 +55,7 @@ static bool php_phongo_query_opts_append_document(bson_t *opts, const char *opts
bson_t b = BSON_INITIALIZER;

if (Z_TYPE_P(value) != IS_OBJECT && Z_TYPE_P(value) != IS_ARRAY) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"%s\" %s to be array or object, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", zend_get_type_by_const(Z_TYPE_P(value)));
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"%s\" %s to be array or object, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(value));
return false;
}

Expand Down Expand Up @@ -195,7 +195,7 @@ static bool php_phongo_query_init_readconcern(php_phongo_query_t *intern, zval *
zval *read_concern = php_array_fetchc(options, "readConcern");

if (Z_TYPE_P(read_concern) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(read_concern), php_phongo_readconcern_ce TSRMLS_CC)) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"readConcern\" option to be %s, %s given", ZSTR_VAL(php_phongo_readconcern_ce->name), zend_get_type_by_const(Z_TYPE_P(read_concern)));
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"readConcern\" option to be %s, %s given", ZSTR_VAL(php_phongo_readconcern_ce->name), PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(read_concern));
return false;
}

Expand Down Expand Up @@ -262,7 +262,7 @@ static bool php_phongo_query_init(php_phongo_query_t *intern, zval *filter, zval
modifiers = php_array_fetchc(options, "modifiers");

if (Z_TYPE_P(modifiers) != IS_ARRAY) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"modifiers\" option to be array, %s given", zend_get_type_by_const(Z_TYPE_P(modifiers)));
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"modifiers\" option to be array, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(modifiers));
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB/ReadPreference.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static PHP_METHOD(ReadPreference, __construct)
return;
}
} else {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected mode to be integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(mode)));
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected mode to be integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(mode));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB/WriteConcern.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static PHP_METHOD(WriteConcern, __construct)
mongoc_write_concern_set_wtag(intern->write_concern, Z_STRVAL_P(w));
}
} else {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected w to be integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(w)));
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected w to be integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(w));
return;
}

Expand Down
29 changes: 7 additions & 22 deletions src/bson-encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,18 @@ static void php_phongo_bson_append_object(bson_t *bson, php_phongo_bson_flags_t
if (Z_TYPE(obj_data) != IS_ARRAY && !(Z_TYPE(obj_data) == IS_OBJECT && instanceof_function(Z_OBJCE(obj_data), zend_standard_class_def TSRMLS_CC))) {
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC,
"Expected %s::%s() to return an array or stdClass, %s given",
Z_OBJCE_P(object)->name->val,
ZSTR_VAL(Z_OBJCE_P(object)->name),
BSON_SERIALIZE_FUNC_NAME,
(Z_TYPE(obj_data) == IS_OBJECT
? Z_OBJCE(obj_data)->name->val
: zend_get_type_by_const(Z_TYPE(obj_data))
)
PHONGO_ZVAL_CLASS_OR_TYPE_NAME(obj_data)
);
zval_ptr_dtor(&obj_data);
#else
if (Z_TYPE_P(obj_data) != IS_ARRAY && !(Z_TYPE_P(obj_data) == IS_OBJECT && instanceof_function(Z_OBJCE_P(obj_data), zend_standard_class_def TSRMLS_CC))) {
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC,
"Expected %s::%s() to return an array or stdClass, %s given",
Z_OBJCE_P(object)->name,
ZSTR_VAL(Z_OBJCE_P(object)->name),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, this isn't necessary since we're in PHP 5 mode here, but it reduces some variation between the PHP 5 and 7 blocks. The only real difference in these blocks is that obj_data is a zval * for PHP 5.

BSON_SERIALIZE_FUNC_NAME,
(Z_TYPE_P(obj_data) == IS_OBJECT
? Z_OBJCE_P(obj_data)->name
: zend_get_type_by_const(Z_TYPE_P(obj_data))
)
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(obj_data)
);
zval_ptr_dtor(&obj_data);
#endif
Expand Down Expand Up @@ -453,22 +447,13 @@ void php_phongo_zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
#endif
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC,
"Expected %s::%s() to return an array or stdClass, %s given",
#if PHP_VERSION_ID >= 70000
Z_OBJCE_P(data)->name->val,
#else
Z_OBJCE_P(data)->name,
#endif
ZSTR_VAL(Z_OBJCE_P(data)->name),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have eliminated the #if block much earlier, but I just noticed this.

BSON_SERIALIZE_FUNC_NAME,
#if PHP_VERSION_ID >= 70000
(Z_TYPE(obj_data) == IS_OBJECT
? Z_OBJCE(obj_data)->name->val
: zend_get_type_by_const(Z_TYPE(obj_data))
PHONGO_ZVAL_CLASS_OR_TYPE_NAME(obj_data)
#else
(Z_TYPE_P(obj_data) == IS_OBJECT
? Z_OBJCE_P(obj_data)->name
: zend_get_type_by_const(Z_TYPE_P(obj_data))
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(obj_data)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only difference here is that obj_data is a zval * for PHP 5.

#endif
)
);

goto cleanup;
Expand Down
4 changes: 2 additions & 2 deletions tests/bson/bson-timestamp_error-006.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Expected increment to be an unsigned 32-bit integer or string, boolean given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected increment to be an unsigned 32-bit integer or string, array given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected increment to be an unsigned 32-bit integer or string, object given
Expected increment to be an unsigned 32-bit integer or string, stdClass given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected timestamp to be an unsigned 32-bit integer or string, %r(null|NULL)%r given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expand All @@ -42,5 +42,5 @@ Expected timestamp to be an unsigned 32-bit integer or string, boolean given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected timestamp to be an unsigned 32-bit integer or string, array given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected timestamp to be an unsigned 32-bit integer or string, object given
Expected timestamp to be an unsigned 32-bit integer or string, stdClass given
===DONE===
2 changes: 1 addition & 1 deletion tests/manager/manager-executeCommand_error-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ echo throws(function() use ($manager, $command) {
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', string given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object given
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', stdClass given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Unknown option 'unknown'
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expand Down
2 changes: 1 addition & 1 deletion tests/manager/manager-executeQuery_error-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ echo throws(function() use ($manager, $query) {
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', string given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object given
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', stdClass given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Unknown option 'unknown'
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expand Down
2 changes: 1 addition & 1 deletion tests/query/query-ctor_error-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Expected "readConcern" option to be MongoDB\Driver\ReadConcern, boolean given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected "readConcern" option to be MongoDB\Driver\ReadConcern, array given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected "readConcern" option to be MongoDB\Driver\ReadConcern, object given
Expected "readConcern" option to be MongoDB\Driver\ReadConcern, stdClass given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected "readConcern" option to be MongoDB\Driver\ReadConcern, %r(null|NULL)%r given
===DONE===
2 changes: 1 addition & 1 deletion tests/server/server-executeCommand_error-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ echo throws(function() use ($manager, $command) {
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', string given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object given
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', stdClass given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Unknown option 'unknown'
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expand Down
2 changes: 1 addition & 1 deletion tests/server/server-executeQuery_error-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ echo throws(function() use ($manager, $query) {
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', string given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object given
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', stdClass given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Unknown option 'unknown'
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expand Down
2 changes: 1 addition & 1 deletion tests/writeConcern/writeconcern-ctor_error-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Expected w to be integer or string, boolean given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected w to be integer or string, array given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected w to be integer or string, object given
Expected w to be integer or string, stdClass given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected w to be integer or string, %r(null|NULL)%r given
===DONE===