Program: 73722
Project: 73722
Buggy source:
/** 
 * Register a "published port" coming from a publisher. The name is the name being used in the matching process to match publisher and subscriber. A subscriber interested in the output of this publisher uses the same name. This registration process of publisher typically happens before the model is preinitialized, for example when opening the model. The subscribers will look for publishers during the preinitialization phase.
 * @param name The name is being used in the matching processto match publisher and subscriber.
 * @param port The published port. 
 * @param global If true, publish globally. If false, publishonly to subscribers that are fired by the same director.
 * @exception NameDuplicationException If the published portis already registered.
 * @exception IllegalActionException If the published port can'tbe added.
 */
public void registerPublisherPort(String name,IOPort port,boolean global) throws NameDuplicationException, IllegalActionException {
  NamedObj container=getContainer();
  if (!isOpaque() && container instanceof CompositeActor && !((CompositeActor)container).isClassDefinition()) {
    ((CompositeActor)container).registerPublisherPort(name,port,global);
  }
 else {
    if (_publishedPorts == null) {
      _publishedPorts=new HashMap<String,List<IOPort>>();
    }
    List<IOPort> portList=_publishedPorts.get(name);
    if (portList == null) {
      portList=new LinkedList<IOPort>();
      _publishedPorts.put(name,portList);
    }
    if (!portList.contains(port))     portList.add(port);
    if (global && this != toplevel()) {
      String portName="String_Node_Str" + StringUtilities.sanitizeName(name);
      IOPort publisherPort=(IOPort)getPort(portName);
      if (publisherPort == null) {
        publisherPort=(IOPort)newPort(portName);
        new Parameter(publisherPort,"String_Node_Str",BooleanToken.TRUE);
        publisherPort.setPersistent(false);
        publisherPort.setOutput(true);
        publisherPort.setMultiport(true);
      }
      linkToPublishedPort(name,publisherPort);
      if (container instanceof CompositeActor) {
        ((CompositeActor)container).registerPublisherPort(name,publisherPort,global);
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Register a "published port" coming from a publisher. The name is the name being used in the matching process to match publisher and subscriber. A subscriber interested in the output of this publisher uses the same name. This registration process of publisher typically happens before the model is preinitialized, for example when opening the model. The subscribers will look for publishers during the preinitialization phase.
 * @param name The name is being used in the matching processto match publisher and subscriber.
 * @param port The published port. 
 * @param global If true, publish globally. If false, publishonly to subscribers that are fired by the same director.
 * @exception NameDuplicationException If the published portis already registered.
 * @exception IllegalActionException If the published port can'tbe added.
 */
public void registerPublisherPort(String name,IOPort port,boolean global) throws NameDuplicationException, IllegalActionException {
  NamedObj container=getContainer();
  if (!isOpaque() && container instanceof CompositeActor && !((CompositeActor)container).isClassDefinition()) {
    ((CompositeActor)container).registerPublisherPort(name,port,global);
  }
 else {
    if (_publishedPorts == null) {
      _publishedPorts=new HashMap<String,List<IOPort>>();
    }
    List<IOPort> portList=_publishedPorts.get(name);
    if (portList == null) {
      portList=new LinkedList<IOPort>();
      _publishedPorts.put(name,portList);
    }
    if (!portList.contains(port))     portList.add(port);
    if (global && this != toplevel()) {
      String portName="String_Node_Str" + StringUtilities.sanitizeName(name);
      IOPort publisherPort=(IOPort)getPort(portName);
      if (publisherPort == null) {
        publisherPort=(IOPort)newPort(portName);
        new Parameter(publisherPort,"String_Node_Str",BooleanToken.TRUE);
        publisherPort.setPersistent(false);
        publisherPort.setOutput(true);
        publisherPort.setMultiport(true);
      }
      linkToPublishedPort(name,publisherPort);
      if (container instanceof CompositeActor) {
        ((CompositeActor)container).registerPublisherPort(name,publisherPort,global);
      }
    }
    if (global && this == toplevel()) {
      TypedIOPort discardPort=null;
      if (discardPort == null) {
        Discard discard=new Discard(this,uniqueName("String_Node_Str"));
        discard.setPersistent(false);
        new Parameter(discard,"String_Node_Str",BooleanToken.TRUE);
        discardPort=discard.input;
      }
      IORelation relation=_publisherRelations != null ? _publisherRelations.get(name) : null;
      if (relation == null) {
        IOPort publishedPort=getPublishedPort(name);
        try {
          relation=(IORelation)newRelation(uniqueName("String_Node_Str"));
        }
 catch (        NameDuplicationException e) {
          throw new IllegalStateException(e);
        }
        relation.setPersistent(false);
        new Parameter(relation,"String_Node_Str",BooleanToken.TRUE);
        publishedPort.liberalLink(relation);
        if (_publisherRelations == null) {
          _publisherRelations=new HashMap<String,IORelation>();
        }
        _publisherRelations.put(name,relation);
      }
      if (!discardPort.isLinked(relation)) {
        discardPort.liberalLink(relation);
        notifyConnectivityChange();
        Director director=getDirector();
        if (director != null) {
          director.invalidateSchedule();
          director.invalidateResolvedTypes();
        }
      }
    }
  }
}