Skip to content

PHPC-677: Keep pem_file valid for life of mongoc_client_t #307

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
Apr 26, 2016
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
20 changes: 11 additions & 9 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,7 @@ static bool php_phongo_apply_wc_options_to_client(mongoc_client_t *client, bson_
return true;
} /* }}} */

static mongoc_client_t *php_phongo_make_mongo_client(const mongoc_uri_t *uri, zval *driverOptions TSRMLS_DC) /* {{{ */
static mongoc_client_t *php_phongo_make_mongo_client(php_phongo_manager_t *manager, const mongoc_uri_t *uri, zval *driverOptions TSRMLS_DC) /* {{{ */
{
#if PHP_VERSION_ID >= 70000
zval *tmp;
Expand Down Expand Up @@ -1948,21 +1948,23 @@ static mongoc_client_t *php_phongo_make_mongo_client(const mongoc_uri_t *uri, zv

#if PHP_VERSION_ID >= 70000
if ((pem = php_stream_context_get_option(ctx, "ssl", "local_cert")) != NULL) {
zend_string *s = zval_get_string(pem);
#else
if (SUCCESS == php_stream_context_get_option(ctx, "ssl", "local_cert", &pem)) {
convert_to_string_ex(pem);
#endif
char filename[MAXPATHLEN];
/* mongoc_client_set_ssl_opts() copies mongoc_ssl_opt_t shallowly;
* its strings must be kept valid for the life of mongoc_client_t */
manager->pem_file = ecalloc(1, MAXPATHLEN);
Copy link
Contributor

@derickr derickr Apr 26, 2016

Choose a reason for hiding this comment

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

You seem to allocate it here, but you don't set it to anything?

Copy link
Member Author

Choose a reason for hiding this comment

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

manager->pem_file is set via the VCWD_REALPATH() macro below.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK


#if PHP_VERSION_ID >= 70000
zend_string *s = zval_get_string(pem);
if (VCWD_REALPATH(ZSTR_VAL(s), filename)) {
if (VCWD_REALPATH(ZSTR_VAL(s), manager->pem_file)) {
#else
convert_to_string_ex(pem);
if (VCWD_REALPATH(Z_STRVAL_PP(pem), filename)) {
if (VCWD_REALPATH(Z_STRVAL_PP(pem), manager->pem_file)) {
#endif
mongoc_ssl_opt_t ssl_options;
mongoc_ssl_opt_t ssl_options = {0};

ssl_options.pem_file = filename;
ssl_options.pem_file = manager->pem_file;
mongoc_client_set_ssl_opts(client, &ssl_options);
}
#if PHP_VERSION_ID >= 70000
Expand All @@ -1985,7 +1987,7 @@ bool phongo_manager_init(php_phongo_manager_t *manager, const char *uri_string,
return false;
}

manager->client = php_phongo_make_mongo_client(uri, driverOptions TSRMLS_CC);
manager->client = php_phongo_make_mongo_client(manager, uri, driverOptions TSRMLS_CC);
mongoc_uri_destroy(uri);

if (!manager->client) {
Expand Down
1 change: 1 addition & 0 deletions php_phongo_structs-5.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ typedef struct {
typedef struct {
zend_object std;
mongoc_client_t *client;
char *pem_file;
} php_phongo_manager_t;

typedef struct {
Expand Down
1 change: 1 addition & 0 deletions php_phongo_structs-7.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ typedef struct {

typedef struct {
mongoc_client_t *client;
char *pem_file;
zend_object std;
} php_phongo_manager_t;

Expand Down
4 changes: 4 additions & 0 deletions src/MongoDB/Manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ static void php_phongo_manager_free_object(phongo_free_object_arg *object TSRMLS
mongoc_client_destroy(intern->client);
}

if (intern->pem_file) {
efree(intern->pem_file);
}

#if PHP_VERSION_ID < 70000
efree(intern);
#endif
Expand Down