Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed PHP-384: Segfault with GridFS and mongo.long_as_object=1.

  • Loading branch information...
commit fcee810ee4b80c49a316f30e887a564d8eb6fbf9 1 parent c642074
@derickr authored
Showing with 44 additions and 7 deletions.
  1. +11 −7 gridfs.c
  2. +33 −0 tests/bug00384.phpt
View
18 gridfs.c
@@ -67,7 +67,9 @@ extern zend_class_entry *mongo_ce_DB,
*mongo_ce_GridFSException,
*mongo_ce_Id,
*mongo_ce_Date,
- *mongo_ce_BinData;
+ *mongo_ce_BinData,
+ *mongo_ce_Int32,
+ *mongo_ce_Int64;
ZEND_EXTERN_MODULE_GLOBALS(mongo);
@@ -1143,12 +1145,14 @@ PHP_METHOD(MongoGridFSFile, getBytes) {
zval_ptr_dtor(&query);
zval_ptr_dtor(&sort);
- if (Z_TYPE_PP(size) == IS_DOUBLE) {
- len = (int)Z_DVAL_PP(size);
- }
- else { // if Z_TYPE_PP(size) == IS_LONG
- len = Z_LVAL_PP(size);
- }
+ if (Z_TYPE_PP(size) == IS_DOUBLE) {
+ len = (int)Z_DVAL_PP(size);
+ } else if (Z_TYPE_PP(size) == IS_LONG) {
+ len = Z_LVAL_PP(size);
+ } else if (Z_TYPE_PP(size) == IS_OBJECT && (Z_OBJCE_PP(size) == mongo_ce_Int32 || Z_OBJCE_PP(size) == mongo_ce_Int64)) {
+ zval *sizet = zend_read_property(mongo_ce_Int64, *size, "value", strlen("value"), NOISY TSRMLS_CC);
+ len = atoi(Z_STRVAL_P(sizet));
+ }
str = (char*)emalloc(len + 1);
str_ptr = str;
View
33 tests/bug00384.phpt
@@ -0,0 +1,33 @@
+--TEST--
+Test for PHP-384: Segfaults with GridFS and long_as_object.
+--INI--
+mongo.long_as_object=0
+--FILE--
+<?php
+$m = new Mongo();
+$m->phpunit->dropCollection( 'fs.files' );
+$m->phpunit->dropCollection( 'fs.chunks' );
+
+$g = $m->phpunit->getGridFS();
+$id = $g->storeBytes( str_repeat("\0", 4096 ) );
+
+
+ini_set( 'mongo.long_as_object', 0 );
+$cursor = $g->get( $id );
+$a = $cursor->getBytes();
+var_dump( $cursor->getSize() );
+
+ini_set( 'mongo.long_as_object', 1 );
+$cursor = $g->get( $id );
+$a = $cursor->getBytes();
+var_dump( $cursor->getSize() );
+
+echo 'OK'. PHP_EOL;
+?>
+--EXPECTF--
+int(4096)
+object(MongoInt64)#12 (1) {
+ ["value"]=>
+ string(4) "4096"
+}
+OK
Please sign in to comment.
Something went wrong with that request. Please try again.