nikic / scalar_objects

Extension that adds support for method calls on primitive types in PHP
MIT License
1.13k stars 44 forks source link

Fix segmentation fault when invoking __callStatic() #11

Closed thekid closed 10 years ago

thekid commented 10 years ago

This pull request fixes a segmentation fault when invocations fall back to __callStatic:

class h {
  static function __callStatic($name, $args) { }
}

register_primitive_type_handler('null', 'h');
$x= null;
return $x->method();

The hack that $this is available in the primitive handler methods and points to the value the method is called on although they're actually static methods doesn't work here. See the comment in zend_vm_def.h in its ZEND_VM_HELPER(zend_do_fcall_common_helper...): An internal function assumes $this is present and won't check that. So PHP would crash by allowing the call. Why the Zend Engine treats __callStatic as an internal function is not 100% clear to me; an educated guess is that it's for performance reasons.

thekid commented 10 years ago

The added test passes, but for some reason tests/string.phpt fails on Travis (but works locally on my machine). Might be related to me using PHP head checkout and Travis using PHP 5.4

nikic commented 10 years ago

Thanks, this is merged now! See https://github.com/nikic/scalar_objects/commit/9d60e480dcd723d54f13b0ad5610880e4cdbfd65. I removed the zval_dtor(obj) call, as it the object will be destroyed by FREE_OP1_IF_VAR() already.

Btw, the string.phpt issue you encountered is actually a bug in PHP - just submitted a report: https://bugs.php.net/bug.php?id=67151