Fixed fetching of a property defined but not initialized in C extension, fixed #1337
authorTugdual Saunier <tugdual.saunier@gmail.com>
Thu, 13 Feb 2014 09:35:20 +0000 (09:35 +0000)
committerTugdual Saunier <tugdual.saunier@gmail.com>
Thu, 13 Feb 2014 09:48:39 +0000 (09:48 +0000)
CHANGELOG
ext/twig/twig.c
test/Twig/Tests/TemplateTest.php

index cbc8805..7a4c3a3 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -2,6 +2,7 @@
 
  * fixed the conversion of the special '0000-00-00 00:00' date
  * added an error message when trying to import an undefined block from a trait
+ * fixed a C extension crash when accessing defined but uninitialized property.
 
 * 1.15.0 (2013-12-06)
 
index a36f192..5cb923f 100644 (file)
@@ -324,10 +324,8 @@ zval *TWIG_PROPERTY(zval *object, zval *propname TSRMLS_DC)
 #else
                tmp = Z_OBJ_HT_P(object)->read_property(object, propname, BP_VAR_IS TSRMLS_CC);
 #endif
-               if (tmp != EG(uninitialized_zval_ptr)) {
-                       return tmp;
-               } else {
-                       return NULL;
+               if (tmp == EG(uninitialized_zval_ptr)) {
+                       ZVAL_NULL(tmp);
                }
        }
        return tmp;
index 34b7a63..aa8dafd 100644 (file)
@@ -268,6 +268,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
         $propertyObject      = new Twig_TemplatePropertyObject();
         $propertyObject1     = new Twig_TemplatePropertyObjectAndIterator();
         $propertyObject2     = new Twig_TemplatePropertyObjectAndArrayAccess();
+        $propertyObject3     = new Twig_TemplatePropertyObjectDefinedWithUndefinedValue();
         $methodObject        = new Twig_TemplateMethodObject();
         $magicMethodObject   = new Twig_TemplateMagicMethodObject();
 
@@ -317,6 +318,11 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
             }
         }
 
+        // additional properties tests
+        $tests = array_merge($tests, array(
+            array(true, null, $propertyObject3, 'foo', array(), $anyType),
+        ));
+
         // additional method tests
         $tests = array_merge($tests, array(
             array(true, 'defined', $methodObject, 'defined',    array(), $methodType),
@@ -545,6 +551,16 @@ class Twig_TemplatePropertyObjectAndArrayAccess extends Twig_TemplatePropertyObj
     }
 }
 
+class Twig_TemplatePropertyObjectDefinedWithUndefinedValue
+{
+    public $foo;
+
+    public function __construct()
+    {
+        $this->foo = @$notExist;
+    }
+}
+
 class Twig_TemplateMethodObject
 {
     public function getDefined()