https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80393

            Bug ID: 80393
           Summary: class_replaceMethod() can modify the wrong class
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libobjc
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rfm at gnu dot org
  Target Milestone: ---

The current implementation in libobjc is

IMP
class_replaceMethod (Class class_, SEL selector, IMP implementation,
                     const char *method_types)
{
 struct objc_method * method;

 if (class_ == Nil  ||  selector == NULL  ||  implementation == NULL
     || method_types == NULL)
   return NULL;

 method = search_for_method_in_hierarchy (class_, selector);

 if (method)
   {
     return method_setImplementation (method, implementation);
   }
 else
   {
     class_addMethod (class_, selector, implementation, method_types);
     return NULL;
   }
}

The problem is that the call to search_for_method_in_hierarchy() searches not
only the class that we are interested in, but also all its superclasses.  So if
the method we are searching for is implemented in one of the superclasses and
not already overridden (which is a common case), the code finds that method and
changes it, resulting in a change to the superclass implementation of the
method (and any other of its subclasses) as well as the class we actually
wanted to change.

The solution would seem to be to use search_for_method_in_list rather than
search_for_method_in_hierarchy.

Reply via email to