php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41961 Search for overridden private methods strays from class hierarchy
Submitted: 2007-07-11 13:01 UTC Modified: 2007-07-12 10:32 UTC
From: robin_fernandes at uk dot ibm dot com Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2007-07-11 (snap) OS: Linux
Private report: No CVE-ID: None
 [2007-07-11 13:01 UTC] robin_fernandes at uk dot ibm dot com
Description:
------------
The wrong method is invoked if the following conditions are met:
1. A class has a public method that overrides/hides private method from its parent. 
2. An unrelated class has a public method of the same name.
3. The overriding method is invoked from within the unrelated class.

The unrelated class's method will be called instead of the overriding method. See the testcase below for an example.

Reproduce code:
---------------
The testcase is over 20 lines, but can be found here:
http://www.nomorepasting.com/getpaste.php?pasteid=1270

Expected result:
----------------
Called public ChildClass::secret() on an instance of: ChildClass

Actual result:
--------------
Called private X::secret() on an instance of: ChildClass

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-11 13:06 UTC] robin_fernandes at uk dot ibm dot com
Below is a _suggested_ fix to zend_object_handlers.c based on snap php5.2-200707111030. I have only tested on Linux using 'make test'.

Note that I am not highly familiar with the Zend Engine internals, so I could have overlooked a side effect or a better solution.

--- php5.2-theirs/Zend/zend_object_handlers.c 2007-03-23 17:30:59.000000000 +0000
+++ php5.2-mine/Zend/zend_object_handlers.c 2007-07-11 13:19:43.000000000 +0100
@@ -800,7 +800,7 @@
   /* Ensure that we haven't overridden a private function and end up calling
    * the overriding public function...
    */
-  if (EG(scope) && fbc->op_array.fn_flags & ZEND_ACC_CHANGED) {
+  if (EG(scope) && is_derived_class(fbc->common.scope, EG(scope)) && fbc->op_array.fn_flags & ZEND_ACC_CHANGED) {
    zend_function *priv_fbc;

    if (zend_hash_find(&EG(scope)->function_table, lc_method_name, method_len+1, (void **) &priv_fbc)==SUCCESS
 [2007-07-11 14:06 UTC] robin_fernandes at uk dot ibm dot com
Sorry - step 2 in the description should read:
"2. An unrelated class has a *private* method of the same name."
 [2007-07-12 10:32 UTC] dmitry@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 04:01:31 2024 UTC