Skip to content

BSON serialization spec compliance #64

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 10 commits into from
Jul 29, 2015
Merged

Conversation

int classname_free = 0;

if (Z_TYPE_P(obj_data) == IS_OBJECT) {
classname_free = !zend_get_object_classname(obj_data, &classname, &classname_len TSRMLS_CC);
Copy link
Member Author

Choose a reason for hiding this comment

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

@bjori: I noticed you were Z_OBJCE_P(object)->name directly when creating the __pclass value. I found zend_get_object_classname() used in core in several places where a class name needed to be reported, so I assume it's fine to use for this exception message.

I can't imagine why folks would want to report a different class name, but I expect the ODM behavior is fine sticking to the name value from the struct.

Copy link
Contributor

Choose a reason for hiding this comment

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

zend_get_object_classname has been removed in PHP-7.
Please continue to use 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.

👍

@jmikola jmikola force-pushed the phpc-329 branch 3 times, most recently from 0cde2cf to 2fcae11 Compare July 28, 2015 00:44
@jmikola
Copy link
Member Author

jmikola commented Jul 28, 2015

@derickr: Feel free to take a look at this and my test changes. I was able to get the 5.3 build working (although I still seem some local failures about missing OpenSSL types), so I'll plan to merge this in tomorrow.


if test "$PHP_MONGODB_FOUND_VERNUM" -ge "50400"; then
PHP_CHECK_GCC_ARG(-Wno-cast-qual, _MAINTAINER_CFLAGS="$_MAINTAINER_CFLAGS -Wno-cast-qual")
fi
Copy link
Contributor

Choose a reason for hiding this comment

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

You shouldn't do this. Instead, fix the consts.

Copy link
Member Author

Choose a reason for hiding this comment

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

Just for 5.3?

5.4+ rightfully lists the args as const char *, so there's no need there.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm fine with this.
Just for reference, which exact case prompted this change?
Isn't it also valid for the arginfos?

Copy link
Member Author

Choose a reason for hiding this comment

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

arginfos appear to be declared as static const in PHP 5.3. I only saw these warnings with zend_parse_parameters() and possibly one other macro (not arginfo).

Copy link
Member Author

Choose a reason for hiding this comment

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

For reference, here are a few of these errors from 5.3:

/home/jmikola/workspace/mongodb/phpc/src/bson.c: In function ‘zif_fromPHP’:
/home/jmikola/workspace/mongodb/phpc/src/bson.c:967:55: warning: passing argument 3 of ‘zend_parse_parameters’ discards ‘const’ qualifier from pointer target type
  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "A", &data) == FAILURE) {
                                                       ^
In file included from /home/jmikola/bin/php-bin/php-5.3/include/php/main/php.h:38:0,
                 from /home/jmikola/workspace/mongodb/phpc/src/bson.c:29:
/home/jmikola/bin/php-bin/php-5.3/include/php/Zend/zend_API.h:245:14: note: expected ‘char *’ but argument is of type ‘const char *’
 ZEND_API int zend_parse_parameters(int num_args TSRMLS_DC, char *type_spec, ...);
              ^
/home/jmikola/workspace/mongodb/phpc/src/bson.c:974:1: warning: passing argument 3 of ‘_estrndup’ discards ‘const’ qualifier from pointer target type
  RETVAL_STRINGL((const char *) bson_get_data(bson), bson->len, 1);
 ^

A full log of the stderr output from a 5.3 build is here: https://gist.github.com/jmikola/e3a3f7da387333187864

Copy link
Contributor

Choose a reason for hiding this comment

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

We don't care any more, as we can drop 5.3 :-D

classname_free = !zend_get_object_classname(obj_data, &classname, &classname_len TSRMLS_CC);
}

phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Expected %s() to return an array or stdClass, %s given", BSON_SERIALIZE_FUNC_NAME, (classname_len ? classname : zend_get_type_by_const(Z_TYPE_P(obj_data))));
Copy link
Contributor

Choose a reason for hiding this comment

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

You should probably include the classname too.
There are plenty matches for the function name so it doesn't help mutch having that in the error message without the classname

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

jmikola added 8 commits July 28, 2015 14:50
Per the serialization spec, we should check that the class (1) exists, (2), is instantiatable, and (3) implements Unserializable.

Additionally, this refactors some code duplication out of apply_classname_to_state(). We should also always check to free classname, since an empty string might have been allocated (i.e. classname_len is zero).
ODM behavior should override only the default type map and named class. Array and stdClass object modes still take precedence.

The PHONGO_TYPEMAP_CLASS switch cases were simplified, since we can trust that the root/document or ODM class implements Unserializable (thanks to php_phongo_bson_typemap_to_state() and php_phongo_bson_visit_binary(), respectively).
This revises the UnexpectedValueException message for an invalid return value (PHPC-331).

Additionally, we consolidated the previous BSON-encoding error tests.
@@ -31,6 +31,10 @@
#include <Zend/zend_hash.h>
#include <Zend/zend_interfaces.h>

#if PHP_VERSION_ID >= 50400
# include <Zend/zend_string.h>
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel this should really be in the compat header as a #else condition for the str_efree() ?
Or is that pushing it a bit far?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that'd be neater too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will do.

@bjori
Copy link
Contributor

bjori commented Jul 28, 2015

I'm not sure why so many unrelated things are in the same PR!

I haven't read the latest and greatest "BSON serialization spec" so I don't know if this does indeed confirm to it, or makes actual sense.

The code itself looks good to me though.

@jmikola
Copy link
Member Author

jmikola commented Jul 29, 2015

I'm not sure why so many unrelated things are in the same PR!

I created half of the tickets after starting PHPC-329 and most of the code was inter-dependent.

@jmikola jmikola merged commit 888b5dc into mongodb:master Jul 29, 2015
jmikola added a commit that referenced this pull request Jul 29, 2015
@jmikola jmikola deleted the phpc-329 branch July 29, 2015 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants