Program: 57639
Project: 57639
Buggy source:
private void onMessageFromRegisteredChannel(WampRouterHandler handler,WampMessage msg){
  if (msg instanceof HelloMessage || msg instanceof WelcomeMessage) {
    closeActiveChannel(handler,new GoodbyeMessage(null,ApplicationError.INVALID_ARGUMENT));
  }
 else   if (msg instanceof AbortMessage || msg instanceof GoodbyeMessage) {
    handler.realm.removeChannel(handler,true);
    idleChannels.add(handler.ctx.channel());
    if (msg instanceof GoodbyeMessage) {
      GoodbyeMessage reply=new GoodbyeMessage(null,ApplicationError.GOODBYE_AND_OUT);
      handler.ctx.writeAndFlush(reply);
    }
  }
 else   if (msg instanceof CallMessage) {
    CallMessage call=(CallMessage)msg;
    String err=null;
    if (!UriValidator.tryValidate(call.procedure)) {
      err=ApplicationError.INVALID_URI;
    }
    if (err == null && !(IdValidator.isValidId(call.requestId))) {
      err=ApplicationError.INVALID_ARGUMENT;
    }
    Procedure proc=null;
    if (err == null) {
      proc=handler.realm.procedures.get(call.procedure);
      if (proc == null)       err=ApplicationError.NO_SUCH_PROCEDURE;
    }
    if (err != null) {
      ErrorMessage errMsg=new ErrorMessage(CallMessage.ID,call.requestId,null,err,null,null);
      handler.ctx.writeAndFlush(errMsg);
      return;
    }
    Invocation invoc=new Invocation();
    invoc.callRequestId=call.requestId;
    invoc.caller=handler;
    invoc.procedure=proc;
    invoc.invocationRequestId=IdGenerator.newLinearId(proc.provider.lastUsedId,proc.provider.pendingInvocations);
    proc.provider.lastUsedId=invoc.invocationRequestId;
    proc.provider.pendingInvocations.put(invoc.invocationRequestId,invoc);
    InvocationMessage imsg=new InvocationMessage(invoc.invocationRequestId,proc.registrationId,null,call.arguments,call.argumentsKw);
    proc.provider.ctx.writeAndFlush(imsg);
  }
 else   if (msg instanceof YieldMessage) {
    YieldMessage yield=(YieldMessage)msg;
    if (!(IdValidator.isValidId(yield.requestId)))     return;
    Invocation invoc=handler.pendingInvocations.get(yield.requestId);
    if (invoc == null)     return;
    handler.pendingInvocations.remove(yield.requestId);
    invoc.procedure.pendingCalls.remove(invoc);
    ResultMessage result=new ResultMessage(invoc.callRequestId,null,yield.arguments,yield.argumentsKw);
    invoc.caller.ctx.writeAndFlush(result);
  }
 else   if (msg instanceof ErrorMessage) {
    ErrorMessage err=(ErrorMessage)msg;
    if (!(IdValidator.isValidId(err.requestId))) {
      return;
    }
    if (err.requestType == InvocationMessage.ID) {
      if (!UriValidator.tryValidate(err.error)) {
        closeActiveChannel(handler,new GoodbyeMessage(null,ApplicationError.INVALID_ARGUMENT));
        return;
      }
      Invocation invoc=handler.pendingInvocations.get(err.requestId);
      if (invoc == null)       return;
      handler.pendingInvocations.remove(err.requestId);
      invoc.procedure.pendingCalls.remove(invoc);
      ErrorMessage fwdError=new ErrorMessage(CallMessage.ID,invoc.callRequestId,null,err.error,err.arguments,err.argumentsKw);
      invoc.caller.ctx.writeAndFlush(fwdError);
    }
  }
 else   if (msg instanceof RegisterMessage) {
    RegisterMessage reg=(RegisterMessage)msg;
    String err=null;
    if (!UriValidator.tryValidate(reg.procedure)) {
      err=ApplicationError.INVALID_URI;
    }
    if (err == null && !(IdValidator.isValidId(reg.requestId))) {
      err=ApplicationError.INVALID_ARGUMENT;
    }
    Procedure proc=null;
    if (err == null) {
      proc=handler.realm.procedures.get(reg.procedure);
      if (proc != null)       err=ApplicationError.PROCEDURE_ALREADY_EXISTS;
    }
    if (err != null) {
      ErrorMessage errMsg=new ErrorMessage(RegisterMessage.ID,reg.requestId,null,err,null,null);
      handler.ctx.writeAndFlush(errMsg);
      return;
    }
    long registrationId=IdGenerator.newLinearId(handler.lastUsedId,handler.providedProcedures);
    handler.lastUsedId=registrationId;
    Procedure procInfo=new Procedure(reg.procedure,handler,registrationId);
    handler.realm.procedures.put(reg.procedure,procInfo);
    if (handler.providedProcedures == null) {
      handler.providedProcedures=new HashMap<Long,WampRouter.Procedure>();
      handler.pendingInvocations=new HashMap<Long,WampRouter.Invocation>();
    }
    handler.providedProcedures.put(procInfo.registrationId,procInfo);
    RegisteredMessage response=new RegisteredMessage(reg.requestId,procInfo.registrationId);
    handler.ctx.writeAndFlush(response);
  }
 else   if (msg instanceof UnregisterMessage) {
    UnregisterMessage unreg=(UnregisterMessage)msg;
    String err=null;
    if (!(IdValidator.isValidId(unreg.requestId)) || !(IdValidator.isValidId(unreg.registrationId))) {
      err=ApplicationError.INVALID_ARGUMENT;
    }
    Procedure proc=null;
    if (err != null) {
      proc=handler.providedProcedures.get(unreg.registrationId);
      if (proc == null) {
        err=ApplicationError.NO_SUCH_REGISTRATION;
      }
    }
    if (err != null) {
      ErrorMessage errMsg=new ErrorMessage(UnregisterMessage.ID,unreg.requestId,null,err,null,null);
      handler.ctx.writeAndFlush(errMsg);
      return;
    }
    for (    Invocation invoc : proc.pendingCalls) {
      handler.pendingInvocations.remove(invoc.invocationRequestId);
      if (invoc.caller.state == RouterHandlerState.Open) {
        ErrorMessage errMsg=new ErrorMessage(CallMessage.ID,invoc.callRequestId,null,ApplicationError.NO_SUCH_PROCEDURE,null,null);
        invoc.caller.ctx.writeAndFlush(errMsg);
      }
    }
    proc.pendingCalls.clear();
    handler.realm.procedures.remove(proc.procName);
    handler.providedProcedures.remove(proc.registrationId);
    if (handler.providedProcedures.size() == 0) {
      handler.providedProcedures=null;
      handler.pendingInvocations=null;
    }
    UnregisteredMessage response=new UnregisteredMessage(unreg.requestId);
    handler.ctx.writeAndFlush(response);
  }
 else   if (msg instanceof SubscribeMessage) {
    SubscribeMessage sub=(SubscribeMessage)msg;
    String err=null;
    if (!UriValidator.tryValidate(sub.topic)) {
      err=ApplicationError.INVALID_URI;
    }
    if (err == null && !(IdValidator.isValidId(sub.requestId))) {
      err=ApplicationError.INVALID_ARGUMENT;
    }
    if (err != null) {
      ErrorMessage errMsg=new ErrorMessage(SubscribeMessage.ID,sub.requestId,null,err,null,null);
      handler.ctx.writeAndFlush(errMsg);
      return;
    }
    long subscriptionId=IdGenerator.newLinearId(handler.lastUsedId,handler.subscriptions);
    handler.lastUsedId=subscriptionId;
    Subscription s=new Subscription(sub.topic,subscriptionId,handler);
    Set<Subscription> subscriptionSet=handler.realm.subscriptions.get(sub.topic);
    if (subscriptionSet == null) {
      subscriptionSet=new HashSet<Subscription>();
      handler.realm.subscriptions.put(sub.topic,subscriptionSet);
    }
    subscriptionSet.add(s);
    if (handler.subscriptions == null) {
      handler.subscriptions=new HashMap<Long,WampRouter.Subscription>();
    }
    handler.subscriptions.put(subscriptionId,s);
    SubscribedMessage response=new SubscribedMessage(sub.requestId,subscriptionId);
    handler.ctx.writeAndFlush(response);
  }
 else   if (msg instanceof UnsubscribeMessage) {
    UnsubscribeMessage unsub=(UnsubscribeMessage)msg;
    String err=null;
    if (!(IdValidator.isValidId(unsub.requestId)) || !(IdValidator.isValidId(unsub.subscriptionId))) {
      err=ApplicationError.INVALID_ARGUMENT;
    }
    Subscription s=null;
    if (err == null) {
      if (handler.subscriptions != null) {
        s=handler.subscriptions.get(unsub.subscriptionId);
      }
      if (s == null) {
        err=ApplicationError.NO_SUCH_SUBSCRIPTION;
      }
    }
    if (err != null) {
      ErrorMessage errMsg=new ErrorMessage(UnsubscribeMessage.ID,unsub.requestId,null,err,null,null);
      handler.ctx.writeAndFlush(errMsg);
      return;
    }
    Set<Subscription> subscriptionSet=handler.realm.subscriptions.get(s.topic);
    subscriptionSet.remove(s);
    if (subscriptionSet.isEmpty()) {
      handler.realm.subscriptions.remove(s.topic);
    }
    handler.subscriptions.remove(unsub.subscriptionId);
    if (handler.subscriptions.isEmpty()) {
      handler.subscriptions=null;
    }
    UnsubscribedMessage response=new UnsubscribedMessage(unsub.requestId);
    handler.ctx.writeAndFlush(response);
  }
 else   if (msg instanceof PublishMessage) {
    PublishMessage pub=(PublishMessage)msg;
    String err=null;
    if (!UriValidator.tryValidate(pub.topic)) {
      err=ApplicationError.INVALID_URI;
    }
    if (err == null && !(IdValidator.isValidId(pub.requestId))) {
      err=ApplicationError.INVALID_ARGUMENT;
    }
    if (err != null) {
      ErrorMessage errMsg=new ErrorMessage(PublishMessage.ID,pub.requestId,null,err,null,null);
      handler.ctx.writeAndFlush(errMsg);
      return;
    }
    long publicationId=IdGenerator.newRandomId(null);
    Set<Subscription> subscriptionSet=handler.realm.subscriptions.get(pub.topic);
    if (subscriptionSet != null) {
      for (      Subscription subscriber : subscriptionSet) {
        if (subscriber.subscriber == handler)         continue;
        EventMessage ev=new EventMessage(subscriber.subscriptionId,publicationId,null,pub.arguments,pub.argumentsKw);
        subscriber.subscriber.ctx.writeAndFlush(ev);
      }
    }
    PublishedMessage response=new PublishedMessage(pub.requestId,publicationId);
    handler.ctx.writeAndFlush(response);
  }
}

Fixed version (ground truth):
private void onMessageFromRegisteredChannel(WampRouterHandler handler,WampMessage msg){
  if (msg instanceof HelloMessage || msg instanceof WelcomeMessage) {
    closeActiveChannel(handler,new GoodbyeMessage(null,ApplicationError.INVALID_ARGUMENT));
  }
 else   if (msg instanceof AbortMessage || msg instanceof GoodbyeMessage) {
    handler.realm.removeChannel(handler,true);
    idleChannels.add(handler.ctx.channel());
    if (msg instanceof GoodbyeMessage) {
      GoodbyeMessage reply=new GoodbyeMessage(null,ApplicationError.GOODBYE_AND_OUT);
      handler.ctx.writeAndFlush(reply);
    }
  }
 else   if (msg instanceof CallMessage) {
    CallMessage call=(CallMessage)msg;
    String err=null;
    if (!UriValidator.tryValidate(call.procedure)) {
      err=ApplicationError.INVALID_URI;
    }
    if (err == null && !(IdValidator.isValidId(call.requestId))) {
      err=ApplicationError.INVALID_ARGUMENT;
    }
    Procedure proc=null;
    if (err == null) {
      proc=handler.realm.procedures.get(call.procedure);
      if (proc == null)       err=ApplicationError.NO_SUCH_PROCEDURE;
    }
    if (err != null) {
      ErrorMessage errMsg=new ErrorMessage(CallMessage.ID,call.requestId,null,err,null,null);
      handler.ctx.writeAndFlush(errMsg);
      return;
    }
    Invocation invoc=new Invocation();
    invoc.callRequestId=call.requestId;
    invoc.caller=handler;
    invoc.procedure=proc;
    invoc.invocationRequestId=IdGenerator.newLinearId(proc.provider.lastUsedId,proc.provider.pendingInvocations);
    proc.provider.lastUsedId=invoc.invocationRequestId;
    proc.provider.pendingInvocations.put(invoc.invocationRequestId,invoc);
    proc.pendingCalls.add(invoc);
    InvocationMessage imsg=new InvocationMessage(invoc.invocationRequestId,proc.registrationId,null,call.arguments,call.argumentsKw);
    proc.provider.ctx.writeAndFlush(imsg);
  }
 else   if (msg instanceof YieldMessage) {
    YieldMessage yield=(YieldMessage)msg;
    if (!(IdValidator.isValidId(yield.requestId)))     return;
    if (handler.pendingInvocations == null)     return;
    Invocation invoc=handler.pendingInvocations.get(yield.requestId);
    if (invoc == null)     return;
    handler.pendingInvocations.remove(yield.requestId);
    invoc.procedure.pendingCalls.remove(invoc);
    ResultMessage result=new ResultMessage(invoc.callRequestId,null,yield.arguments,yield.argumentsKw);
    invoc.caller.ctx.writeAndFlush(result);
  }
 else   if (msg instanceof ErrorMessage) {
    ErrorMessage err=(ErrorMessage)msg;
    if (!(IdValidator.isValidId(err.requestId))) {
      return;
    }
    if (err.requestType == InvocationMessage.ID) {
      if (!UriValidator.tryValidate(err.error)) {
        closeActiveChannel(handler,new GoodbyeMessage(null,ApplicationError.INVALID_ARGUMENT));
        return;
      }
      if (handler.pendingInvocations == null)       return;
      Invocation invoc=handler.pendingInvocations.get(err.requestId);
      if (invoc == null)       return;
      handler.pendingInvocations.remove(err.requestId);
      invoc.procedure.pendingCalls.remove(invoc);
      ErrorMessage fwdError=new ErrorMessage(CallMessage.ID,invoc.callRequestId,null,err.error,err.arguments,err.argumentsKw);
      invoc.caller.ctx.writeAndFlush(fwdError);
    }
  }
 else   if (msg instanceof RegisterMessage) {
    RegisterMessage reg=(RegisterMessage)msg;
    String err=null;
    if (!UriValidator.tryValidate(reg.procedure)) {
      err=ApplicationError.INVALID_URI;
    }
    if (err == null && !(IdValidator.isValidId(reg.requestId))) {
      err=ApplicationError.INVALID_ARGUMENT;
    }
    Procedure proc=null;
    if (err == null) {
      proc=handler.realm.procedures.get(reg.procedure);
      if (proc != null)       err=ApplicationError.PROCEDURE_ALREADY_EXISTS;
    }
    if (err != null) {
      ErrorMessage errMsg=new ErrorMessage(RegisterMessage.ID,reg.requestId,null,err,null,null);
      handler.ctx.writeAndFlush(errMsg);
      return;
    }
    long registrationId=IdGenerator.newLinearId(handler.lastUsedId,handler.providedProcedures);
    handler.lastUsedId=registrationId;
    Procedure procInfo=new Procedure(reg.procedure,handler,registrationId);
    handler.realm.procedures.put(reg.procedure,procInfo);
    if (handler.providedProcedures == null) {
      handler.providedProcedures=new HashMap<Long,WampRouter.Procedure>();
      handler.pendingInvocations=new HashMap<Long,WampRouter.Invocation>();
    }
    handler.providedProcedures.put(procInfo.registrationId,procInfo);
    RegisteredMessage response=new RegisteredMessage(reg.requestId,procInfo.registrationId);
    handler.ctx.writeAndFlush(response);
  }
 else   if (msg instanceof UnregisterMessage) {
    UnregisterMessage unreg=(UnregisterMessage)msg;
    String err=null;
    if (!(IdValidator.isValidId(unreg.requestId)) || !(IdValidator.isValidId(unreg.registrationId))) {
      err=ApplicationError.INVALID_ARGUMENT;
    }
    Procedure proc=null;
    if (err == null) {
      if (handler.providedProcedures != null) {
        proc=handler.providedProcedures.get(unreg.registrationId);
      }
      if (proc == null) {
        err=ApplicationError.NO_SUCH_REGISTRATION;
      }
    }
    if (err != null) {
      ErrorMessage errMsg=new ErrorMessage(UnregisterMessage.ID,unreg.requestId,null,err,null,null);
      handler.ctx.writeAndFlush(errMsg);
      return;
    }
    for (    Invocation invoc : proc.pendingCalls) {
      handler.pendingInvocations.remove(invoc.invocationRequestId);
      if (invoc.caller.state == RouterHandlerState.Open) {
        ErrorMessage errMsg=new ErrorMessage(CallMessage.ID,invoc.callRequestId,null,ApplicationError.NO_SUCH_PROCEDURE,null,null);
        invoc.caller.ctx.writeAndFlush(errMsg);
      }
    }
    proc.pendingCalls.clear();
    handler.realm.procedures.remove(proc.procName);
    handler.providedProcedures.remove(proc.registrationId);
    if (handler.providedProcedures.size() == 0) {
      handler.providedProcedures=null;
      handler.pendingInvocations=null;
    }
    UnregisteredMessage response=new UnregisteredMessage(unreg.requestId);
    handler.ctx.writeAndFlush(response);
  }
 else   if (msg instanceof SubscribeMessage) {
    SubscribeMessage sub=(SubscribeMessage)msg;
    String err=null;
    if (!UriValidator.tryValidate(sub.topic)) {
      err=ApplicationError.INVALID_URI;
    }
    if (err == null && !(IdValidator.isValidId(sub.requestId))) {
      err=ApplicationError.INVALID_ARGUMENT;
    }
    if (err != null) {
      ErrorMessage errMsg=new ErrorMessage(SubscribeMessage.ID,sub.requestId,null,err,null,null);
      handler.ctx.writeAndFlush(errMsg);
      return;
    }
    long subscriptionId=IdGenerator.newLinearId(handler.lastUsedId,handler.subscriptions);
    handler.lastUsedId=subscriptionId;
    Subscription s=new Subscription(sub.topic,subscriptionId,handler);
    Set<Subscription> subscriptionSet=handler.realm.subscriptions.get(sub.topic);
    if (subscriptionSet == null) {
      subscriptionSet=new HashSet<Subscription>();
      handler.realm.subscriptions.put(sub.topic,subscriptionSet);
    }
    subscriptionSet.add(s);
    if (handler.subscriptions == null) {
      handler.subscriptions=new HashMap<Long,WampRouter.Subscription>();
    }
    handler.subscriptions.put(subscriptionId,s);
    SubscribedMessage response=new SubscribedMessage(sub.requestId,subscriptionId);
    handler.ctx.writeAndFlush(response);
  }
 else   if (msg instanceof UnsubscribeMessage) {
    UnsubscribeMessage unsub=(UnsubscribeMessage)msg;
    String err=null;
    if (!(IdValidator.isValidId(unsub.requestId)) || !(IdValidator.isValidId(unsub.subscriptionId))) {
      err=ApplicationError.INVALID_ARGUMENT;
    }
    Subscription s=null;
    if (err == null) {
      if (handler.subscriptions != null) {
        s=handler.subscriptions.get(unsub.subscriptionId);
      }
      if (s == null) {
        err=ApplicationError.NO_SUCH_SUBSCRIPTION;
      }
    }
    if (err != null) {
      ErrorMessage errMsg=new ErrorMessage(UnsubscribeMessage.ID,unsub.requestId,null,err,null,null);
      handler.ctx.writeAndFlush(errMsg);
      return;
    }
    Set<Subscription> subscriptionSet=handler.realm.subscriptions.get(s.topic);
    subscriptionSet.remove(s);
    if (subscriptionSet.isEmpty()) {
      handler.realm.subscriptions.remove(s.topic);
    }
    handler.subscriptions.remove(unsub.subscriptionId);
    if (handler.subscriptions.isEmpty()) {
      handler.subscriptions=null;
    }
    UnsubscribedMessage response=new UnsubscribedMessage(unsub.requestId);
    handler.ctx.writeAndFlush(response);
  }
 else   if (msg instanceof PublishMessage) {
    PublishMessage pub=(PublishMessage)msg;
    String err=null;
    if (!UriValidator.tryValidate(pub.topic)) {
      err=ApplicationError.INVALID_URI;
    }
    if (err == null && !(IdValidator.isValidId(pub.requestId))) {
      err=ApplicationError.INVALID_ARGUMENT;
    }
    if (err != null) {
      ErrorMessage errMsg=new ErrorMessage(PublishMessage.ID,pub.requestId,null,err,null,null);
      handler.ctx.writeAndFlush(errMsg);
      return;
    }
    long publicationId=IdGenerator.newRandomId(null);
    Set<Subscription> subscriptionSet=handler.realm.subscriptions.get(pub.topic);
    if (subscriptionSet != null) {
      for (      Subscription subscriber : subscriptionSet) {
        if (subscriber.subscriber == handler)         continue;
        EventMessage ev=new EventMessage(subscriber.subscriptionId,publicationId,null,pub.arguments,pub.argumentsKw);
        subscriber.subscriber.ctx.writeAndFlush(ev);
      }
    }
    PublishedMessage response=new PublishedMessage(pub.requestId,publicationId);
    handler.ctx.writeAndFlush(response);
  }
}